在Matplotlib的3d条形图问题

时间:2012-01-25 20:54:16

标签: python 3d matplotlib bar-chart

当我制作一个包含4个或更多值的3d条形图时,图形看起来是正确的但是当我用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()

生成这个:

Triangular bar

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

Correct bar

2 个答案:

答案 0 :(得分:1)

这几乎肯定是一个错误。尝试使用示例代码可以得到所需的矩形条结果,包含任意数量的点(使用1,2,3,5,15测试): Example of code working as desired for 3 points

我在linux上运行matplotlib版本1.1.1rc。如果可以,请尝试更新到最新版本。注意

import matplotlib
print matplotlib.__version__

会告诉您正在使用的版本。

答案 1 :(得分:0)

这是因为你只给它3点积分。只需将代码更改为以下内容即可。

indexs = np.arange(4)  # not 3
heights = np.random.rand(4) # not 3