Matplotlib中的3D柱状图问题

7

当我用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
这看起来像是一个可能的错误。 - Thomas K
1
用3个你可以画三角形,但用2个则无法画出来...适用于Windows 7和Matplotlib 1.1.0。 - joaquin
有人知道在哪里可以报告matplotlib的错误吗? - user1170056
2
请在此处查看“报告问题”:http://matplotlib.sourceforge.net/faq/troubleshooting_faq.html - Scott B
2个回答

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__

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


代码在Python 2.7.3/Matplotlib 1.1.1上的OS X 10.7.4上也能正常工作,因此这很可能是一个错误。 - Tim

0
这是因为您只提供了3个点来绘制。只需更改代码如下,它就应该可以工作了。
indexs = np.arange(4)  # not 3
heights = np.random.rand(4) # not 3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接