Matplotlib 3D ValueError shape mismatch:objects cannot be broadcast to a single shape [duplicate](Matplotlib 3D ValueError shape mismatch:objects cannot be broadcast to a single shape [duplicate])

kuarbcqp  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(183)

这个问题已经有答案了

Parameters required by bar3d with python(1个答案)
11天前关闭.

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import numpy as np

plt.style.use('dark_background')
fig = plt.figure()
ax = plt.axes(projection='3d')

x = np.arange(1,11)
y = np.array([5,6,7,8,2,5,6,3,7,2])
z = np.zeros(10)

dx = np.ones(10)
dy = np.ones(10)
dz = np.arange(1,100)

ax.bar3d(x,y,z,dx,dy,dz,color=np.array(['r','b','y','k','orange','lime','aqua','c','m','g']))
ax.set_xlabel('x-axis')
ax.set_ylabel('y-axis')
ax.set_zlabel('z-axis')
ax.set_title('3D Bar Chart')`

plt.show()

为什么我得到这个错误当我运行这个?
Exception has occurred: ValueError shape mismatch: objects cannot be broadcast to a single shape. Mismatch is between arg 0 with shape (10,) and arg 5 with shape (99,).我希望这段代码可以运行良好,并绘制我的图

rryofs0p

rryofs0p1#

@Marco为你指明了正确的方向。
如果你不知道参数的作用,可以查看文档。在你的例子中,输入help(ax.bar3d),它会给你给予这样的东西:

Generate a 3D barplot.

This method creates three-dimensional barplot where the width,
depth, height, and color of the bars can all be uniquely set.

Parameters
----------
x, y, z : array-like
    The coordinates of the anchor point of the bars.

dx, dy, dz : float or array-like
    The width, depth, and height of the bars, respectively.

所以,如果你想修改每个条形图的大小,dx, dy, dz应该有与x, y, z相同数量的元素。

相关问题