【发布时间】:2020-10-16 04:53:29
【问题描述】:
数据来自本网站。 https://www.kaggle.com/kemical/kickstarter-projects
我的堆积条形图已断开连接。我不知道发生了什么。我的数据都不包含任何空值。该系列的值是频率。有没有人遇到过这个?我只是想让我的酒吧连接起来。
fig, ax = plt.subplots(nrows=1, figsize=(15,5))
x = clean_df['main_category'].value_counts().index
print("Number of unique main categories:", clean_df['main_category'].nunique())
for year in [2010, 2011, 2012, 2013, 2014, 2015, 2016]:
y = clean_df[clean_df['launched'].dt.year == year]['main_category'].value_counts()
if year > 2010:
bottom = clean_df[clean_df['launched'].dt.year <= year-1]['main_category'].value_counts()
else:
bottom = 0
ax.set_xlabel("Main Catagories", fontsize=14)
ax.set_ylabel("Frequency/Count", fontsize=14)
ax.bar(x=x, height=y, width=0.9, bottom=bottom, label=str(year))
ax.yaxis.grid(linestyle='-', linewidth=0.7)
ax.set_xticklabels(x, rotation=45, ha='right')
ax.legend(loc='upper right')
plt.tight_layout();
【问题讨论】:
-
每个标签在情节中都有相同的年份(2016)
-
为什么不用pandas来做堆积条形图呢?似乎会容易得多。
-
问题可能是
clean_df[...]['main_category'].value_counts()在每次调用中改变了类别的顺序(顺序似乎是从大到小值计数,每年不同)。因此,标签与条形不对应。如果您设置 alpha=0.4 左右,您还会看到条形重叠。按照 BigBen 的建议,使用 pandas 或 seaborn 来创建情节会更好。
标签: python pandas matplotlib bar-chart