Time series plot started ~ python edition ~

Introduction

This article is the 22nd day article of MicroAd Advent Calendar 2019.

Thing you want to do

Time series plots are a very useful tool when visualizing log data. Until now, I have lived such a life where I escaped by plotting the dates on the horizontal axis by replacing them with numbers. But now it is different. Now you can plot well with the horizontal axis in chronological order! So, I would like to summarize the story as a memorandum. The graph created here uses only the ** minimum ** option.

Method

You can use matplotlib.dates to plot nicely. Here is a code example.

Daily

import io
import pandas as pd
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()


def main():
    data = """date,value
    2018-12-01,15
    2018-12-02,30
    2018-12-03,25
    2018-12-04,18
    2018-12-05,9
    2018-12-06,22
    2018-12-07,34
    2018-12-08,33
    2018-12-09,28
    2018-12-10,22
    2018-12-11,26
    2018-12-12,31"""
    df = pd.read_csv(io.StringIO(data), parse_dates=[0])

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.plot(df['date'], df['value'])

    ##Customize the following
    daysFmt = mdates.DateFormatter('%Y-%m-%d')
    ax.xaxis.set_major_formatter(daysFmt)
    fig.autofmt_xdate()

    plt.show()


if __name__ == '__main__':
    main()

The figure created by this code is as follows. qiita.png

If you want to change the display method a little,

    daysFmt = mdates.DateFormatter('%Y-%m-%d')

You can play around with it here.

    daysFmt = mdates.DateFormatter('%m/%d')

If you change it as above, the x-axis label will look like this: qiita2.png

By date and time

I will change the data a little and try it for each date and time.


import io
import pandas as pd
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()


def main():
    data = """date,value
        2018-12-01 00,15
        2018-12-01 01,30
        2018-12-01 02,25
        2018-12-01 03,18
        2018-12-01 04,9
        2018-12-01 05,22
        2018-12-01 06,34
        2018-12-01 07,33
        2018-12-01 08,28
        2018-12-01 09,22
        2018-12-01 10,26
        2018-12-01 11,31"""
    df = pd.read_csv(io.StringIO(data), parse_dates=[0])

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.plot(df['date'], df['value'])

    ##Customize the following
    daysFmt = mdates.DateFormatter('%m/%d %H:%M')
    ax.xaxis.set_major_formatter(daysFmt)
    fig.autofmt_xdate()

    plt.show()


if __name__ == '__main__':
    main()

The obtained figure is as follows. qiita3.png

Change the frequency of the scale

If you want to change the frequency of the scale, add the following two lines.

    daysLoc = mdates.HourLocator(byhour=None, interval=3, tz=None)
    ax.xaxis.set_major_locator(daysLoc)

This will change the frequency of the scale. qiita4.png It has become a very lonely figure. This time I set it with ʻinterval, but you can get exactly the same result by removing the ʻinterval option and setting it withbyhour = range (2, 12, 3). In addition to the HourLocater used here, there are many such as MonthLocator, WeekdayLocator, DayLocator, MonthLocator, MinuteLocator, SecondLocator.

Rotation of axis label

The axis label is too long and sticks out! In that case, you can rotate it. You can do that by adding the following two lines.

    labels = ax.get_xticklabels()
    plt.setp(labels, rotation=45)

This is the figure obtained. qiita5.png You can see that the angle of the label has changed.

Finally

Here's a summary of the ** minimum ** options I needed to plot over time. Create a graph that is easier to understand depending on the situation! Now that we have the visualization, it's time series analysis ...

Reference material

matplotlib.dates (official documentation) Axis setting when plotting time series data with matplotlib + bonus Python matplotlib time series graph (time axis setting) [Set the axis scale of the graph of time series data with matplotlib.dates] (https://bunseki-train.com/setting_ticks_by_matplotlib_dates/)

Recommended Posts

Time series plot started ~ python edition ~
[Python] Plot time series data
Python: Time Series Analysis
Python time series question
Time series plot / Matplotlib
Python 2 series and 3 series (Anaconda edition)
Python: Time Series Analysis: Preprocessing Time Series Data
started python
[Python] Accelerates loading of time series CSV
Get Started with TopCoder in Python (2020 Edition)
[Python] Violin Plot
Time Series Decomposition
I started python
python time measurement
First time python
Plot CSV of time series data with unixtime value in Python (matplotlib)
Python: Time Series Analysis: Building a SARIMA Model
Get time series data from k-db.com in Python
Django 1.11 started with Python3.6
A * algorithm (Python edition)
First Python 3rd Edition
1.1 Getting Started with Python
Python 3.4 Create Windows7-64bit environment (for financial time series analysis)
Output the time from the time the program was started in python
Getting Started with Python
RNN_LSTM1 Time series analysis
Time series analysis 1 Basics
Output python execution time
Time floor function (Python)
[Time series with plotly] Dynamic visualization with plotly [python, stock price]
Getting Started with Python
python get current time
Python Mathematics Series ① Transpose
Display TOPIX time series
I made a package to filter time series with python
How to generate exponential pulse time series data in python
Reformat the timeline of the pandas time series plot with matplotlib
Graph time series data in Python using pandas and matplotlib
Python Math Series ② Matrix Multiplication
Getting Started with Python Functions
Python application: Pandas Part 2: Series
Python execution time measurement memo
PyTorch C ++ VS Python (2019 Edition)
Time series analysis related memo
Getting Started with Python Django (1)
CI environment construction ~ Python edition ~
Python Mathematics Series ③ Determinant (replacement)
Getting Started with Python Django (4)
Python installation (Mac edition) (old)
Getting Started with Python Django (3)
Python 3 series installation for mac
Execution time measurement with Python With
Time series analysis part 4 VAR
Time series analysis Part 3 Forecast
Python started by C programmers
Getting Started with Python Django (6)
Get started with Python! ~ ② Grammar ~
Time series analysis Part 1 Autocorrelation
Plot geographic information in Python
IfcOpenShell python bindings get started
Time synchronization (Windows) with Python