【发布时间】:2019-04-14 06:31:05
【问题描述】:
我正在尝试使用 this site 中的代码使用 matplotlib 绘制 3d Surface plots:
X,Y,Z 得到如下:
from math import pi
from numpy import cos, meshgrid
alpha = 0.7
phi_ext = 2 * pi * 0.5
def flux_qubit_potential(phi_m, phi_p):
return 2 + alpha - 2 * cos(phi_p)*cos(phi_m) - alpha * cos(phi_ext - 2*phi_p)
phi_m = linspace(0, 2*pi, 100)
phi_p = linspace(0, 2*pi, 100)
X,Y = meshgrid(phi_p, phi_m)
Z = flux_qubit_potential(X, Y).T
3d 绘图是使用以下代码完成的:
from mpl_toolkits.mplot3d.axes3d import Axes3D
fig = plt.figure(figsize=(14,6))
# `ax` is a 3D-aware axis instance, because of the projection='3d' keyword argument to add_subplot
ax = fig.add_subplot(1, 2, 1, projection='3d')
p = ax.plot_surface(X, Y, Z, rstride=4, cstride=4, linewidth=0)
# surface_plot with color grading and color bar
ax = fig.add_subplot(1, 2, 2, projection='3d')
p = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)
cb = fig.colorbar(p, shrink=0.5)
但是,如果我将 X、Y 和 Z 替换为我的 x、y、z 3d 数据(示例如下),则会出现 Z has to be 2 dimensional 错误。如何使用通常的 x、y、z 值进行绘图,如下所示:
x y z
0 12 0 0.1
1 13 1 0.8
2 14 3 1.0
3 16 4 1.2
4 18 4 0.7
【问题讨论】:
-
“我的 x,y,z 3d 数据”是什么意思?您的代码中的任何地方都没有引用任何此类内容。
-
我在上面添加了我的数据样本。
-
关于“为什么?”,Why does pyplot.contour() require Z to be a 2D array?。我非常倾向于将其作为Simplest way to plot 3d surface given 3d points 的副本关闭,除非问题说明了为什么不是。
-
@rnso 您问题中的链接已失效。
-
该网站必须已被删除。代码已经在问题中了。
标签: python matplotlib 3d