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

使用Pandas DataFrame绘图时X轴日期显示异常(显示1970年而非2024年)

Pandas堆叠柱状图X轴日期显示异常问题解决

问题描述

我有如下DataFrame,其中date列存储的是Timestamp对象:

datenewtodofixedclose
02023-08-20 00:00:00+00:002000
12023-08-27 00:00:00+00:000000
22023-09-03 00:00:00+00:000000
32023-09-10 00:00:00+00:000000
42023-09-17 00:00:00+00:000000
52023-09-24 00:00:00+00:000000
62023-10-01 00:00:00+00:000000
72023-10-08 00:00:00+00:000000
82023-10-15 00:00:00+00:000000
92023-10-22 00:00:00+00:000000
102023-10-29 00:00:00+00:000005
112023-11-05 00:00:00+00:000003
122023-11-12 00:00:00+00:000003
132023-11-19 00:00:00+00:000001
142023-11-26 00:00:00+00:000001
152023-12-03 00:00:00+00:000001
162023-12-10 00:00:00+00:000000
172023-12-17 00:00:00+00:000001
182023-12-24 00:00:00+00:000003
192023-12-31 00:00:00+00:000000
202024-01-07 00:00:00+00:000000
212024-01-14 00:00:00+00:000110
222024-01-21 00:00:00+00:000000
232024-01-28 00:00:00+00:000000
242024-02-04 00:00:00+00:000001
252024-02-11 00:00:00+00:000000
262024-02-18 00:00:00+00:000000
272024-02-25 00:00:00+00:000000
282024-03-03 00:00:00+00:000000
292024-03-10 00:00:00+00:000010
302024-03-17 00:00:00+00:000000
312024-03-24 00:00:00+00:000000
322024-03-31 00:00:00+00:000000
332024-04-07 00:00:00+00:000001

尝试使用Pandas绘制堆叠柱状图时,柱状图本身显示正常,但X轴日期异常,显示为1970年左右的日期,而非数据中的2023-2024年。使用的代码如下:

matplotlib.use('agg')
matplotlib.style.use('ggplot')

fig, ax = plt.subplots(figsize=(16, 10))

df.plot(
    kind='bar',
    ax=ax,
    x='date',
    use_index=True,
    stacked=True,
)

ax.xaxis.set_major_formatter(matplotlib.dates.AutoDateFormatter(ax.xaxis.get_major_locator()))

ax.tick_params(
    'x',
    labelrotation=75
)

ax.figure.savefig(f'fig/{filename}')

plt.close(ax.figure)

经测试,直接使用matplotlib绘制单系列柱状图时X轴日期显示正常,代码如下:

ax.bar(df['date'],
    df['close'],
    width=3,
    )

问题原因

当使用Pandas的plot(kind='bar')并指定x='date'时,Pandas会将date列视为分类变量而非时间序列变量。此时X轴的底层刻度值是分类的索引(0、1、2...),而非原始的Timestamp值。之后用matplotlib.dates.AutoDateFormatter格式化这些索引值时,会把它们当作Unix时间戳(从1970-01-01开始的秒数),因此显示出1970年的异常日期。

解决方法

方法一:将date列设为DataFrame索引

把date列设置为DataFrame的索引,让Pandas识别其为时间序列,自动处理日期刻度:

matplotlib.use('agg')
matplotlib.style.use('ggplot')

# 将date列设为索引
df.set_index('date', inplace=True)

fig, ax = plt.subplots(figsize=(16, 10))

df.plot(
    kind='bar',
    ax=ax,
    stacked=True,
)

# 自定义日期显示格式
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%Y-%m-%d'))
ax.tick_params('x', labelrotation=75)

ax.figure.savefig(f'fig/{filename}')
plt.close(ax.figure)

方法二:手动设置X轴刻度标签

如果不想修改原DataFrame结构,可以直接提取date列的格式化字符串替换X轴刻度标签:

matplotlib.use('agg')
matplotlib.style.use('ggplot')

fig, ax = plt.subplots(figsize=(16, 10))

df.plot(
    kind='bar',
    ax=ax,
    x='date',
    stacked=True,
    use_index=True,
)

# 替换刻度标签为格式化后的日期
ax.set_xticklabels(df['date'].dt.strftime('%Y-%m-%d'))
ax.tick_params('x', labelrotation=75)

ax.figure.savefig(f'fig/{filename}')
plt.close(ax.figure)

注意事项

  • 方法一更符合时间序列可视化的规范,Pandas会自动处理时间相关的刻度逻辑;
  • 方法二更灵活,适合需要保留原DataFrame结构的场景;
  • 注意代码中df['close']的大小写,原测试代码中的CLOSED应为close(匹配DataFrame列名)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 06:07:01