The world’s leading publication for data science, AI, and ML professionals.

Clear charts with Matplotlib

How to build Matplotlib charts that people actually want to read and use.

Photo by Isaac Smith on Unsplash
Photo by Isaac Smith on Unsplash

Data science is a lot about convincing people, showing what you have found, what patterns you actually understood. Charting comes a long way in this exercise. Sadly many scientific libraries allow you to produce Charts out of the box such as the one below.

Barplot from matplotlib documentation (1)
Barplot from matplotlib documentation (1)

Are your eyes drawn to the chart with a sense of curiosity? Personally, mine are not and more importantly, I think the non-technical, general public will dismiss it rather fast as you have to make a conscious effort to go beyond the visual lack of clarity.

Most will dismiss the importance of aesthetics, but I really think it is one of the connecting bridges between "tech" and "business" people.

It helps to better communicate.

Charts are about what you want to show, what is the most important information that a reader should extract? Is it the axis? You can hardly miss the dark frame and lines. If you want to read the bar value, it is stacked between the error bar lines. The important information is hidden.

In this post, I am taking the example of bar plots pretending that we measured some score in different groups of people. The required code is shared at the end of the post.

In a bar plot, what I want to clearly see by decreasing order of importance are:

  1. the bars and their respective proportions,
  2. the title,
  3. the scales,
  4. and a sense of their values and unit.

You can visually communicate this order. The most important element should be the first you see. Below, I suggest one option to go into that direction. The bars have a strong colour that makes them visible and the title and axis legends have a medium grey that is less marked as it is secondary information. Finally the horizontal grid lines are extremely light just to help a little if you want to read the values. Nothing extraordinary, it takes an hour the first time but then it is mostly about copy/pasting a configuration you are happy about.

plot_1.png (code at the end of the post)
plot_1.png (code at the end of the post)

A word about colours. Do not use the default colours. Just don’t.

Nowadays you have plenty of tools to actually select colour palettes that are harmonious, that work well together without knowing a thing about the theory. For example, the colours that I choose here were selected on an online tool (coolors.co), but most likely any of these tools will do a fair job. If you want to be sensitive to colours blinds, special palettes exist that work for everyone (e.g. davidmathlogic.com/colorblind/). If the plot below had the red for men and blue for women, it would (sadly) surprise many of us. Colours are associated with meanings and emotions, use them to clarify your Communication.

plot_2.png (code at the end of the post)
plot_2.png (code at the end of the post)

In conclusion, designing charts that will bring valuable information without cluttering the eye, boils down to a few core principles:

  • Every shape that is present on a chart has a purpose, a meaning. If not, remove it (or hide it in white/transparent if you have to).
  • Choose the colours instead of letting the library doing it for you.
  • Choose a font size that everybody can read (no, that is not 6pt). Decrease the plot area and increase the font size if the chart will have a final smaller size. There is no point to label something that can’t be read.
  • We are in 2020, use resolutions that matches with our current screens quality (here, I specified 300 dpi or use vector graphics).

If you define how the chart looks, you can reproduce it and be consistent. Otherwise you are controlled by the tool you use and you are not telling a story.

It is really simple, but I keep seeing cluttered and hard to read charts. Next, you might want to think about the visuals that you normally produce. As you would not voice or write words without meaning or purpose, you could apply the same logic to your charts.

Code:

Sources :

[1] https://matplotlib.org/2.0.2/examples/api/barchart_demo.html


Related Articles