欢迎来到RankFan的Blogs

扩大
缩小

多子图绘制

matplotlib图形整合之多个子图一起绘制

plt.subplot(211)
plt.subplot(223)
plt.subplot(224)

REF

df_check_Hpd.DataFrame, index 为时间,如果不是需要用 pd.to_datetime() 转化一下。

fig = plt.figure(figsize=(10, 16),)  #  layout="constrained", 使得图片更加紧凑
# fig.tight_layout()
col_i = 0
col_j = 0
col_num = 2
len_fig = int(df_check_H.shape[1]/2)
plt.suptitle(' $|$ Condition Variance ($H_t$)',
             fontsize=14,
             # fontweight='medium',
             weight='bold')  # 共同 title
plt.subplots_adjust(left=0.125, right=0.9, top=0.935, bottom=0.110)
for i_ in range(1, len_fig + 1):
    col_j += col_num
    plt.subplot(int(len_fig/col_num), col_num, i_)
    plt.plot(df_check_H.index, df_check_H.loc[:, cols_H[col_i]],
             linewidth=1, linestyle='solid', color='#6495ED',
             label=cols_H[col_i])
    plt.plot(df_check_H.index, df_check_H.loc[:, cols_H[col_i+1]],
             linewidth=1, linestyle='solid', color='#DC143C',
             label=cols_H[col_i+1])

    # plt.plot(df_check_H.index, df_check_H.loc[:, cols_H[col_i:col_j]],
    #          linewidth=1, linestyle='solid',
    #          label=cols_H[col_i:col_j])

    plt.autoscale(enable=True, axis='x', tight=False)
    plt.tick_params(axis='both', which='major', labelsize=9)
    # plt.axhline(linewidth=0.5, color='black')
    plt.legend(fontsize='medium', frameon=False, prop={'style': 'italic', 'weight': 'bold'})  # fontsize 'medium', frameon=False 不显示边框
    col_i += col_num
plt.show(block=True)

ax 绘制

plt.subplot(2, 2, 1)
plt.plot(df_check_H.index, df_check_H.loc[:, cols_H[:2]], label=cols_H[:2])
plt.legend()

plt.subplot(2, 2, 2)
plt.plot(df_check_H.index, df_check_H.loc[:, cols_H[2:4]], label=cols_H[2:4])
plt.legend()

plt.subplot(2, 2, 3)
plt.plot(df_check_H.index, df_check_H.loc[:, cols_H[4:6]], label=cols_H[4:6])
plt.legend()

plt.subplot(2, 2, 4)
plt.plot(df_check_H.index, df_check_H.loc[:, cols_H[6:8]], label=cols_H[6:8])
plt.legend()

posted on 2022-10-21 16:26  RankFan  阅读(29)  评论(0编辑  收藏  举报

导航