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

Matplotlib绘图异常:日期与数据坐标轴疑似映射错误

Matplotlib绘图异常:日期与数据坐标轴疑似映射错误

我现在用Matplotlib绘制一张折线图,X轴设为日期,Y轴对应数据,但画出来的图完全不符合预期,实在搞不懂问题出在哪……

我的代码如下:

df['Date_TrailClean'] = pd.to_datetime(df['Date_TrailClean'])
# x axis values
x = df['Date_TrailClean']
# corresponding y axis values
y = df['AdjTotItems']

fig, ax = plt.subplots()
# plotting the points 
ax.plot(x, y)

ax.xaxis.set_major_locator(mdates.MonthLocator(bymonth=(1, 7)))
ax.xaxis.set_minor_locator(mdates.MonthLocator())

ax.set_ylabel(r'Adjusted Total Items')

# function to show the plot
plt.show()

运行后生成的图看起来像是数据被映射到了错误的坐标轴上,效果如下:
异常的日期数据折线图

我用到的数据包含日期列Date_TrailClean和对应的数据列AdjTotItems。

备注:内容来源于stack exchange,提问作者Heather Kay

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 11:09:34