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

Python matplotlib+pandas读取CSV绘图时无法显示全部x轴标签

pandas绘制CSV数据图表显示全部x轴标签方法

pandas默认调用matplotlib绘图时会自动抽样显示x轴刻度,哪怕分类总数不多也可能出现标签漏显,手动指定x轴刻度配置即可展示全部标签,修改后的可直接运行代码如下:

import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

pathcsv = r'D:\iPython\csvfile\samplecsv2.csv'
df = pd.read_csv(pathcsv)
# 获取绘图生成的坐标轴实例
ax = df.set_index('Names').plot()
# 强制配置x轴显示全部标签:参数依次为刻度位置、标签文本、标签旋转角度
ax.set_xticks(range(len(df['Names'])), labels=df['Names'], rotation=0)

plt.show()
  • 可选调整:如果x轴标签文字较长出现重叠遮挡,把rotation参数修改为30或者45,让标签倾斜显示即可提升可读性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 04:45:38