[英]How to plot dates on the x-axis using Seaborn (or matplotlib)
I have a csv file with some time series data.我有一个包含一些时间序列数据的 csv 文件。 I create a Data Frame as such:
我创建了一个数据框:
df = pd.read_csv('C:\\Desktop\\Scripts\\TimeSeries.log')
When I call df.head(6)
, the data appears as follows:当我调用
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
Then, I have the following to force the 'Date' column into datetime format:然后,我有以下强制“日期”列转换为日期时间格式:
df['Date'] = pd.to_datetime(df['Date'], errors = 'coerce')
Interestingly, I see " pandas.core.series.Series
" when I call the following:有趣的是,当我调用以下内容时,我看到了“
pandas.core.series.Series
”:
type(df['Date'])
Finally, I call the following to create a plot:最后,我调用以下代码来创建一个图:
%matplotlib qt
sns.tsplot(df['Value'])
On the x-axis from left to right, I see integers ranging from 0 to the number of rows in the data frame.在从左到右的 x 轴上,我看到从 0 到数据框中行数的整数。 How does one add the 'Date' column as the x-axis values to this plot?
如何将“日期”列作为 x 轴值添加到该图中?
Thanks!谢谢!
Not sure that tsplot is the best tool for that.不确定 tsplot 是最好的工具。 You can just use:
你可以只使用:
df[['Date','Value']].set_index('Date').plot()
use the time
parameter for tsplot
使用
tsplot
的time
参数
from docs:来自文档:
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'])
However tsplot
is used to plot timeseries in the same time window for different conditions.然而
tsplot
用于在不同条件下在同一时间窗口中绘制时间序列。 To plot a single timeseries you could also use plt.plot(time = df['Date'], data = df['Value'])
要绘制单个时间序列,您还可以使用
plt.plot(time = df['Date'], data = df['Value'])
I think it is too late.我认为为时已晚。
First, you have to notice that 'Date' column is a series of 'datetime' type so you should do that to get the 'date' part:首先,您必须注意到“日期”列是一系列“日期时间”类型,因此您应该这样做以获得“日期”部分:
df['Date'] = df['Date'].map(lambda x:x.date())
now group your data frame by 'Date' and then reset index in order to make 'Date' a column (not an index).现在按“日期”对数据框进行分组,然后重置索引以使“日期”成为一列(而不是索引)。
Then you can use plt.plot_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.