Python 如何使用 Seaborn(或 matplotlib)在 x 轴上绘制日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39228121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to plot dates on the x-axis using Seaborn (or matplotlib)
提问by equanimity
I have a csv file with some time series data. I create a Data Frame as such:
我有一个包含一些时间序列数据的 csv 文件。我创建了一个数据框:
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. How does one add the 'Date' column as the x-axis values to this plot?
在从左到右的 x 轴上,我看到从 0 到数据框中行数的整数。如何将“日期”列作为 x 轴值添加到该图中?
Thanks!
谢谢!
回答by milos.ai
Not sure that tsplot is the best tool for that. You can just use:
不确定 tsplot 是最好的工具。你可以只使用:
df[['Date','Value']].set_index('Date').plot()
回答by Ryszard Cetnarski
use the time
parameter for tsplot
使用time
参数tsplot
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. To plot a single timeseries you could also use plt.plot(time = df['Date'], data = df['Value'])
然而tsplot
,用于在不同条件下在同一时间窗口中绘制时间序列。要绘制单个时间序列,您还可以使用plt.plot(time = df['Date'], data = df['Value'])
回答by Mohanad Refaai
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'])