Matplotlib Tutorial
Let’s make some maps! 🗺
Hello, and welcome to a brand new matplotlib tutorial. This time, I’ll train you the best way to create insightful Hexagon maps just like the one above.
Visualizing geographic data is troublesome as a result of areas (akin to nations) fluctuate in dimension and form.
The result’s that some areas are arduous to see whenever you plot your knowledge utilizing common maps.
It’s additionally troublesome so as to add data akin to nation names or values to your visualizations.
Another that removes such variations is to make use of a hexagon map.
The thought is to characterize every space as a hexagon and organize them in a manner that resembles the precise map.
Since every hexagon is equivalent in form, it’s simple so as to add data in a structured manner and to create a stupendous knowledge visualization.
This tutorial teaches you the best way to just do that utilizing knowledge from the presidential elections in the USA.
(Don’t neglect to have a look at my different Matplotlib tutorials as properly)
Let’s get began. 🚀
Step 1: Import libraries
We begin by importing the required libraries.
import pandas as pd
from matplotlib.patches import Polygon
import matplotlib.pyplot as plt
import seaborn as snsimport matplotlib.patheffects as PathEffects
That’s it.
Step 2: Create a seaborn type
Subsequent, we use seaborn to set the background and font household. I’m utilizing Work Sans
and #F4EBCD
, however be happy to experiment.
font_family = "Work sans"
background_color = "#E0E9F5"sns.set_style({
"axes.facecolor": background_color,
"determine.facecolor": background_color,
"font.household": font_family,
})
FYI: I usually use background_color="#00000000"
to get a clear background if I need to add the chart to an infographic or related.