Skip to content
Advertisement

Plotting Pandas DataFrame from pivot

I am trying to plot a line graph comparing the Murder Rates of particular States through the years 1960-1962 using Pandas in a Jupyter Notebook.

A little context about where I am now, and how I arrived here:

I’m using a crime csv file, which looks like this: enter image description here

I’m only interested in 3 columns for the time being: State, Year, and Murder Rate. Specifically I was interested in only 5 states – Alaska, Michigan, Minnesota, Maine, Wisconsin.

So to produce the desired table, I did this (only showing top 5 row entries):

JavaScript

enter image description here

From here I used the pivot function

JavaScript

enter image description here

And this is where I get stuck. I received KeyError when doing (KeyError was Year):

JavaScript

and when attempting just

JavaScript

I get this wonky graph.

enter image description here

How do I get my desired graph?

Advertisement

Answer

Given a dataframe in a long (tidy) format, pandas.DataFrame.pivot is used to transform to a wide format, which can be plotted directly with pandas.DataFrame.plot

Tested in python 3.8.11, pandas 1.3.3, matplotlib 3.4.3

JavaScript

The plots

You can tell Pandas (and through it the matplotlib package that actually does the plotting) what xticks you want explicitly:

JavaScript

Output:

enter image description here

ax is a matplotlib.axes.Axes object, and there are many, many customizations you can make to your plot through it.

Here’s how to plot with the States on the x axis:

JavaScript

Output:

enter image description here

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement