如何绘制日期和月份 [英] How to plot day and month
本文介绍了如何绘制日期和月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个随时间变化的每日趋势图.
I have a chart of a daily trend over time.
这里的年份无关紧要,我只想显示日期和月份.我知道您可以显示年份和月份,但事实并非如此.
The year is not relevant here and I want to show only day and month. I know you can show year and month but that is not the case.
我试图创建一个名为"Day_Month"的新变量:
I tried to create a new variable called "Day_Month":
import datetime as dt
df['Day'] = df['date'].dt.day
df['Month'] = df['date'].dt.month
df['Day_Month'] = df['Day'].astype(str) + "-" +
但是无法将其绘制为字符串或将其转换为日期类型.
but it's not possible to plot it as a string nor to convert it to date type.
最终,我希望图表如下所示:
eventually, I would like my chart to look like this:
推荐答案
我找到的答案是:
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(date, price , label="Price")
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d'))
这篇关于如何绘制日期和月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文