How to create a meaningful bar plot from time series data in Python
If you’re like me, you work pretty hard to create pretty visualizations, that are easy to read and understand by almost anybody. For example, when I plot a bar graph, I want to be able to highlight a certain bar, use different colors for the ticks on the x-axis, choose a nice font for the title, etc.
Below is an example of such a bar plot that I created for one of my projects. The data file can be downloaded from here. If you find this article useful or if you simply like it, please CLAP!
Import the necessary libraries.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
Read the data into a dataframe and view the first few observations/rows. As you can see in the screenshot below, the index date is a timestamp. The data is now a time series dataset.
df = pd.read_csv(‘train.csv’)
df[‘date’] = pd.to_datetime(df[‘date’])
Define variables for the x-axis, y-axis, titles, colors, and ticks.
xaxes = [‘temperature’, ‘humidity’, ‘light’, ‘co2’]
yaxes = [‘celsius’, ‘percent %’, ‘lux’, ‘ppm’]
titles = [‘Temperature’, ‘Humidity’, ‘Light’, ‘CO2’]
colors = [‘navy’, ‘purple’, ‘green’, ‘teal’]
dates = df.resample(‘D’).mean().index
Plot the bar graph. Please remember to indent correctly. I have added a screenshot of proper indentation, as the code in the article may not reflect it.
f, a = plt.subplots(2,2)
a = a.ravel()
for idx, ax in enumerate(a):
for tick in ax.get_xticklabels():
values = df.resample(‘D’).mean()[xaxes[idx]].values
clrs = [‘salmon’ if (x == max(values)) else colors[idx] for x in values]
max_id = clrs.index(‘salmon’)
clrs = [‘orange’ if (x == min(values)) else colors[idx] for x in values]
clrs[max_id] = ‘salmon’
min_id = clrs.index(‘orange’)
ax.bar(dates, df.resample(‘D’).mean()[xaxes[idx]], color=clrs)
ax.set_title(‘Average ‘+titles[idx]+’ by Day’, fontsize=18, color=colors[idx], pad=10)
I hope you enjoyed creating this visualization as much as I did :-).
If you find this article useful or if you simply like it, please CLAP!
Thank you. Happy coding!