简体   繁体   English

python pandas时间线图

[英]python pandas time line graph

I have the following data frame我有以下数据框

data_df = 
date          value
2016-01-15    1555
2016-01-16    1678
2016-01-17    1789
...  

I would like to create a timeline graph, with the date as the x axis我想创建一个时间线图,以日期为 x 轴

I import the visualization modules我导入可视化模块

import matplotlib.pyplot as plt
%matplotlib inline
import vincent as vin
import seaborn as sb

I try to add a column to format the date data_df['dates'] = plt.date2num(ad_data.date)我尝试添加一列来格式化日期 data_df['dates'] = plt.date2num(ad_data.date)

Then I want to plot the timeline plot_date(data_df.dates, data_df.shown)然后我想绘制时间线 plot_date(data_df.dates, data_df.shown)

This doesn't work, since I am not converting the date correctly.这不起作用,因为我没有正确转换日期。

You can use:您可以使用:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

#sample data    
start = pd.to_datetime('2016-01-15')
rng = pd.date_range(start, periods=100)

data_df = pd.DataFrame({'date': rng, 'value': range(100)})  
data_df.value = data_df.value * 15 / data_df.date.dt.day
print (data_df)
         date        value
0  2016-01-15     0.000000
1  2016-01-16     0.937500
2  2016-01-17     1.764706
3  2016-01-18     2.500000
4  2016-01-19     3.157895
5  2016-01-20     3.750000
6  2016-01-21     4.285714
7  2016-01-22     4.772727
8  2016-01-23     5.217391
9  2016-01-24     5.625000
10 2016-01-25     6.000000
...
...

If necessary convert column date to to_datetime and then set_index from column date :如果有必要将列dateto_datetime然后set_index从列date

data_df.date = pd.to_datetime(data_df.date)
data_df.set_index('date', inplace=True)
print (data_df)
                  value
date                   
2016-01-15     0.000000
2016-01-16     0.937500
2016-01-17     1.764706
2016-01-18     2.500000
2016-01-19     3.157895
2016-01-20     3.750000
2016-01-21     4.285714
2016-01-22     4.772727
2016-01-23     5.217391
2016-01-24     5.625000
2016-01-25     6.000000
...
...

Plot Series data_df['value'] by plot and then set format of axis x :通过plot绘制Series data_df['value'] ,然后设置x轴的格式:

ax = data_df['value'].plot()

ticklabels = data_df.index.strftime('%Y-%m-%d')
ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))
plt.show()

图形

If your date is a datetime thingy (if not, use pd.to_datetime() , it should recognize the format), it should work by just calling date_df.plot() .如果您的日期是日期时间(如果不是,请使用pd.to_datetime() ,它应该识别格式),它应该通过调用date_df.plot() Make sure it is set as index (so use date_df.index = date_df['date']确保它被设置为索引(所以使用date_df.index = date_df['date']

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

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