ValueError:num必须为1<=num<=90,而不是91

2024-05-13 19:27:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用print(df_eda.head(20))生成了以下数据帧

enter image description here

我正在使用下面的代码绘制第1天到第90天之间的数据

# Day 1 ~ 90
fig = plt.figure(figsize = (15, 40))
for day in range(1,91):
    ax = plt.subplot(18, 5, day)
    price = df_eda.loc[df_eda.day == day, 'price'].values
    mean = price.mean().round(3)
    std = price.std().round(3)
    skew = (3*(mean - np.median(price))/price.std()).round(3)
    if skew >= 1.5:
        plt.hist(price, alpha = 0.7, bins = 50, color = 'red')
    elif skew <= -1.5:
        plt.hist(price, alpha = 0.7, bins = 50, color = 'blue')
    else:
        plt.hist(price, alpha = 0.7, bins = 50, color = 'gray')
    plt.title(f'day{day}')
    plt.xticks([])
    plt.yticks([])
    plt.xlabel('')
    plt.ylabel('')
    plt.text(0.25, 0.9, f'mean : {mean}',  ha='left', va='center', transform=ax.transAxes)
    plt.text(0.25, 0.8, f'std : {std}',  ha='left', va='center', transform=ax.transAxes)
    plt.text(0.25, 0.7, f'skew : {skew}',  ha='left', va='center', transform=ax.transAxes)

enter image description here

现在,我想使用下面的代码通过在范围()内更改开始/结束值来绘制第91天到第181天之间的数据

# Day 91~180
fig = plt.figure(figsize = (15, 40))
for day in range(91,181):
    ax = plt.subplot(18, 5, day)
    price = df_eda.loc[df_eda.day == day, 'price'].values
    mean = price.mean().round(3)
    std = price.std().round(3)
    skew = (3*(mean - np.median(price))/price.std()).round(3)
    if skew >= 1.5:
        plt.hist(price, alpha = 0.7, bins = 50, color = 'red')
    elif skew <= -1.5:
        plt.hist(price, alpha = 0.7, bins = 50, color = 'blue')
    else:
        plt.hist(price, alpha = 0.7, bins = 50, color = 'gray')
    plt.title(f'day{day}')
    plt.xticks([])
    plt.yticks([])
    plt.xlabel('')
    plt.ylabel('')
    plt.text(0.25, 0.9, f'mean : {mean}',  ha='left', va='center', transform=ax.transAxes)
    plt.text(0.25, 0.8, f'std : {std}',  ha='left', va='center', transform=ax.transAxes)
    plt.text(0.25, 0.7, f'skew : {skew}',  ha='left', va='center', transform=ax.transAxes)

这段代码给出了以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-247-a5208b4f991c> in <module>
      2 fig = plt.figure(figsize = (15, 40))
      3 for day in range(91,181):
----> 4     ax = plt.subplot(18, 5, day)
      5     price = df_eda.loc[df_eda.day == day, 'price'].values
      6     mean = price.mean().round(3)

c:\users\10290313\appdata\local\programs\python\python36\lib\site-packages\matplotlib\pyplot.py in subplot(*args, **kwargs)
   1140 
   1141     fig = gcf()
-> 1142     ax = fig.add_subplot(*args, **kwargs)
   1143     bbox = ax.bbox
   1144     axes_to_delete = []

c:\users\10290313\appdata\local\programs\python\python36\lib\site-packages\matplotlib\figure.py in add_subplot(self, *args, **kwargs)
   1400                     # more similar to add_axes.
   1401                     self._axstack.remove(ax)
-> 1402             ax = subplot_class_factory(projection_class)(self, *args, **kwargs)
   1403 
   1404         return self._add_axes_internal(key, ax)

c:\users\10290313\appdata\local\programs\python\python36\lib\site-packages\matplotlib\axes\_subplots.py in __init__(self, fig, *args, **kwargs)
     37 
     38         self.figure = fig
---> 39         self._subplotspec = SubplotSpec._from_subplot_args(fig, args)
     40         self.update_params()
     41         # _axes_class is set in the subplot_class_factory

c:\users\10290313\appdata\local\programs\python\python36\lib\site-packages\matplotlib\gridspec.py in _from_subplot_args(figure, args)
    688                 if num < 1 or num > rows*cols:
    689                     raise ValueError(
--> 690                         f"num must be 1 <= num <= {rows*cols}, not {num}")
    691                 return gs[num - 1]   # -1 due to MATLAB indexing.
    692         else:

ValueError: num must be 1 <= num <= 90, not 91

请注意,我想在for循环中使用day,循环需要从第91天开始 非常感谢你的帮助


Tags: inselfdffigargspltaxmean