Convert DataFrame index to DatetimeIndex when downsampling (http://qiita.com/TomHortons/items/9633394afb55bd777fa0#4-%E3%82%B5%E3%83%B3%E3%83%97 % E3% 83% AB% E9% 96% 93% E9% 9A% 94% E3% 81% 8C% E3% 81% BE% E3% 81% B0% E3% 82% 89% E3% 81% AA% E3 % 83% 87% E3% 83% BC% E3% 82% BF% E3% 82% 92% E3% 83% 80% E3% 82% A6% E3% 83% B3% E3% 82% B5% E3% 83 % B3% E3% 83% 97% E3% 83% AA% E3% 83% B3% E3% 82% B0% E3% 81% 99% E3% 82% 8B), which is very convenient, but from there sns.tsplot It takes a long time to visualize with.
As an example, create a DataFrame.
import datetime
import pandas as pd
df = []
start_date = datetime.datetime(2015, 7, 1)
for i in range(10):
for j in [1,2]:
unit = 'Ones' if j == 1 else 'Twos'
date = start_date + datetime.timedelta(days=i)
df.append({
'Date': date,
'Value': i * j,
'Unit': unit
})
df = pd.DataFrame(df)
When executed, a DataFrame containing the following Datetime type columns will be created.
Date Unit Value
0 2015-07-01 Ones 0
1 2015-07-01 Twos 0
2 2015-07-02 Ones 1
3 2015-07-02 Twos 2
4 2015-07-03 Ones 2
5 2015-07-03 Twos 4
6 2015-07-04 Ones 3
7 2015-07-04 Twos 6
8 2015-07-05 Ones 4
9 2015-07-05 Twos 8
10 2015-07-06 Ones 5
11 2015-07-06 Twos 10
12 2015-07-07 Ones 6
13 2015-07-07 Twos 12
14 2015-07-08 Ones 7
15 2015-07-08 Twos 14
16 2015-07-09 Ones 8
17 2015-07-09 Twos 16
18 2015-07-10 Ones 9
19 2015-07-10 Twos 18
If you draw this DataFrame with sns.tsplot,
import seaborn as sns
import matplotlib.pyplot as plt
sns.tsplot(df, time='Date', value='Value', unit='Unit')
Datetime is not displayed normally even though Date is specified on the horizontal axis. If you use DataFrame.plot (), this area will be displayed smoothly, but if you use tsplot, you definitely want to use seaborn.
Questions around this area were summarized in StackOverflow.
Matplotlib uses floating point numbers for dates, and pandas and seaborn seem to need to explicitly indicate them. Therefore, use date locator and date formatter.
First, convert the date so that matplotlib.date can understand it.
import datetime
import pandas as pd
import matplotlib.dates as mdates
df = []
start_date = datetime.datetime(2015, 7, 1)
for i in range(10):
for j in [1,2]:
unit = 'Ones' if j == 1 else 'Twos'
date = start_date + datetime.timedelta(days=i)
df.append({
'Date': mdates.date2num(date),
'Value': i * j,
'Unit': unit
})
df = pd.DataFrame(df)
It is almost the same as the first DataFrame, but before putting the date in Date, it is converted by matplotlib.dates.date2num ().
Alternatively, you can process the existing DataFrame with map as follows.
df.index = map(lambda x: mdates.date2num(x), df.index)
After converting Date, draw tsplot.
import seaborn as sns
import matplotlib.pyplot as plt
# build the figure
fig, ax = plt.subplots()
sns.tsplot(df, time='Date', value='Value', unit='Unit', ax=ax)
# assign locator and formatter for the xaxis ticks.
ax.xaxis.set_major_locator(mdates.AutoDateLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y.%m.%d'))
After specifying ax in subplot, set the axis with set_major_locator and set_major_formatter. Here,% Y.% M.% D is modified as necessary.
When executed, it looks like this.
The date can be displayed as the x-axis. The angle of the letters is tilted by 45 degrees to make it easier to see the overlapping parts.
# put the labels at 45deg since they tend to be too long
fig.autofmt_xdate()
I was able to use the date on the x-axis safely. ** There are several ways to instruct to tilt the axis display, but note that the date has been garbled several times depending on the method. ** **
Recommended Posts