简体   繁体   English

Pandas Python 可视化 - 值错误:形状不匹配:错误

[英]Pandas Python Visualization - ValueError: shape mismatch: ERROR

*Edit: Why the right plot (Bar) is showing 50%, half black screen on the plot, wierd numbers, "garbage"... how to fix the right plot? *编辑:为什么正确的 plot(条形图)在 plot 上显示 50%,半黑屏,奇怪的数字,“垃圾”......如何修复正确的 Z32FA6E1B78A9D4028953E6055A2AA4?

here is my code:这是我的代码:

top_series = all_data.head(50).groupby('Top Rated ')['Top Rated '].count()
top_values = top_series.values.tolist()
top_index = ['Top Rated', 'Not Top Rated']
top_colors = ['#27AE60', '#E74C3C']


rating_series = all_data.head(50).groupby('Rating')['Rating'].count()
rating_values = rating_series.values.tolist()
rating_index = ['High' , 'Low']
rating_colors = ['#F1C40F', '#27AE60']

fig, axs = plt.subplots(1,2, figsize=(16,5))
axs[0].pie(top_values, labels=top_index, autopct='%1.1f%%', shadow=True, startangle=90,
           explode=(0.05, 0.05), radius=1.5, colors=top_colors, textprops={'fontsize':15})
    
axs[1].bar(rating_series.index, rating_series.values, color='b')
axs[1].set_xlabel('Rating')
axs[1].set_ylabel('Amount')


fig.suptitle('Does "Rating" really affect on Top Sellers ? ')

CSV cols: CSV 列: 在此处输入图像描述

Output (look at the right plot): Output(看右图):

在此处输入图像描述

I suppose, that keys is a list of all keys.我想,那个keys是所有键的列表。 So it can have a different shape than the top_values .因此它可以具有与top_values不同的形状。 If you would do:如果你愿意:

axs[1].bar(top_series.index, top_series.values, color='b')

It should work well.它应该运作良好。

But, if you just want to plot the histogram, there is even shorter version, without temporary objects:但是,如果你只想 plot 直方图,还有更短的版本,没有临时对象:

all_data['Top Rated '].value_counts().plot(kind = 'bar', ax=axs[1])

Edit: The Rating column is a numeric one, not a string one.编辑: Rating列是一个数字,而不是一个字符串。 You have to create a column which will have values High and Low .您必须创建一个具有值HighLow的列。 For example:例如:

all_data['Rating_Cat'] = all_data['Rating'].apply(lambda x : 'High' if (x > 10000000 ) else 'Low')

And then use this column to plot this kind of bar plot然后用本栏到 plot 这种栏 plot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM