柱状图
import matplotlib.pyplot as plt
# # 画图柱状图
shunxu=["introduction","related work","method","evaluation and result","conclusion"]
df = pd.read_csv('.csv', index_col='year', parse_dates=True,usecols=['paper_id', 'year', 'label', 'count'], encoding='utf-8')
grouped_data = df.groupby(['label'])['count'].mean()
# 设置画布大小为10英寸宽、8英寸高
plt.figure(figsize=(8, 8))
# 绘制柱状图
ax = grouped_data.plot(kind='bar')
# 按照自定义顺序设置横坐标的标签
ax.set_xticklabels(shunxu)#亲测不能放到ax里面只能这样单独拿出来
ax.set_xticks(range(len(grouped_data)))
ax.legend()
plt.ylabel("numbers")
plt.xticks(rotation=0)
plt.title("The distribution of entity counts by chapter structure",fontsize=16,fontweight='bold')
plt.show()
子图——分
import matplotlib.pyplot as plt
fig=plt.figure()#确定大的画布
colors = [plt.cm.Set2(0), plt.cm.Set2(1), plt.cm.Set2(2), plt.cm.Set2(3), plt.cm.Set2(4)]
selected_years = [x for x in range(1980, 2016) if x % 5 == 0]
df.index = df.index.to_period('Y')
# 绘制a图
ax1=fig.add_subplot(211)#第一个数字是表示有多少行,第二个是多少列,第三个是第几个图
data = df[df.index.year.isin(selected_years)].groupby(['year', 'label'])['count'].mean().unstack()
data = data[shunxu]
data.plot(
kind='bar',
figsize=(10, 5),
color=colors,
ax=ax1,
legend=False
)
ax1.set_xticklabels(ax1.get_xticklabels(), rotation=0)
ax1.set_title('(a)')
# # 绘制b图
ax2=fig.add_subplot(212)
#需要注意的是折线图会自己填充缺失值
for i ,j in enumerate(shunxu):
filtered_fb = df[df["label"] == j]
# if not filtered_fb.empty:
df[df['label'] == j].groupby(["year"])['count'].mean().plot(
kind='line',
figsize=(10, 5),
color=colors[i],
label=j,
ax=ax2,
# drawstyle='steps-pre',#设置是否填充缺失值
linewidth=2
)
ax2.set_title('(b)')
plt.subplots_adjust(hspace=0.4)
fig.suptitle('Entity Distribution Time Series', fontsize=16,fontweight='bold')
fig.tight_layout()
fig.legend(shunxu)#标签在大图上
plt.show()
组柱状图+时间序列折线图