【问题标题】:Contour plot python with dataset等高线图 python 与数据集
【发布时间】:2023-02-08 16:56:36
【问题描述】:

当我的 z 值是我拥有的磁场的大小时,我需要等高线图 python 的帮助。但是,我总是得到 z 值必须是二维的。我的 x 值和 y 值是 x 轴和 yaxis。

我想知道如何替换我的数据以获得等值线图

【问题讨论】:

  • 请提供足够的代码,以便其他人可以更好地理解或重现问题。

标签: python plot contour


【解决方案1】:

由于您没有提供任何数据,我假设您的磁场数据是纬度、经度和 MagZ(或类似的东西)的 csv 格式。

获取此类数据最合理的地方是 NOAA 的 WMM 网格计算器 (Link to the online calculator)。这会生成一个以空格分隔的数据文件,如下所示:

-90.00 -180.00 420.0000 2022.15   44788.48  145.00
-90.00 -179.00 420.0000 2022.15   44788.48  145.00
-90.00 -178.00 420.0000 2022.15   44788.48  145.00
-90.00 -177.00 420.0000 2022.15   44788.48  145.00
-90.00 -176.00 420.0000 2022.15   44788.48  145.00

在我的示例中,我在 420 公里的高度上以 1 度为步长测量了整个地球的磁场强度。您当然也可以只为磁场的 z 分量生成网格。

要将其导入 python,一定要使用 pandas DataFrames:

import pandas as pd

mag_f = pd.read_csv('models/mag_model_f.txt', delim_whitespace=True)
mag_f.columns = ['lat', 'lon', 'alt', 'date', 'mag_f', 'aux']

作为下一步,我想将我的 DataFrame 转换为数据透视表,因为这使得可视化数据变得非常容易:

mag_table = mag.pivot_table(values='mag_f', index='lat', columns='lon')

使用 matplotlib 的 imshow 您可以生成磁场的“热图” - 并添加等高线:

fig, ax = plt.subplots()
# draw magnetic field map
ax.imshow(mag_table, cmap='jet', interpolation='nearest')
# add contour lines
contours = ax.contour(mag_table, 20, colors='black', linewidths=0.3)

结果是这样的:

Magnetic field of earth with contour lines

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 2022-10-22
    相关资源
    最近更新 更多