如何在Python中将3D数据绘制为2D网格颜色图? [英] How to plot 3D data as 2D grid colormap in Python?
问题描述
我有3D数据作为numpy数组的列(也就是说,array [0] = [0,0,0]等),例如
I have 3D data as the columns of a numpy array (that is to say that array[0] = [0,0,0], etc.), e.g.
X Y Z
0 0 0
0 1 10
1 0 20
1 1 30
我想对此进行绘制,以使每个(X,Y)坐标在该坐标上均具有一个正方形,并带有一个从(例如)0到30的色条显示Z值.
I would like to plot this so that each (X,Y) co-ordinate has a square centered on the co-ordinate, with a colorbar from (e.g.) 0 to 30 showing the Z value.
然后我想覆盖一些轮廓线,但是问题的第一部分是最重要的.
I would then like to overlay some contour lines, but the first part of the question is most important.
对于已经掌握数据的人有帮助,但是我不确定最好的matplotlib例程来调用我的列数据.另外,这是用于科学出版物,因此需要具有良好的质量和外观!希望有人能帮忙!
There is help for people who have already-gridded data, but I am not sure of the best matplotlib routine to call for my column data. Also, this is for scientific publication, so needs to be of a good quality and look! Hope someone can help!
推荐答案
您可以使用matplotlib.mlab
中的griddata
正确地对数据进行网格化.
You can use griddata
from matplotlib.mlab
to grid your data properly.
import numpy as np
from matplotlib.mlab import griddata
x = np.array([0,0,1,1])
y = np.array([0,1,0,1])
z = np.array([0,10,20,30])
xi = np.arange(x.min(),x.max()+1)
yi = np.arange(y.min(),y.max()+1)
ar = griddata(x,y,z,xi,yi)
# ar is now
# array([[ 0., 20.],
# [ 10., 30.]])
映射的xi
和yi
点的选择取决于您,它们不必是整数,因为griddata
可以为您插值.
The choice of the mapped xi
and yi
points is up to you, and they do not have to be integers as griddata
can interpolate for you.
这篇关于如何在Python中将3D数据绘制为2D网格颜色图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!