PyPlot栏崩溃,Log = True

时间:2013-09-27 09:09:04

标签: python matplotlib

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

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'),但随后酒吧消失了。我不知道如何在这么简单的情况下使用日志参数错误。

1 个答案:

答案 0 :(得分:4)

这似乎是您的matplotlib版本中的错误。虽然bottom的参数plt.bar的默认值声称为0,但添加额外的关键字参数设置bottom=None,随后会导致使用bottom进行算术运算失败。

作为一种解决方法,您可以将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()