错误绘图:数据必须是一维的

时间:2019-07-01 15:07:49

标签: python python-3.x

我不断收到此错误:

Exception: Data must be 1-dimensional

我正在测试此代码。

prod_count = pd.DataFrame(df.groupby(['product_name'])['order_id'].count().sort_values(ascending=False).head(20))
plt.figure()
sns.barplot(prod_count.index, prod_count.values, alpha=0.8)
plt.title('Counts of Top Products Sold')
plt.ylabel('Number of Products', fontsize=12)
plt.xlabel('Products', fontsize=12)
plt.show()

我的product_count.index如下:

Index(['Banana', 'Bag of Organic Bananas', 'Organic Strawberries',
       'Organic Hass Avocado', 'Limes', 'Strawberries', 'Organic Baby Spinach',
       'Large Lemon', 'Organic Raspberries', 'Organic Garlic',
       'Organic Avocado', 'Organic Yellow Onion', 'Organic Zucchini',
       'Organic Gala Apples', 'Cucumber Kirby', 'Organic Red Onion',
       'Organic Whole Milk', '100% Whole Wheat Bread', 'Organic Cilantro',
       'Apple Honeycrisp Organic'],
      dtype='object', name='product_name')

我的prod_count.values如下:

array([[48],
       [34],
       [25],
       [23],
       [18],
       [17],
       [17],
       [17],
       [13],
       [12],
       [11],
       [11],
       [11],
       [10],
       [ 9],
       [ 9],
       [ 9],
       [ 9],
       [ 8],
       [ 8]], dtype=int64)

我不确定为什么要在进行订单计数时字段名称显示为“ order_id”,但是数据框应该是这样的。

product_name                        order_id
Banana  48
Bag of Organic Bananas  34
Organic Strawberries    25
Organic Hass Avocado    23
Limes   18
Strawberries    17
Organic Baby Spinach    17
Large Lemon 17
Organic Raspberries 13
Organic Garlic  12
Organic Avocado 11
Organic Yellow Onion    11
Organic Zucchini    11
Organic Gala Apples 10
Cucumber Kirby  9
Organic Red Onion   9
Organic Whole Milk  9
100% Whole Wheat Bread  9
Organic Cilantro    8
Apple Honeycrisp Organic    8

而且,图表应如下所示。

enter image description here 仅供参考,我在此站点上找到了绘图代码。

https://www.kaggle.com/tejainece/seaborn-barplot-and-pandas-value-counts

任何帮助将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:1)

问题是您正在尝试绘制非一维的pd.DataFrame(prod_count是一个数据框)。因此,您想从该数据框中访问“ order_id”列中的值。所以试试这个: sns.barplot(prod_count.index, prod_count['order_id'].values, alpha=0.8)