ValueError:num 必须为 1 <= num <= 2,而不是 3

2024-02-26

我有以下内容dataframe我使用生成的pivot_table:

我正在使用以下代码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()

我期待如下的输出:

但这段代码给了我以下错误:

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


请注意,您仅生成两个子图:

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

第一个参数是每行中的图数,第二个参数是每列中的图数(另请参见matplotlib.pyplot.subplot 文档 http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.subplot)。因此,您的案例中可用的绘图总数为:1*2 = 2。如果你想创建 25 个,你可以使用:

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

每行 5 个图和每列 5 个图加起来总共5*5 = 25

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ValueError:num 必须为 1 <= num <= 2,而不是 3 的相关文章

随机推荐