matplotlib将来自csv的X Y Z数据绘制为pcolormesh [英] matplotlib plot X Y Z data from csv as pcolormesh

查看:185
本文介绍了matplotlib将来自csv的X Y Z数据绘制为pcolormesh的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据格式如下:

X,Y,Z
0,0,0.0
0,1,0.0
1,0,1.0
1,1,0.55
2,0,4.0
2,1,3.216

我不确定如何将数据输入到pcolormesh.我想我必须使用np.meshgrid,但是在这种情况下我不确定如何使用.

I am not sure how to feed this data to pcolormesh. I think I have to use np.meshgrid but I'm not sure how to in this case.

dat = pd.read_csv('my_dat.csv')
plt.pcolormesh(dat['X'], dat['Y'], dat['Z'])
plt.show()

Value error: need more than one value to unpack

我不明白-为什么这行不通?

I don't understand - why doesn't this just work?

推荐答案

您的数据仅需要重塑.此处无需使用np.meshgrid,因为每个单元格已经有了x和y坐标.

Your data just needs to be reshaped. No need to use np.meshgrid here, since you already have an x and y coord for each cell.

如果x坐标为nx,y坐标为ny,则可以执行以下操作:

If you have nx coordinates in x, and ny coordinates in y, then you can do this:

X = dat['X'].reshape(nx,ny).T
Y = dat['Y'].reshape(nx,ny).T
Z = dat['Z'].reshape(nx,ny).T

plt.pcolormesh(X,Y,Z)
plt.show()

请注意,pcolormesh希望您的xy尺寸比z尺寸大一倍,因为xy定义了单元格的边缘,而z定义了单元格的边缘单元中心的颜色.从文档中:

Note that pcolormesh prefers that you have your x and y dimensions be one greater that the z dimension, since x and y define the edges of the cells, and z defines the colour at the cell centre. From the docs:

理想地,X和Y的尺寸应比C的尺寸大一;如果尺寸相同,则C的最后一行和最后一列将被忽略.

Ideally the dimensions of X and Y should be one greater than those of C; if the dimensions are the same, then the last row and column of C will be ignored.

因此,在您的示例中,除非添加的虚拟单元格的行和列的x和y坐标比您的单元格数大1,否则最后一行的颜色将丢失.一种替代方法可能是使用 plt.contourf ,对于x,y ,并且z的长度应该相同.

So, in you example, the colours from the final row and column will be lost, unless you add a row and column of dummy cells with x and y coordinates 1 greater than your number of cells. An alternative might be to use plt.contourf, for which x, y, and z should be the same length.

这篇关于matplotlib将来自csv的X Y Z数据绘制为pcolormesh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆