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

如何调整Seaborn折线图的X轴标签使其完整显示?

解决Seaborn折线图X轴标签显示不全的问题

以下是几种实用的解决办法,直接套用就能解决标签被截断或重叠的问题:

方法1:旋转X轴标签并调整布局

这是最常用的方式,通过旋转标签角度,再让图表自动调整布局,避免标签被边缘截断:

import matplotlib.pyplot as plt
import seaborn as sns

sns.lineplot(x="ds", y="y", data=df_new, hue="Type", marker="o")
# 旋转标签45度,设置右对齐让排版更整齐
plt.xticks(rotation=45, ha='right')
# 自动调整图表元素间距,防止标签被画布边缘截断
plt.tight_layout()
plt.show()

方法2:扩大图表画布尺寸

如果标签本身很长,直接给图表分配更大的空间,从根源上解决拥挤问题:

import matplotlib.pyplot as plt
import seaborn as sns

# 创建宽12、高6的画布(尺寸可根据需求调整)
plt.figure(figsize=(12, 6))
sns.lineplot(x="ds", y="y", data=df_new, hue="Type", marker="o")
plt.tight_layout()
plt.show()

方法3:缩小X轴标签字体

通过减小标签字号,让更多标签能在现有空间里完整显示:

import matplotlib.pyplot as plt
import seaborn as sns

sns.lineplot(x="ds", y="y", data=df_new, hue="Type", marker="o")
# 将标签字号设为8(可根据实际情况调整)
plt.xticks(fontsize=8)
plt.tight_layout()
plt.show()

内容的提问来源于stack exchange,提问作者Gabriel Angelo Branco

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 05:50:26