Home Artificial Intelligence Construct a Higher Bar Chart with This Trick Dataset The Code

Construct a Higher Bar Chart with This Trick Dataset The Code

Construct a Higher Bar Chart with This Trick
The Code

(It’s really a seaborn scatter plot!)

Towards Data Science
A part of an “Age of Congress” scatter plot (all images by the writer)

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 best way, we’ll unlock a cornucopia of marker types you might not know exist.

Because the US has Age of Candidacy laws, the birthdays of members of Congress are a part of the general public record. You will discover them in multiple places, including the Biographical Directory of the US 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, resembling 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:


Please enter your comment!
Please enter your name here