You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matplotlib双图绘制中折线图标题不显示问题求助

解决折线图标题不显示的问题

问题出在你给折线图设置标题的代码逻辑上:你调用了ax1.set_label('Linediagramm für Ländern'),但set_label()并非设置子图标题的方法——它的作用是给坐标轴或绘图元素设置图例标签。要给子图添加顶部标题,正确的方法是使用ax1.set_title()。

修改后的完整代码

import matplotlib.pyplot as plt
# 假设new_df已提前定义

fig = plt.figure()
ax0 = fig.add_subplot(1,2,1)
ax1 = fig.add_subplot(1,2,2)

new_df.iloc[:,0:3].plot(
    kind='box',
    figsize=(20,7),
    color='red',
    ax=ax0
)

ax0.set_title('Boxdiagramm für Ländern')
ax0.set_ylabel('Nummern')
ax0.set_xlabel('Jahre')

new_df['Total'].plot(
    kind='line',
    figsize=(20,7),
    ax=ax1
)

# 关键修改:替换set_label为set_title
ax1.set_title('Linediagramm für Ländern')
ax1.set_ylabel('Nummern')
ax1.set_xlabel('Ländern')
ax1.legend(labels=None)

plt.show()

补充说明

  • set_title():Matplotlib中专门用于设置子图顶部标题的方法
  • set_label():多用于给坐标轴(x/y轴)或绘图元素(如折线、柱状条)设置标签,用于生成图例时关联标识

内容的提问来源于stack exchange,提问作者eren9677

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 08:10:39