matplotlib draws sub-pictures, adjusts the spacing of sub-pictures, sets sub-pictures, and big picture titles

plt.tight_layout()Automatically adjust the sub-picture spacing
Before
Insert picture description here
adjustment : After adjustment

fig = plt.figure(figsize=(12,5))
for i in range(5):
    ax = fig.add_subplot(5, 1, i+1)                      # 3行3列的第一个位置
    word_pro = lda.get_topic_terms(i, topn=10000)
    word_pro_x = list(map(lambda x: x[0],word_pro))
    word_pro_y = list(map(lambda x: x[1],word_pro))
    ax.scatter(word_pro_x, word_pro_y,s=10)
    plt.ylim([0,0.08]); plt.xlim(0,3300)
    ax.set_title(f'topic{i+1}')                          # 设置小图标题
plt.tight_layout()                                       # 自动调整子图间距
plt.xlabel('number of word')                             # 不设置总标题
fig.suptitle(None)                                       # 设置大图标题

Insert picture description here
Set sub-picture title: ax.set_title()Set the overall picture title:fig.suptitle

fig.add_subplot(1, 3, 1).set_ylabel(r'$Score_{nlp}$') Specify a subgraph to set the total x-axis label title

# 不同星级的情感评分  
fig = plt.figure(dpi=150)  # dpi=150
for n, df in enumerate([df_m,df_p,df_h]):
    ax = fig.add_subplot(1, 3, n+1)                      # 3行3列的第一个位置
    df.boxplot(column='final_score',by=['star_rating'],ax=ax)
    ax.set_title(f'{df.index.name}')                          # 不设置子标题
    ax.set_xlabel(None)
    ax.set_xlabel(' Star rating ')
fig.add_subplot(1, 3, 1).set_ylabel(r'$Score_{nlp}$')    # 单独设置某个
plt.tight_layout()                                       # 自动调整子图间距
fig.suptitle(None)                                            # 不设置总标题

Insert picture description here

Guess you like

Origin blog.csdn.net/Caiqiudan/article/details/111401883