使用Matplotlib绘制2D阵列
所以我有一个二维数组(名为Data),看起来像:
So I have a 2D array (named Data) that looks like:
Shape 0 Shape 1 ... Shape N
------- ------- -------
Scale 0 | Value00 , Value01 ... Value0N |
Scale 1 | Value10 , Value11 ... Value1N |
.
.
.
Scale N | ValueN0 , ValueN1 ... ValueNN |
我想创建一个3D图,其中ValueXXs是Z轴.我尝试了两次,但是每次都给我一个相对于另一个旋转的表面,所以我让自己有些困惑.这是我第一次尝试解决方案:
And I want to create a 3D plot where the ValueXXs are the Z axis. I've tried two attempts, but each give me a surface that is rotated with respect to the other one, so I've gotten myself a bit confused. Here is my 1st attempt at a solution:
x,y = numpy.mgrid[0:50:50j,0:50:50j]
f = Data
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(x,y,f,rstride=1,cstride=1)
这是我的第二次尝试:
nx, ny = 50, 50
x = range(nx)
y = range(ny)
hf = plt.figure()
ha = hf.add_subplot(111, projection='3d')
X, Y = numpy.meshgrid(x, y)
ha.plot_surface(X,Y,Data,rstride=1,cstride=1)
检查X和Y确实没有帮助,因为它是正方形.我不确定X何时代表我的比例",何时代表我的形状".
Examining X and Y does no help really because its a square. I'm not sure when X represents my 'Scale' vs when it is representing my 'Shape'.
那么,这两个例子到底是怎么回事?有没有更好的方法来绘制此数组?
So, what is really going on with these two examples? Is there a better way to plot this array?
谢谢!
如果我正确地理解您的话,那么困惑是哪个轴是哪个轴,对吗?在这种情况下,您可以轻松绘制已知的不对称形状,并且绘图将告诉您所有信息.例如,采用画廊中的示例:
If I understand you right, the confusion is which axis is which, right? If this is the case, you can easily plot a known asymmetric shape and the plot will tell you everything. For example, adopting an example from the gallery:
# By Armin Moser
from mpl_toolkits.mplot3d import Axes3D
import matplotlib
import numpy as np
from matplotlib import cm
from matplotlib import pyplot as plt
step = 0.04
maxval = 1.0
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# create supporting points in polar coordinates
r = np.linspace(0,1.25,50)
p = np.linspace(0,2*np.pi,50)
R,P = np.meshgrid(r,p)
# transform them to cartesian system
X,Y = R*np.cos(P),R*np.sin(P)
#Z = ((R**2 - 1)**2)
Z = (X**2 + 0.2*Y**2 -1)**2 # <------- edit
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet)
#ax.set_zlim3d(0, 1)
ax.set_xlabel(r'$\phi_\mathrm{real}$')
ax.set_ylabel(r'$\phi_\mathrm{im}$')
ax.set_zlabel(r'$V(\phi)$')
plt.show()