2016-06-13 142 views
5

我有產生使用pivot_table以下dataframeValueError異常:NUM必須爲1 <= NUM​​ <= 2,而不是3

enter image description here

和我使用以下代碼來boxplot多個列:

fig = plt.figure() 
for i in range(0,25): 
    ax = plt.subplot(1,2,i+1) 
    toPlot1.boxplot(column='Score',by=toPlot1.columns[i+1],ax=ax) 
fig.suptitle('test title', fontsize=20) 
plt.show() 

我期待像下面的輸出:

enter image description here

但這個代碼給我下面的錯誤:

--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-275-9c68ce91596f> in <module>() 
     1 fig = plt.figure() 
     2 for i in range(0,25): 
----> 3  ax = plt.subplot(1,2,i+1) 
     4  toPlot1.boxplot(column='Score',by=toPlot1.columns[i+1],ax=ax) 
     5 fig.suptitle('test title', fontsize=20) 

E:\Anaconda2\lib\site-packages\matplotlib\pyplot.pyc in subplot(*args, **kwargs) 
    1020 
    1021  fig = gcf() 
-> 1022  a = fig.add_subplot(*args, **kwargs) 
    1023  bbox = a.bbox 
    1024  byebye = [] 

E:\Anaconda2\lib\site-packages\matplotlib\figure.pyc in add_subplot(self, *args, **kwargs) 
    1003      self._axstack.remove(ax) 
    1004 
-> 1005    a = subplot_class_factory(projection_class)(self, *args, **kwargs) 
    1006 
    1007   self._axstack.add(key, a) 

E:\Anaconda2\lib\site-packages\matplotlib\axes\_subplots.pyc in __init__(self, fig, *args, **kwargs) 
    62      raise ValueError(
    63       "num must be 1 <= num <= {maxn}, not {num}".format(
---> 64        maxn=rows*cols, num=num)) 
    65     self._subplotspec = GridSpec(rows, cols)[int(num) - 1] 
    66     # num - 1 for converting from MATLAB to python indexing 

ValueError: num must be 1 <= num <= 2, not 3 

我相信這是因爲只能有一個圖表2個箱線圖?

關於如何解決這個問題的任何想法?任何指針將不勝感激。

TIA。

回答

10

請注意,您只生成兩個次要情節:

ax = plt.subplot(1,2,i+1) 

第一個參數是每一行地塊的數量和每列地塊的第二數量(見the matplotlib.pyplot.subplot documentation)。因此,您的案例中可用的地塊總數爲:1*2 = 2。如果你想創建25例如,你可以使用:每行

ax = plt.subplot(5,5,i+1) 

5地塊和5%列添加到5*5 = 25

+2

感謝總數那麼多。從我的結局來看,這非常愚蠢。注意自己:在互聯網上覆制粘貼代碼前正確理解參數。 – Patthebug

相關問題