从 3D 列表绘制 3d 条形图
plotting 3d barchart from a 3D list
我的数据集看起来像(包含 ~25 个数据点):
x=[2.225 2.325 2.425 2.075 2.375 1.925 1.975 1.775 1.975 2.375]
y=[147.75 130.25 161.75 147.75 165.25 151.25 158.25 151.25 172.25 123.25]
z=[-1.36, -0.401, -0.741, -0.623, -0.44, -0.37, 0.120, 2.8, 0.026, -1.19]
我正在尝试使用数据绘制 3D 条形图。
我正在尝试:
from mpl_toolkits.mplot3d import Axes3D
fig_3d_bar = plt.figure(figsize=(7, 5))
dx = fig_3d_bar.add_subplot(111, projection='3d')
x_pos = np.array(x)
y_pos = np.array(y)
z_pos = np.zeros(len(x))
dx = np.ones(len(x))
dy = np.ones(len(y))
dz = z
dx.bar3d(x_pos, y_pos, z_pos, dx, dy, dz, color='#00ceaa')
但这给我一个错误报告:
dx.bar3d(x_pos, y_pos, z_pos, dx, dy, dz, color='#00ceaa')
AttributeError: 'numpy.ndarray' object has no attribute 'bar3d'
一点帮助就大有帮助。不知道怎么回事。
谢谢。
您的代码有错误。您对 Axes
对象和条形大小都使用变量名称 dx
。我猜你想要
ax = fig_3d_bar.add_subplot(111, projection='3d')
ax.bar3d(x_pos, y_pos, z_pos, dx, dy, dz, color='#00ceaa')
条形尺寸
由于 x 数据和 y 数据的比例不同,条形图在图中显得很宽。您可以通过相应地缩放 dx
和 dy
来调整它们。
dx = np.ones(len(x))*0.1
dy = np.ones(len(y))*5
条形颜色
条形的颜色可以通过使用 ScalarMappable instance. For this you need a norm object that scales the z-values to the range [0,1]. You can choose any of the predefined colormaps 或创建您自己的颜色来适应 z 值。
import matplotlib.colors as cls
import matplotlib.cm as cm
norm = cls.Normalize() # Norm to map the z values to [0,1]
norm.autoscale(z)
cmap = cm.ScalarMappable(norm, 'jet') # Choose any colormap you want
ax.bar3d(x_pos, y_pos, z_pos, dx, dy, dz, color=cmap.to_rgba(z))
我的数据集看起来像(包含 ~25 个数据点):
x=[2.225 2.325 2.425 2.075 2.375 1.925 1.975 1.775 1.975 2.375]
y=[147.75 130.25 161.75 147.75 165.25 151.25 158.25 151.25 172.25 123.25]
z=[-1.36, -0.401, -0.741, -0.623, -0.44, -0.37, 0.120, 2.8, 0.026, -1.19]
我正在尝试使用数据绘制 3D 条形图。
我正在尝试:
from mpl_toolkits.mplot3d import Axes3D
fig_3d_bar = plt.figure(figsize=(7, 5))
dx = fig_3d_bar.add_subplot(111, projection='3d')
x_pos = np.array(x)
y_pos = np.array(y)
z_pos = np.zeros(len(x))
dx = np.ones(len(x))
dy = np.ones(len(y))
dz = z
dx.bar3d(x_pos, y_pos, z_pos, dx, dy, dz, color='#00ceaa')
但这给我一个错误报告:
dx.bar3d(x_pos, y_pos, z_pos, dx, dy, dz, color='#00ceaa')
AttributeError: 'numpy.ndarray' object has no attribute 'bar3d'
一点帮助就大有帮助。不知道怎么回事。
谢谢。
您的代码有错误。您对 Axes
对象和条形大小都使用变量名称 dx
。我猜你想要
ax = fig_3d_bar.add_subplot(111, projection='3d')
ax.bar3d(x_pos, y_pos, z_pos, dx, dy, dz, color='#00ceaa')
条形尺寸
由于 x 数据和 y 数据的比例不同,条形图在图中显得很宽。您可以通过相应地缩放 dx
和 dy
来调整它们。
dx = np.ones(len(x))*0.1
dy = np.ones(len(y))*5
条形颜色
条形的颜色可以通过使用 ScalarMappable instance. For this you need a norm object that scales the z-values to the range [0,1]. You can choose any of the predefined colormaps 或创建您自己的颜色来适应 z 值。
import matplotlib.colors as cls
import matplotlib.cm as cm
norm = cls.Normalize() # Norm to map the z values to [0,1]
norm.autoscale(z)
cmap = cm.ScalarMappable(norm, 'jet') # Choose any colormap you want
ax.bar3d(x_pos, y_pos, z_pos, dx, dy, dz, color=cmap.to_rgba(z))