2013-08-02 69 views
4

我意识到这个问题之前已被问到(Python Pyplot Bar Plot bars disappear when using log scale),但给出的答案不适用于我。我把我的pyplot.bar(x_values,y_values等,登录= TRUE),但得到一个错误,指出:Barplot与日志y轴程序语法与matplotlib pyplot

"TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'" 

我白白一直在寻找使用柱状图与pyplot代码的实际例子y轴设置为日志但没有找到它。我究竟做错了什么?

这里是代码:

import matplotlib.pyplot as pyplot 
ax = fig.add_subplot(111) 
fig = pyplot.figure() 
x_axis = [0, 1, 2, 3, 4, 5] 
y_axis = [334, 350, 385, 40000.0, 167000.0, 1590000.0] 
ax.bar(x_axis, y_axis, log = 1) 
pyplot.show() 

我得到一个错误,甚至当我removre pyplot.show。在此先感谢帮助

+1

显示使用_full_回溯请 – tacaswell

回答

1

,误差值在ax.bar(...由于调至log = True声明。我不确定这是一个matplotlib错误还是以无意的方式使用它。通过删除有问题的参数log=True可以很容易地解决这个问题。

这可以简单地通过简单地记录y值自己来弥补。

x_values = np.arange(1,8, 1) 
y_values = np.exp(x_values) 

log_y_values = np.log(y_values) 

fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.bar(x_values,log_y_values) #Insert log=True argument to reproduce error 

需要添加适当的标签log(y)要清楚它是日志值。

+0

OP希望y轴上的对数比例不是x轴。 – tacaswell

+0

问题是当我将y_axis设置为日志时,没有填充横条 – Justin

+0

我已经在编辑中解决了这些问题。我承认它不如记录x轴那么漂亮,但错误来自'log = True'参数仍然是真的。 – Greg

7

您确定这是您的所有代码吗?代码在哪里抛出错误?在绘图过程中?因为这个工作对我来说:

In [16]: import numpy as np 
In [17]: x = np.arange(1,8, 1) 
In [18]: y = np.exp(x) 

In [20]: import matplotlib.pyplot as plt 
In [21]: fig = plt.figure() 
In [22]: ax = fig.add_subplot(111) 
In [24]: ax.bar(x, y, log=1) 
Out[24]: 
[<matplotlib.patches.Rectangle object at 0x3cb1550>, 
<matplotlib.patches.Rectangle object at 0x40598d0>, 
<matplotlib.patches.Rectangle object at 0x4059d10>, 
<matplotlib.patches.Rectangle object at 0x40681d0>, 
<matplotlib.patches.Rectangle object at 0x4068650>, 
<matplotlib.patches.Rectangle object at 0x4068ad0>, 
<matplotlib.patches.Rectangle object at 0x4068f50>] 
In [25]: plt.show() 

这里的情节 enter image description here

+0

的代码抛出在错误当它达到ax.bar(X,Y,日志= 1)。由于某种原因,它仍然不能正常工作 – Justin

3

正如格雷格答案的评论中已经建议的那样,通过将默认行为设置为“剪辑”,您的确看到一个问题,即fixed in matplotlib 1.3。升级到1.3可以解决这个问题。

请注意,您应用日志比例的方式似乎并不重要,无论是作为关于轴的barset_yscale的关键字参数。

参见this answer to "Logarithmic y-axis bins in python"启示该解决方法:

plt.yscale('log', nonposy='clip') 
+0

thx,救了我一天 – wuppi