python绘图案例

柱状图

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()

组柱状图+时间序列折线图

  • 11
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值