Create professional plots using Matplotlib

towards-data-science

This post was originally published by Rizky Maulana Nurhidayat at Towards Data Science

Figure 8. Tick direction setting in Matplotlib (Image by Author).

  • Adjust major and minor tick size

In Figure 8, I think you can not see the difference between the three plots because the tick’s size is too small. How to make it bigger? You can change it using this code for the x-axis and y-axis.

plt.rcParams['xtick.major.size'] = 5.0
plt.rcParams['xtick.minor.size'] = 3.0plt.rcParams['ytick.major.size'] = 5.0
plt.rcParams['ytick.minor.size'] = 3.0

Matplotlib gives you xtick and ytick major size of 3.5 and for minor tick is 2.0. To show the minor tick, you can use this code.

from matplotlib.ticker import MultipleLocatorax.xaxis.set_minor_locator(MultipleLocator(.5))
ax.yaxis.set_minor_locator(MultipleLocator(.005))

Of course, you need to define ax using

fig, ax = plt.subplots(figsize=(xsize, ysize))

or

ax = plt.gca()

Here is the code to figure with configuring the minor tick

Code to configure major and minor tick.

If you run the code, you will get a figure, as shown in Figure 9.

Figure 9. Adjust the major and minor tick in Matplotlib (Image by Author).

  • Adjust line width

In this part, I will share with you how to adjust the line width of the axes. You can use this code to adjust it.

plt.rcParams['axes.linewidth'] = 3.0

By default, the line width is set to 0.8, given in pts. I set it in 3.0 to feel the difference between the default setting and the updated version. If you add the code above in Figure 9, it will give the result shown in Figure 10.

Figure 10. Adjust the line width in Matplotlib (Image by Author).

As you see in Figure 10, the now minor tick is not visible because the minor tick size is smaller than the line width.

  • Adjust handle length

In the last part, I will demonstrate how to adjust the handle length and the distance between the text legend and its symbol. I think you will know more if you run the following code and analyze the result, as shown in Figure 11.

plt.rcParams['legend.handlelength'] = 5.0

Figure 11. Adjust legend handle length in Matplotlib (Image by Author).

You can see the difference in the legend box. I set it to 5.0 so that you can see the difference clearly.

2. Create a colorblind-friendly palette

Color blindness (or color blindness — or more specific color vision deficiency (CVD)) is well known but hard to imagine if you are not suffering. Before I know the data from https://www.color-blindness.com/, I did not realize that people who have color blindness is not small, approximately every 1 in 12 men (8%) and 1 in 200 women (0.5%). So, if you want to generate figures/plots, please ensure it is color-blind friendly.

There are four types of color blindness, they are Protan Color Blindness, Deutan Color Blindness, Tritan Color Blindness / Tritanomaly, and Monochromacy and Achromatopsia. Detail information for each type can be accessed here.

One of the safest solutions you can use to deal with color blind is to avoid using red and green together. For simply, you can use the service from Gregor Aisch in https://gka.github.io/palettes. You can generate many palettes, which is color-blind friendly. To use it, first, you need to choose what palettes type you want to create. There are two options, sequential and diverging, as shown in Figure 12.

Figure 12. Generate color-blind friendly palettes (https://gka.github.io/palettes).

After that, in the same section (section number 1), you need to declare how many colors you want to generate. In this story, I will choose diverging colors with the number of colors is 5.

In section 2, you can fill it with several colors you want in the Hex color code. If you just know the colors you want in RGB, you can transform it using this link. I choose these colors for the left panel.

#0051a2, #10542c, #ffd44f

and these colors for the right panel.

lightyellow, #ff005e, #93003a

In section 3, check correct lightness and bezier interpolation. If your choice colors are color-blind friendly, you will get the information “The palette is color-blind safe” in the right panel of section 3, as shown in Figure 13.

Figure 13. A color-blind friendly palette from https://gka.github.io/palettes.

Then, scroll down in section 4, “Export the color codes in various formats.” You can pick the color code to generate from the colors in section 3. Here is the color code I got.

['#0051a2', '#97964a', '#ffd44f', '#f4777f', '#93003a']

How to apply it? Just declare it as your color in your plots. Here is the example of my plot, shown in Figure 14.

Figure 14. Applying color-blind friendly in Matplotlib (Image by Author).

It’s still pretty and, of course, color-blind friendly. You can vary the colors you want to generate. Please ensure that you get the notification that says The palette is color-blind safe in section 2.

You can see the example of my chosen colors that is not color-blind friendly.

Figure 15. The unfriendly color blind palettes (https://gka.github.io/palettes).

If you got a notification like that, please change the colors to help the people who have color blindness.

Conclusion

To boost your skills in data visualization using Matplotlib, you need to make a plotting template. It can be built by choosing your right plotting style, adjusting some basic parameters using rcParams, and choosing the color-blind friendly palettes. I hope this story can help you create the professional plots considering some information I mentioned before.

If you are interested to learn more about data visualization techniques using Matplotlib, you can visit my profile here, and don’t forget to follow. By following me, you can get the notification when I post a story.

That’s all. Thank you.

References

[1] Oguri, M et al., (2012) MNRAS Volume 420, Issue 4, March 2012, Pages 3213–3239, https://doi.org/10.1111/j.1365-2966.2011.20248.x

[2] MNRAS, General Instruction for Submission in MNRAS https://academic.oup.com/mnras/pages/General_Instructions

[3] Color Blindness https://www.color-blindness.com/

[4] Color-blind friendly palettes https://gka.github.io/palettes

[5] Color code transformation https://html-color-codes.info/convert-color-format/

Spread the word

This post was originally published by Rizky Maulana Nurhidayat at Towards Data Science

Related posts