Take Your Histograms to the Next Level Using Matplotlib
By: Max Hilsdorf
Tags: Python, Programming, Data Visualization, Data Science, Data Analysis

Step 3: Emphasize Information

Emphasizing information doesn’t just mean increasing font sizes, but also f.e. choosing a color scheme that is benefitial to the message you are trying to convey with your visualization. Since this step will take much more code, I am going to show code snippets seperately.

First, we need a different set of colors. The orange/blue combination in the previous plots just doesn’t look good. An easy way to change your plots layout is to change the matplotlib style sheet. Personally, I love the “bmh” style sheet. However, this style sheet adds a grid, which would be distracting in our plot. Let’s change the style sheet to “bmh” and remove the grid it produces.

plt.style.use("bmh")

# Later in the code

ax.grid(False)

Another aesthetic improvement would be to reduce the histogram opacity. This will make the KDE more dominant which will give the reader an overall smoother impression.

avocado.plot(kind = "hist", density = True, alpha = 0.65, bins = 15)

To make the title stand out more, we can increase its font size. The “pad” argument will allow us to add an offset, too.

ax.set_title("Avocado Prices in U.S. Markets", size = 17, pad = 10)

During this step, I also wondered whether the y label “Frequency” was necessary. After testing both variants, I found having an y label more confusing than helpful. If no other information is given, I think we intuitively read the y axis as “Frequency”.

ax.set_ylabel("")

If we applied all of the above, we would get this plot:

Plot with emphasized information (1/3)

At this point, the visualization is ready for your presentation or report. However, there is one more thing we could do to make the plot more interpretable. In principle, histograms hold all the information we need to display percentiles. Unfortunately, it is impossible to read the information directly from the plot. What if we want to show our boss what the 75th percentile of avocado prices is while keeping all the information about the distribution? We could compute some percentiles and display them in the plot as vertical lines. This is similar to what a boxplot would do, but actually integrated into the histogram. Let’s try this!

First, we want to plot the vertical lines. For this, we’ll calculate the 5th, 25th, 50th, 75th, and 95th percentiles of the distribution. One way to do it would be to make every vertical line a bit longer than the previous one in a kind of stairwise motion. We can also give the inner lines a higher opacity than the outer lines.

To do this, we’ll store the quantiles, line opacities and line lengths in a list of lists. This will allow us to loop through this list and plot all lines automatically.

Plot with emphasized information (2/3)

Now, we need to add labels. Just like before, we can use different opacities and in this case font sizes to reflect the density of the distribution. Each text should have a little offset compared to the percentile lines for better readability.

Plot with emphasized information (3/3)

We can now use this histogram to make business decisions on at which price to sell our avocados. Maybe our avocados are a bit better than the average avocado and our company is rather well-known for their avocados. Maybe we should charge the price that is at the 75th percentile of the distribution (around 1.65$)? This histogram is a combination of histogram and boxplot, in a way.

This is the entire code for the final histogram: