Plotting time-series data with matplotlib
Category: matplotlib, year: 2020
Below is an example of how to plot time-series data (data values attached to actual dates) read from a .csv (comma-separated value) file, with matplotlib in Python.
Note: the code uses extra modules than is strictly necessary for convenience: pandas for parsing date formats, and seaborn for styling the plots.
import matplotlib as mpl
import matplotlib.pyplot as plt
import datetime
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.dates as mdates
from matplotlib.dates import DateFormatter
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
# read values from specified .csv file
input_file = "/tmp/path_to_input_file.csv"
date_values = []
plot_values = []
data_file = open(input_file, "r")
for line in data_file:
# skip empty or commented lines
if len(line) == 0:
continue
if line[0] == '#':
continue
# make sure there's at least one comma separator in the line
if not ',' in line:
continue
# split first two columns, into date and value for that date
pair = line.split(',')
date_str = pair[0].strip()
value_str = pair[1].strip()
# parse the date string into an actual date type
date_value = pd.to_datetime(date_str, format="%d/%m/%Y")
date_values.append(date_value)
# Note: for floating point values, this cast to int() will need to be changed
plot_values.append(int(value_str))
sns.set()
sns.set_style('darkgrid', {'axes.linewidth': 0.5, 'axes.edgecolor':'black'})
fig, ax = plt.subplots(1, 1)
fig.tight_layout()
fig.set_figwidth(17)
fig.set_figheight(9)
ax.plot(date_values, plot_values)
# set up comma thousand-separator formatting for the Y-axis values
ax.get_yaxis().set_major_formatter(mpl.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))
ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())
ax.grid(visible=True, which='major', color='w', linewidth=0.8)
ax.grid(visible=True, which='minor', color='w', linewidth=0.4)
# plot minor X-axis gridlines every month
ax.xaxis.set_minor_locator(mdates.MonthLocator(interval=1))
# X-axis values start with the first date value and end with the last date value by default
x_axis_start_date = date_values[0]
x_axis_end_date = date_values[-1]
# uncomment this to push the X-axis end date limit further by 12 days to pad things a bit
#x_axis_end_date += datetime.timedelta(days=12)
ax.set_xlim([x_axis_start_date, x_axis_end_date])
fig.tight_layout()
plt.show()