在x轴 pandas Matplotlib中处理日期 [英] Manipulating Dates in x-axis Pandas Matplotlib
本文介绍了在x轴 pandas Matplotlib中处理日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一组非常简单的数据,如下所示。我正在寻找一种方法来绘制此堆积的条形图并格式化x轴(日期),使其开始于1996-31-12,结束于2016-31-12,增量为365天。我编写的代码正在绘制每个日期,因此x轴非常混乱并且不可读。
I have a pretty simple set of data as displayed below. I am looking for a way to plot this stacked bar chart and format the x-axis (dates) so it starts at 1996-31-12 and ends at 2016-31-12 on increments of 365 days. The code I have written is plotting every single date and therefore the x-axis is very bunched up and not readable.
Datafame:
Date A B
1996-31-12 10 3
1997-31-03 5 6
1997-31-07 7 5
1997-30-11 3 12
1997-31-12 4 10
1998-31-03 5 8
.
.
.
2016-31-12 3 9
推荐答案
这是一个类似的问题:熊猫时间序列图设置x轴主要和次要刻度线和标签
This is a similar question: Pandas timeseries plot setting x-axis major and minor ticks and labels
您可以使用matplotlib本身而不是熊猫来管理它。
You can manage this using matplotlib itself instead of pandas.
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# if your dates are strings you need this step
df.Date = pd.to_datetime(df.Date)
fig,ax = plt.subplots()
ax.plot_date(df.Date,df.A)
ax.plot_date(df.Date,df.B)
ax.xaxis.set_major_locator(mdates.YearLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b\n%Y'))
plt.show()
这篇关于在x轴 pandas Matplotlib中处理日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文