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
相关产品推荐
相关产品推荐

