Home Artificial Intelligence Construct a Higher Bar Chart with This Trick

Construct a Higher Bar Chart with This Trick

1
Construct a Higher Bar Chart with This Trick

(It’s really a seaborn scatter plot!)

A part of an “Age of Congress” scatter plot (all images by the creator)

Every time I want inspiration for effective visualizations, I browse The Economist, the Visual Capitalist, or The Washington Post. During one in all these forays, I ran across an interesting infographic — much like the one shown above — that plotted the age of every member of the US Congress against their generational cohort.

My first impression was that this was a horizontal bar chart, but closer inspection revealed that every bar was composed of multiple markers, making it a scatter plot. Each marker represented one member of Congress.

On this Quick Success Data Science project, we’ll recreate this attractive chart using Python, pandas, and seaborn. Along the way in which, we’ll unlock a cornucopia of marker types chances are you’ll not know exist.

Because the USA has Age of Candidacy laws, the birthdays of members of Congress are a part of the general public record. You’ll find them in multiple places, including the Biographical Directory of the USA Congress and Wikipedia.

For convenience, I’ve already compiled a CSV file of the names of the present members of Congress, together with their birthdays, branch of presidency, and party, and stored it on this Gist.

The next code was written in Jupyter Lab and is described by cell.

Importing Libraries

from collections import defaultdict  # For counting members by age.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import patches # For drawing boxes on the plot.
import pandas as pd
import seaborn as sns

Assigning Constants for the Generational Data

We’ll annotate the plot in order that generational cohorts, akin to Baby Boomers and Gen X, are highlighted. The next code calculates the present age spans for every cohort and includes lists for generation names and highlight colours. Because we would like to treat these lists as constants, we’ll capitalize the names and use an underscore as a prefix.

# Prepare generational data for plotting as boxes on chart:
CURRENT_YEAR = 2023…

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here