Step-by-step: How to plot a map with slider to represent time evolution of murder rate in the US using Plotly
This post in based on this other one I posted a few days ago, where I'm exploring a new data set about murder rates in the US. I decided to write a plot detailing how to plot a map of said murder rates in the US, but also adding a slider to explore the different years included in the data set.
The data is gathered and published by the FBI, but I'm gonna be using this other version, that I minimally modified to make it easier to use.
Also, I am using this post to practice how to embed my own python code in a way that is useful and looks nice. I am gonna do this by converting the particular snippet of code into html here.
Ok, so, let's start with the data, which looks like this:
We have one row per year and per state, and in each one of them, we have the population of the state, as well as the total number of violent crimes committed, the breakdown by type of crime, and the murder rate per 100,000 people.
I'm gonna start by plotting a single year of data on a map of the US, and then I'll build from there.
First, some imports:
I load the data file, and select the focus year (I'll also remove data for District of Columbia, for now):
Then I specify the color scheme for the map plot, and I create a new column that will include the mouse-hovering text for each state:
I decided to go with a red color scale, as it has an almost-intuitive association with violence. But you can pick different color schemes if you prefer. I recommend the color brewer site or the color picker site. In particular, the color brewer site not only gives you the hexadecimal or RGB codes for the colors you like, but also, it suggests pleasant color schemes for a given number of colors, as well as black & white printer-friendly, or color blind-friendly color schemes etc.
The data object for plotting need to be a list of dictionaries set up as follows:
Then I take care of the layout, create the figure object and I plot it :
Or, if you want to plot it in a different window on your browser:
This is the output:
We clearly observe how in 1960, the South was a considerable more violent region of the US than the North. It would be interesting to see how that pattern changes over time.
Thus, now we make the necessary modifications to add a slider to go over the different years in the data set. The main conceptual differences are that now the data object now is going to be a list of dictionaries, and also that I need to create a 'steps', and a 'slider' object that will go as an argument for the layout command.
After loading the data set and defining the color scheme, I create an empty list for the data, that I will populate with dictionaries (one per year):
Now, I populate the data object, one dictionary per year that will be displayed with the slider, by iterating over the different years in the data set:
Next, I create the 'steps' and the 'slider' objects:
Finally, we create the layout, and plot it (both on the notebook and as a separate browser window):
And that's it! See how it looks:
As we slide from one year to the next, we observe how the South-North differences get less accentuated. Note also that the color scale is relative to each year (that is something I'll fix later on).