0

我正在尝试做一个简单的对数条形图:

import matplotlib.pyplot as plt
plt.bar(range(10), [4**i for i in range(10)], log=True)
plt.show()

它崩溃了:

  File "C:\Program Files (x86)\Python33\lib\site-packages\matplotlib\pyplot.py", line 2383, in bar


 ret = ax.bar(left, height, width=width, bottom=bottom, **kwargs)
  File "C:\Program Files (x86)\Python33\lib\site-packages\matplotlib\axes.py", line 4903, in bar
    self.add_patch(r)
  File "C:\Program Files (x86)\Python33\lib\site-packages\matplotlib\axes.py", line 1572, in add_patch
    self._update_patch_limits(p)
  File "C:\Program Files (x86)\Python33\lib\site-packages\matplotlib\axes.py", line 1590, in _update_patch_limits
    xys = patch.get_patch_transform().transform(vertices)
  File "C:\Program Files (x86)\Python33\lib\site-packages\matplotlib\patches.py", line 582, in get_patch_transform
    self._update_patch_transform()
  File "C:\Program Files (x86)\Python33\lib\site-packages\matplotlib\patches.py", line 578, in _update_patch_transform
    bbox = transforms.Bbox.from_bounds(x, y, width, height)
  File "C:\Program Files (x86)\Python33\lib\site-packages\matplotlib\transforms.py", line 786, in from_bounds
    return Bbox.from_extents(x0, y0, x0 + width, y0 + height)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

当我使用 log=False 时,它​​可以工作。我使用 plt.yscale('log'),但随后这些条形消失了。我不明白,在这样一个简单的情况下,我怎么能错误地使用 log 参数。

4

1 回答 1

4

这似乎是您的 matplotlib 版本中的错误。虽然声称参数的默认值为bottom,但添加一个额外的关键字参数集随后会导致算术使用失败。plt.bar0bottom=Nonebottom

作为一种解决方法,您可以bottom再次将自己设置为默认值。

import numpy as np
import matplotlib.pyplot as plt
x = np.arange(10)
plt.bar(x, 4**x, bottom=0, log=True)
plt.show()
于 2013-09-27T09:27:42.737 回答