CleanVisuals
By: P. Bradshaw
Tags: Plotting, Data Visualization, Fivethirtyeight, Data Science, Matplotlib

What We Will Create:

Above, we have a horizontal bar plot. For the moment, they are one of my favorites to create just because of the readability. Below, I am going to show you the code I used to create this, then break it down per section to make it easier to follow.

If you wanted to see what this looks like in its “raw” form..

In a lot of cases, this is totally fine. However as you can see, this plot needs a little love, but it can be achieved with this one line of code:

top_stem.plot.barh(x='Major', y='Total')

Now..to improve this:

# Using the five thirty eight style

plt.style.context('fivethirtyeight') # Colors

cmap = ['#fff100', '#ff8c00', '#e81123', '#ec008c', '#68217a', >>>>>>>>'#00188f', '#00bcf2', '#00b294', '#009e49', '#bad80a'] # Plotting the top 10 STEM Majors

ax = top_stem.plot.barh(x='Major', y='Total', figsize=(12,8), >>>>>color=cmap) # Title and Subtitle

ax.text(s="Let's Talk S.T.E.M ", y=10.6, x=-5, fontsize=17, >>>>>>>>fontweight='semibold')

ax.text(s="Looking At The Top Ten S.T.E.M Related Degrees Chosen By >>>>>>>>Both Sexes ", y=10, x=-5, fontsize=14, fontstyle='italic') # The little details

ax.spines['top'].set_visible(False)

ax.spines['right'].set_visible(False)

ax.spines['bottom'].set_visible(False)

ax.spines['left'].set_visible(False) # Removing y-label, legend and hiding my y-axis

ax.set_ylabel('')

ax.get_legend().remove()

ax.yaxis.set_visible(False) # Plotting Labels particular positions

ax.text(x=60000,y=8.9, s="Civil Engineering", fontsize=12, >>>>>>>>fontweight='light', color=('#000000'));

ax.text(x=69000,y=7.9, s="General Engineering", fontsize=12, >>>>>>>>fontweight='light', color=('#000000'));

ax.text(x=70000,y=6.9, s="Multi-Disciplinary or General Science", >>>>>>>>fontsize=13, fontweight='light', color=('#000000'));

ax.text(x=74000,y=5.9, s="Chemistry", fontsize=12, >>>>>>>>fontweight='light', color=('#000000'));

ax.text(x=80000,y=4.9, s="Mathematics", fontsize=12, >>>>>>>>fontweight='light', color=('#000000'));

ax.text(x=89000,y=3.9, s="Electrical Engineering", fontsize=12, >>>>>>>>fontweight='light', color=('#000000'));

ax.text(x=99000,y=2.9, s="Mechanical Engineering", fontsize=12, >>>>>>>>fontweight='light', color=('#000000'));

ax.text(x=135000,y=1.9, s="Computer Science", fontsize=12, >>>>>>>>fontweight='light', color=('#000000'));

ax.text(x=215000,y=.9, s="Nursing", fontsize=12, fontweight='light', >>>>>>>>color=('#000000'));

ax.text(x=287000,y=-0.10, s="Biology", fontsize=12, >>>>>>>>fontweight='light', color=('#000000'));

If you are familiar with matplotlib style, then you know that they have many to choose from. Since, our goal is to replicate a fivethiryeight plot, we will go with that style.

plt.style.context('fivethirtyeight')

For this next part, you can add color in a few different ways, however for this particular plot, I wanted each degree to have it’s own color. That is why I went the more tedious route of adding HEX color codes individually. You do not have to do this, you can just pass in one color if you prefer.

cmap = ['#fff100', '#ff8c00', '#e81123', '#ec008c', '#68217a', >>>>>>>>'#00188f', '#00bcf2', '#00b294', '#009e49', '#bad80a']

After setting our colors, we can add that into our plot assigning it to color=()

ax = top_stem.plot.barh(x='Major', y='Total',

figsize=(12,8), color=cmap)

And like that you have a plot.. Let’s add a title and subtitle and set their positions.

ax.text(s="Let's Talk S.T.E.M ", y=10.6, x=-5, fontsize=17,

>>>>>>>>fontweight='semibold') ax.text(s="Looking At The Top Ten S.T.E.M Related Degrees Chosen By >>>>>>>>Both Sexes ", y=10, x=-5, fontsize=14, fontstyle='italic')

Next, I want to remove the black box style frame from my plot.

ax.spines['top'].set_visible(False)

ax.spines['right'].set_visible(False)

ax.spines['bottom'].set_visible(False)

ax.spines['left'].set_visible(False)

I didn’t have an x-label on this graph, but I do want to hide the y-label and the legend. This is inner-changeable, if you wanted to hide an x-axis instead of a y, or hide both simply switch the y to an x.

ax.set_ylabel('')

ax.get_legend().remove()

In most cases, you are basically done at this point. This next step is a little more tedious…alright it’s annoying and you don’t have to do this. However, I chose to take this step because my y-axis descriptors are long and I did not like the look of it. So I placed them IN the plot. What’s cool about this is that you can set the position to wherever you like, and add all of the same customization features as a title or subtitle. I call it annoying because you have to keep tweaking it to get each label in just the right spot. However, once you get there the results are worth all the effort.