Plotting with Pandas - Dates and Bar Plots

Dalya Gartzman
codeburst
Published in
3 min readJan 18, 2020

--

Starting a new job is an exciting phase for me, but it also led me to the following sad realization - I left all my best copy-pastable code snippets behind!

To avoid such future catastrophes, I decided to start a habit of publicly documenting my favorite (and most non-trivial) code tricks.

I will start with something I already had to do on my first week - plotting. In this post I will focus on plotting directly from Pandas, and using datetime related features.

Hope you find this useful as well!

For the full code behind this post go here.
Versions: python 3.7.3, pandas 0.23.4, matplotlib 3.0.2

Step I - setting up the data

In this post we will use the Austin pet adoption dataset from Kaggle.

Let’s start with reading the data, keeping only relevant columns, and making sure that date columns are read as datetime types.

Step II - Our Most Basic Plot

Let’s make a bar plot by the day of the week.
For that, we will extract both the weekday_name and weekday_num so as to make sure the days will be sorted:

And now we are ready to plot:

Step 3 - The Birthday Surprise

Basic statistics show that in every group of 50 people (or pets) we have a 99% chance of having at least two people (pets) who were born on the same day of the year.

Let’s test this!

Ho my, the dates look all messed up… Let’s try to sort this:

Looking much better now :)

Step 4 - Plot by Pet Color

Now we are ready for our final plot, showing stacked bars by pet color.

And now we are ready to make a beautiful plot:

And that’s it… Hope you found this useful!

--

--