matplotlib将不接受pyplot.bar()的一维数组

时间:2019-07-17 19:35:04

标签: python matplotlib

我首先在Jupyter Notebook中编写了此代码,并且工作正常。我想解决我遇到的一些交互性问题,因此我在PyCharm中运行了代码。我不断收到

  

TypeError:只有大小为1的数组可以转换为Python标量。

从我发现的结果来看,即使第二维为1,多维数组也不能用于pyplot中的条形图。但是,当我打印出数组的形状时,它们对所有人似乎都显示是一维的。此外,来自追溯的问题似乎是我传递的width参数,但我只是传递了整数1。

我试图通过遍历数据框中的列来创建堆积的条形图。第一列有效,但第二列引发错误。

Python 3.7, matplotlib 3.1.1, pandas 0.24.2, numpy 1.16.4, jupyter 1.0.0, notebook 5.7.8

我尝试使用.values.values.tolist()转换所有不同的数组。

def plot_conference_pct_of_ten_year_total(conf_champs):
    new_cols = [col for col in conf_champs if col != 'Total' and col[-6:] != '_count']
    new_df = conf_champs[new_cols]
    print(new_df.head())
    # new_df.to_csv('20190716_temp.csv')
    order = new_df.idxmax(axis=1).unique()
    for col in new_df:
        if col not in order[:]:
            order = np.append(order, col)
    years = conf_champs.index
    total = np.empty(len(years))
    total[:] = 0.0
    plt.figure(figsize=(10, 5))
    plt.title('Pct of Claimed National Championships by Current Conference, prev 10 years')
    bar_colors = get_bar_colors()
    n = 0
    for conf in order:
        print('years.shape: {}'.format(years.shape))
        print('conf_champs[conf].shape: {}'.format(conf_champs[conf].shape))
        print('total.shape: {}'.format(total.shape))
        print('conf: {}'.format(conf))
        print('bar_colors[n]: {}'.format(bar_colors[n]))
        plt.bar(years, conf_champs[conf], bottom=total, label=conf, width=1, color=bar_colors[n])
        total += conf_champs[conf]
        n += 1
    plt.xlim(years.min(), years.max())
    format_axis_as_pct(plt, 'y')
    plt.legend()
    return plt

调试打印和错误消息:

           Ivy   Big Ten  ACC  SEC  Pac-12  Independent  Big 12  Other
1869  0.500000  0.500000  0.0  0.0     0.0          0.0     0.0    0.0
1870  0.666667  0.333333  0.0  0.0     0.0          0.0     0.0    0.0
1872  0.800000  0.200000  0.0  0.0     0.0          0.0     0.0    0.0
1873  0.833333  0.166667  0.0  0.0     0.0          0.0     0.0    0.0
1874  0.875000  0.125000  0.0  0.0     0.0          0.0     0.0    0.0

years.shape: (149,)
conf_champs[conf].shape: (149,)
total.shape: (149,)
conf: Ivy
bar_colors[n]: red
years.shape: (149,)
conf_champs[conf].shape: (149,)
total.shape: (149,)
conf: Pac-12
bar_colors[n]: chartreuse
Traceback (most recent call last):
  File "C:/Users/[User]/.PyCharmCE2019.1/config/scratches/scratch_7.py", line 277, in <module>
    plt = plot_conference_pct_of_ten_year_total(conf_champs)
  File "C:/Users/[User]/.PyCharmCE2019.1/config/scratches/scratch_7.py", line 243, in plot_conference_pct_of_ten_year_total
    plt.bar(years, conf_champs[conf], bottom=total, label=conf, width=1, color=bar_colors[n])
  File "C:\Users\[User]\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\pyplot.py", line 2440, in bar
    **({"data": data} if data is not None else {}), **kwargs)
  File "C:\Users\[User]\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\__init__.py", line 1601, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "C:\Users\[User]\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axes\_axes.py", line 2430, in bar
    label='_nolegend_',
  File "C:\Users\[User]\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\patches.py", line 707, in __init__
    Patch.__init__(self, **kwargs)
  File "C:\Users\[User]\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\patches.py", line 89, in __init__
    self.set_linewidth(linewidth)
  File "C:\Users\[User]\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\patches.py", line 368, in set_linewidth
    self._linewidth = float(w)
TypeError: only size-1 arrays can be converted to Python scalars

Process finished with exit code 1

0 个答案:

没有答案