2016-06-13 29 views
5

Ho la seguente dataframe che ho generato usando pivot_table:ValueError: num deve essere 1 <= num <= 2, non 3

enter image description here

e sto usando il seguente codice per boxplot multiplo colonne:

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() 

mi aspettavo un output simile al seguente:

enter image description here

Ma questo codice mi dà il seguente errore:

--------------------------------------------------------------------------- 
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 

Credo che sia perché non ci può essere solo 2 grafici a scatole su un grafico?

Qualche idea su come risolvere questo problema? Qualsiasi suggerimento sarebbe molto apprezzato.

TIA.

risposta

10

Nota che si genera solo due sottotrame:

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

Il primo argomento è il numero di trame in ogni riga e la seconda il numero di trame per colonna (vedi anche the matplotlib.pyplot.subplot documentation). Quindi il numero totale di grafici disponibili nel tuo caso è: 1*2 = 2. Se si desidera creare 25 si potrebbe, ad esempio l'uso:

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

5 trame per fila e 5 per ogni colonna di aggiungere un numero totale di 5*5 = 25

+2

Grazie mille. E 'stato molto sciocco dalla mia fine. Nota per sé: capire i parametri correttamente prima di copiare il codice di incollaggio disponibile su Internet. – Patthebug