How to Create a Polar Histogram With Python and Matplotlib | by Oscar Leo | Aug, 2023

Matplotlib Tutorial

Creating a graph that captures the eye and tells the beholder an insightful story.

Graph created by the author

Hi, and welcome to this Python + Matplotlib tutorial, where I will show you how to create the beautiful polar histogram you see above.

Polar histograms are great when you have too many values for a standard bar chart. The circular shape where each bar gets thinner towards the middle allows us to cram more information into the same area.

A nice feature is the resulting visual comparison between the lowest and highest values as they meet each other after one lap around the center.

In this tutorial, I’m using data from the World Happiness Report and information about income levels from the World Bank. Both data sources are publicly available under Creative Commons licenses.

My data frame contains 146 countries and three columns.

Here’s what it looks like.

Screenshot by the author

I’ll show and explain every line of code required to create the visualization. If you want to follow along, you can find the code and data I’m using in this GitHub repository.

Let’s get started.

Importing libraries

We only need standard Python libraries familiar to everyone. PIL is not mandatory, but it’s my preferred choice for handling images which we do when adding flags.

import math
import numpy as np
import pandas as pd

import seaborn as sns
import matplotlib.pyplot as plt

from PIL import Image
from matplotlib.lines import Line2D
from matplotlib.patches import Wedge
from matplotlib.offsetbox import OffsetImage, AnnotationBbox

The only thing that stands out is a few specific Matplotlib imports at the end. I’ll cover those components later in the tutorial.

As usual, I use pandas to load the data.

Source link

Leave a Comment