Originally published at dev.to and modified a bit to fit Medium’s editing system.

It’s true that matplotlib is a fantastic visualizing tool in python. But it’s also true that tweaking details in matplotlib is a real pain. You may easily lose hours to find out how to change a small part of your plot. Sometimes you even don’t know the name of the part, which makes googling it harder. Even if you find a hint on Stack Overflow, you may spend another couple of hours to make it fit your case. These non-rewarding tasks can be avoided by knowing what a figure in matplotlib consists of and what you can do with them. Like most of you, I think, I have overcome my plotting problems by reading lots of answers by matplotlib gurus on Stack Overflow. Recently, I noticed that an official tutorial about Artist objects is very informative and helpful to understand what is going on when we plot with matplotlib and to reduce a large amount of time spent for tweaking. In this post, I would like to share some basic knowledge about Artist objects in matplotlib which would prevent you from spending hours for tweaking.

Purpose of thispost

I’m not going to write about how-tos like “do like this when you want to do this”, but a basic concept of Artist in matplotlib which helps you choose suitable search queries and arrange a solution for a similar problem as yours. After reading this, you'll probably understand those huge amount of recipes on the Internet more clearly. This also applies to those who use seaborn and plotting features of pandas which are wrappers of matplotlib.

Contents

This post is basically an English version of the original article I wrote in Japanese , and is mostly based on Artist tutorial and Usage Guide (2.1.1 at the time of publication of the original one)

Who is thisfor?

Matplotlib users who

are able to make plots if needed, but often struggle to make them appropriate for publication or presentation (and are irritated by “the last one mile” to what you really want). have successfully found an exact solution on Stack Overflow but are still hazy about how it works and cannot apply it to other problems. found multiple hints for a problem but are not sure which to follow. Environment Python 3.6 matplotlib 2.2 %matplotlib inline import matplotlib.pyplot as plt import numpy as np

plt.show() is omitted in this article because I use Jupyter notebook's inline plot.

Two plotting styles you should be awareof

Before looking into Artist objects, I would like to mention the difference between plt.plot and ax.plot , or Pyplot and object-oriented API. While object-oriented API style is officially recommended, there are still lots of examples and code snippets using Pyplot style, including official docs. Some are even mixing both styles meaninglessly, which causes unnecessary confusion for beginners. Since official doc has good notes about them such as A note on the Object-Oriented API vs Pyplot and Coding Styles , here I only make some comments on them. If you look for introduction for them, I recommend official tutorials.

Object-oriented API interface

This is the recommended style which often starts with fig, ax = plt.subplots() or other equivalents followed by ax.plot , ax.imshow etc. fig and ax are, in fact, Artist s. Here are some simplest examples.

# example 1 fig, ax = plt.subplots() ax.plot(x,y) # example 2 fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.plot(x, y)

Some tutorials use fig = plt.gcf() and ax = plt.gca() . These should be used when you switch from Pyplot interface to OO interface, but some Pyplot-based codes include, for example, pointless ax = plt.gca() which is apparently copied from OO-based code without understanding. Using plt.gcf() or plt.gca() isn't a bad thing at all if one switch the interface intentionally. Considering implicit switching can be a cause of confusion for beginners, using plt.subplots or fig.add_subplot from the beginning would be the best practice for most of the cases if they are publicly available.

Pyplot interface

This is a MATLAB-user-friendly style in which everything is done with plt.*** .

# <a href="https://matplotlib.org/tutorials/introductory/pyplot.html" data-href="https://matplotlib.org/tutorials/introductory/pyplot.html" rel="noopener" target="_blank">https://matplotlib.org/tutorials/introductory/pyplot.html</a> def f(t): return np.exp(-t) * np.cos(2*np.pi*t) t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02) plt.figure(1) plt.subplot(211) plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') plt.subplot(212) plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.show()
“Artist” in Matplotlib―something I wanted to know before spending tremendous  ...

At first, it seems very simple because there is no need to think about which objects you are handling. You only need to know which “state” you are in, which is why this style is also called “stateful interface”. Here, a “state” means which figure and subplot you are currently in. As you see in Pyplot tutorial , it gives a nice figure if your plot is not so complicated. Although Pyplot interface offers lots of functions to change plot settings, you may reach its limit within a couple of hours, days, months (or never if you are lucky enough) depending on what you want to do. At this stage, you need to switch to OO interface. That is why I recommend to use OO interface from the beginning. But Pyplot is still useful for quick checks or any occasions where you need rough plots.

The hierarchy in matplotlib

After googling several times, you will notice that matplotlib has a hierarchical structure consisting of something often called fig and ax . Old doc for matplotlib 1.5 has a nice image explaining this.


“Artist” in Matplotlib―something I wanted to know before spending tremendous  ...
Actually, these three components are special Artist s called "containers" (and there is the fourth container Tick ) which we wi

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

分页:12
转载请注明
本文标题:“Artist” in Matplotlib―something I wanted to know before spending tremendous ...
本站链接:https://www.codesec.net/view/620864.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(11)