繁体   English   中英

如何使用 Seaborn(或 matplotlib)在 x 轴上绘制日期

[英]How to plot dates on the x-axis using Seaborn (or matplotlib)

我有一个包含一些时间序列数据的 csv 文件。 我创建了一个数据框:

df = pd.read_csv('C:\\Desktop\\Scripts\\TimeSeries.log')

当我调用df.head(6) ,数据显示如下:

Company     Date                 Value
ABC         08/21/16 00:00:00    500
ABC         08/22/16 00:00:00    600
ABC         08/23/16 00:00:00    650
ABC         08/24/16 00:00:00    625
ABC         08/25/16 00:00:00    675
ABC         08/26/16 00:00:00    680

然后,我有以下强制“日期”列转换为日期时间格式:

df['Date'] = pd.to_datetime(df['Date'], errors = 'coerce')

有趣的是,当我调用以下内容时,我看到了“ pandas.core.series.Series ”:

type(df['Date'])

最后,我调用以下代码来创建一个图:

%matplotlib qt
sns.tsplot(df['Value'])

在从左到右的 x 轴上,我看到从 0 到数据框中行数的整数。 如何将“日期”列作为 x 轴值添加到该图中?

谢谢!

不确定 tsplot 是最好的工具。 你可以只使用:

df[['Date','Value']].set_index('Date').plot()

使用tsplottime参数

来自文档:

time : string or series-like
    Either the name of the field corresponding to time in the data DataFrame or x values for a plot when data is an array. If a Series, the name will be used to label the x axis.
#Plot the Value column against Date column
sns.tsplot(data = df['Value'], time = df['Date'])

然而tsplot用于在不同条件下在同一时间窗口中绘制时间序列。 要绘制单个时间序列,您还可以使用plt.plot(time = df['Date'], data = df['Value'])

我认为为时已晚。

首先,您必须注意到“日期”列是一系列“日期时间”类型,因此您应该这样做以获得“日期”部分:

df['Date'] = df['Date'].map(lambda x:x.date())

现在按“日期”对数据框进行分组,然后重置索引以使“日期”成为一列(而不是索引)。

然后你可以使用plt.plot_date

df_groupedby_date = df.groupby('Date').count()
df_groupedby_date.reset_index(inplace=True)
plt.plot_date(x=df_groupedby_date['Date'], y=df_groupedby_date['Value'])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM