在python中将pcolormesh与3个一维数组一起使用 [英] Using pcolormesh with 3 one dimensional arrays in python
问题描述
我正在尝试使用pcolormesh绘制3个一维数组,其中填充了坐标和值.下面的数组只是示例,但它们遵循相同的原理.我有3个数组,一个数组用于x坐标,一个数组用于y坐标,一个数组用于它们的值(或z).每个索引对应一个位置.索引0对应于x等于1,y等于1,z等于6.所有索引均如此.
I am trying to use pcolormesh to plot 3 one dimensional arrays filled with coordinates and values. The arrays below are simply examples, yet they follow the same principle. I have 3 arrays, one for the x coordinate, one for the y coordinate, and one for their value (or z). Each index corresponds to a location. Index of 0 corresponds to x of 1, y of 1 and z of 6. This goes on for all indexes.
x = [1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4]
y = [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4]
z = [6,2,3,5,8,2,3,2,4,2,3,6,7,3,3,2]
plt.figure()
plt.pcolor(x,y,z)
plt.colorbar()
plt.show()
我也尝试过...
x = [1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4]
y = [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4]
z = [6,2,3,5,8,2,3,2,4,2,3,6,7,3,3,2]
N = np.sqrt(len(x))
N = int(N)
np.reshape(x,(N,N))
np.reshape(y,(N,N))
np.reshape(z,(N,N))
plt.pcolormesh(x,y,c=z)
plt.show()
似乎没有任何作用.发送帮助
Nothing seems to be working. Send Help
推荐答案
您的第二次尝试是正确的方向-除了两件事:
Your second attempt was the right direction - except for two things:
-
np.reshape
返回整形后的数组,它没有应用于该数组参数. - if 您要按名称指定第三个kwarg-它是大写的
C
,而不是小写的c
np.reshape
returns the reshaped array, it is not applied to the array parameter.- if you want to specify the third kwarg by name - it's a capital
C
not a lower casec
这可行:
x = [1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4]
y = [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4]
z = [6,2,3,5,8,2,3,2,4,2,3,6,7,3,3,2]
x = np.reshape(x,(4, 4))
y = np.reshape(y,(4, 4))
z = np.reshape(z,(4, 4))
plt.pcolormesh(x, y, z)
但是,更清洁的阅读方式可能是这样的:
However, cleaner to read perhaps would be sth like this:
x, y = np.meshgrid(range(4), range(4))
z = [6, 2, 3, 5, 8, 2, 3, 2, 4, 2, 3, 6, 7, 3, 3, 2]
z = np.reshape(z, (4, 4))
plt.pcolormesh(x, y, z)
这就是制作 meshgrid
的用例.
这篇关于在python中将pcolormesh与3个一维数组一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!