x和y应该代表z值从其出现的平面。我创建了一个随机矩阵,以与x,y中的3x10值配对。
这是错误消息:
ValueError:形状不匹配:对象无法广播为单个形状
import matplotlib.pyplot as plt
import numpy as np
x = [0,1,2,3,4,5,6,7,8,9,10] #creating random data
y = [0,1,2,3]
a = np.random.rand (3, 10)
z = np.array(a, ndmin=2) #not really sure if this piece is necessary.
fig = plt.figure()
ax = fig.add_subplot(1,1,1, projection='3d')
x, y = np.meshgrid(x, y)
ax.plot_surface(x, y, z)
plt.show()
ValueError:形状不匹配:对象无法广播为单个形状
我已经尝试过将“ z = np.array(a,ndmin = 2)”排除在外。也没用。
答案 0 :(得分:1)
问题有两个:
Axes3D
以启用3d绘图。我认为您不需要额外使用z = np.array(a, ndmin=2)
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
x = [0,1,2,3,4,5,6,7,8,9,10] #creating random data
y = [0,1,2,3]
a = np.random.rand(4, 11)
x, y = np.meshgrid(x, y)
fig = plt.figure()
ax = fig.add_subplot(1,1,1, projection='3d')
ax.plot_surface(x, y, a)
plt.show()