How to create a meaningful bar plot from time series data in Python

Cristina Sahoo
2 min readDec 9, 2020

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.sort_values(by=’date’, inplace=True)
df[‘date’] = pd.to_datetime(df[‘date’])
df.set_index(‘date’, inplace=True)
df.head()

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()
f.set_figheight(10)
f.set_figwidth(18)

for idx, ax in enumerate(a):
ax.xaxis.set_visible(True)
for tick in ax.get_xticklabels():
tick.set_rotation(45)
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)
ax.set_xticks(dates)
ax.get_xticklabels()[max_id].set_color(‘salmon’)
ax.get_xticklabels()[min_id].set_color(‘orange’)
ax.set_ylabel(yaxes[idx])

plt.tight_layout()
f.savefig(‘train.jpg’)
plt.show()

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!

--

--

Cristina Sahoo

I am a Data Scientist with 10+ years experience in the Tech Industry, and a background in Education and Customer Service.