[PYTHON] Use date to x-axis of tsplot depicted in seaborn

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')

download.png

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.

Screen Shot 2017-07-26 at 15.14.07.png

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.

download.png

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()

download.png

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

Use date to x-axis of tsplot depicted in seaborn
Date of Address already in use error in Flask
Comparison of how to use higher-order functions in Python 2 and 3
Use <input type = "date"> in Flask
Summary of how to use pandas.DataFrame.loc
How to use classes in Theano
Mock in python-how to use mox
How to use SQLite in Python
Summary of how to use pyenv-virtualenv
Use PyCaret to predict the price of pre-owned apartments in Tokyo!
How to use ChemSpider in Python
How to use PubChem in Python
Summary of how to use csvkit
I tried various patterns of date strings to be entered in pandas.to_datetime
How to use calculated columns in CASTable
Conversion of string <-> date (date, datetime) in Python
[Python] Summary of how to use pandas
[Introduction to Python] How to use class in Python?
Use of constraints file added in pip 7.1
How to use Google Test in C
Easy way to use Wikipedia in Python
Minimum knowledge to use Form in Flask
[Python2.7] Summary of how to use unittest
How to use Anaconda interpreter in PyCharm
R: Use Japanese instead of Japanese in scripts
How to use __slots__ in Python class
Jupyter Notebook Basics of how to use
Basics of PyTorch (1) -How to use Tensor-
Summary of how to use Python list
How to use regular expressions in Python
[Python2.7] Summary of how to use subprocess
How to use Map in Android ViewPager
How to use is and == in Python
[Question] How to use plot_surface of python
Use the Java SDK of GoogleMapsAPI to get the result of reverse GeoCoding in Japanese.
I want to use Python in the environment of pyenv + pipenv on Windows 10
How to use the C library in Python
How to use folium (visualization of location information)
A simple example of how to use ArgumentParser
[Python] How to use two types of type ()
How to keep track of work in Powershell
Convert "number" of excel date to python datetime
Summary of how to import files in Python 3
How to use Python Image Library in python3 series
Not much mention of how to use Pickle
EP 11 Use `zip` to Process Iterators in Parallel
Use cryptography module to handle OpenSSL in Python
Story of trying to use tensorboard with pytorch
How to use tkinter with python in pyenv
Summary of studying Python to use AWS Lambda
Use pygogo to get the log in json.
To represent date, time, time, and seconds in Python
Use os.getenv to get environment variables in Python
How to display the modification date of a file in C language up to nanoseconds
I want to use a network defined by myself in PPO2 of Stable Baselines
Use API to mark a large number of unread emails in Gmail as read
[Implementation explanation] How to use the Japanese version of BERT in Google Colaboratory (PyTorch)