2013-08-02 228 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