This article is the 22nd day article of MicroAd Advent Calendar 2019.
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.
You can use matplotlib.dates
to plot nicely.
Here is a code example.
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.
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:
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.
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.
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
.
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. You can see that the angle of the label has changed.
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 ...
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