在matplotlib中使用XYZ数据的2D彩色图

时间:2013-05-30 15:41:42

标签: map colors matplotlib 2d

我有三个列式数据集来自

形式的模拟
x, y, z

看起来像这样:

0.0000000E+00  0.000000000000000E+000   1.00000000000000
0.0000000E+00  0.200000002980232        1.00000000000000
0.0000000E+00  0.400000005960464        1.00000000000000
0.0000000E+00  0.600000008940697        1.00000000000000
0.0000000E+00  0.800000011920929        1.00000000000000
0.0000000E+00   1.00000001490116        1.00000000000000

0.1000000      0.000000000000000E+000  0.974332364008348
0.1000000      0.200000002980232       0.974332364008348
0.1000000      0.400000005960464       0.974332364008348
0.1000000      0.600000008940697       0.974332364008348
0.1000000      0.800000011920929       0.974332364008348
0.1000000       1.00000001490116       0.974332364008348

0.2000000      0.000000000000000E+000  0.999148125725412
0.2000000      0.200000002980232       0.999148125725412
0.2000000      0.400000005960464       0.999148125725412
0.2000000      0.600000008940697       0.999148125725412
0.2000000      0.800000011920929       0.999148125725412
0.2000000       1.00000001490116       0.999148125725412

...

我想制作我的XYZ数据的二维彩色地图,其中x和y只是坐标,z是每个点的值。

在GNUPLOT中,这很容易做到:

如果我使用

set pm3d map

splot 'datafile.txt'

我获得了正确的情节。

但现在我想知道如何在matplotlib中实现这一目标。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我的X和Y值是数组,如下所示:

 x_vals = np.linspace(500, 40000, 160)
 y_vals = np.linspace(100, 5000, 50)

我的Z值是一个2D数组,在循环中计算如下:

z_vals = []
for this_y in y_vals:
    this_z = []
    for this_x in x_vals:
        this_val = gain_for_position(this_x, this_y, val_a, val_b)
        this_z.append(this_val)
    z_vals.append(this_z)

我绘制的情节用以下代码绘制:

plt.contourf(x_vals, y_vals, z_vals, 8)
contour_labels = plt.contour(x_vals, y_vals,
                             z_vals, 8, colors='black', linewidth=.5)
plt.clabel(contour_labels, inline=1, fontsize=10)

并产生了这样的情节。enter image description here