pandas ValueError: num 必须是 1 <= num <= 2,而不是 3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37799865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ValueError: num must be 1 <= num <= 2, not 3
提问by Patthebug
I have the following dataframe
that I generated using pivot_table
:
我使用以下方法dataframe
生成了以下内容pivot_table
:
and I'm using the following code to boxplot
the multiple columns:
我将以下代码用于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()
I was expecting an output like the following:
我期待如下输出:
But this code gives me the following error:
但是这段代码给了我以下错误:
---------------------------------------------------------------------------
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
I believe it's because there can only be 2 boxplots on one graph?
我相信这是因为一张图上只能有 2 个箱线图?
Any idea on how to fix this? Any pointers would be highly appreciated.
关于如何解决这个问题的任何想法?任何指针将不胜感激。
TIA.
TIA。
回答by MSeifert
Note that you only generate two subplots:
请注意,您只生成两个子图:
ax = plt.subplot(1,2,i+1)
The first argument is the number of plots in each row and the second the number of plots per column (see also the matplotlib.pyplot.subplot documentation). So the total number of plots avaiable in your case is: 1*2 = 2
. If you want to create 25 you could for example use:
第一个参数是每行的绘图数,第二个参数是每列的绘图数(另请参阅matplotlib.pyplot.subplot 文档)。因此,地块的总数量,你的情况是缴费:1*2 = 2
。如果你想创建 25 你可以例如使用:
ax = plt.subplot(5,5,i+1)
5 plots per row and 5 per column add to a total number of 5*5 = 25
每行 5 个地块和每列 5 个地块添加到总数中 5*5 = 25