- Object-oriented way of using matplotlib – 1. The Reasons
- Object-oriented way of using matplotlib – 2. MATLAB style versus OO style
- Object-oriented way of using matplotlib – 3. Basics of Object Oriented APIs
- Object-oriented way of using matplotlib – 4. Multiple Subplots
- Object-oriented way of using matplotlib – 5. Customizing Plots, Legend
- Object-oriented way of using matplotlib – 5. Customizing Plots, Ticks
- Object-oriented way of using matplotlib – 5. Customizing Plots, Colorbar
- Object-oriented way of using matplotlib – 6. Artist and Useful Links
I would like to post a series of articles about object-oriented way of using matplotlib. That explains why we should use that way and how to code like that. The main references are come from the official matplotlib website, “matplotlib.org” and the book, 『Python Data Science Handbook: Essential Tools for Working with Data, Jake VanderPlas, O’REILLY, 2017』.
In the previous posts, we’ve tasted some differences between MATLAB styles and OO styles. We could learn how to add subplots in different ways. In this post, you can learn how to customize the various stuffs of a plot such as legend, tick and colorbar.
5. Customizing Plots
c. Customizing Colorbar
The Colorbar is a very popular element in scatter plot or heatmap plot. A colorbar can be treated as an axes object so that one should notice that it can be aligned with other axes objects. If you want to get the color information from the plot, you should get the plot handle at first, then you should designate the handle and its corresponding axes object ( axes of subplot ) when calling fig.colorbar() as a mappable object for colorbar range. In OO style, fig.colorbar() is used rather than plt.colorbar().
ⅰ. Colorbar Replacements
There are plenty of ways to locate colobars in a figure. You can find a way to do that in below example. The full source is here. [Link]
ⅱ. Customizing Colorbar
To call fig.colorbar(), you should give it to two arguments that one is mappable object and the other is axes corresponding to its colorbar object. As you have seen in the above example, colorbar can be shared with several axes objects together. To clip the range of colorbar, you can call set_clim() method with mappable.
Here are some example for customizing colorbar.
The full source code is avaiable. [Link]
We have seen several customizing properties. Let’s move to the last article of this series. It will show the way of adding artist and some applications.