use*_*056 7 python 3d matplotlib bar-chart
当我制作一个包含4个或更多值的3d条形图时,图形看起来是正确的,但当II尝试3时,条形变为三角形,发生了什么?
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
color_grade_classes = ['#80FF00','#FFFF00','#FF8000', '#FF0000']
for colors, rows in zip(color_grade_classes, [3,2,1,0] ):
indexs = np.arange(3)
heights = np.random.rand(3)
print rows, indexs, heights, colors
ax.bar(indexs, heights, zs = rows, zdir='y', color=colors, alpha=0.8)
ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.show()
Run Code Online (Sandbox Code Playgroud)
生成这个:

但当我将索引和高度的数量增加到5时,我得到了这个:

这是因为你只给它 3 个点来绘制。只需将代码更改为以下内容即可。
indexs = np.arange(4) # not 3
heights = np.random.rand(4) # not 3
Run Code Online (Sandbox Code Playgroud)