How Many Ways To Call Axes In Matplotlib

栏目: IT技术 · 发布时间: 4年前

内容简介:AlthoughLet’s say you just decide to make plots usingIn this post, I will summarise the differences of these functions (i.e.

Although matplotlib is extremely powerful and the only limitation might be our imagination, it is a bit challenging for new users to find the right path as there are always more than one way to achieve the same goal in matplotlib . Calling axes is one of them.

Let’s say you just decide to make plots using object-oriented interface ( aka artist layer plotting ) in matplotlib . However, I bet you will be soon running into problems when trying to instantiate axes to start your plotting. You may find that plt.subplots() and plt.subplot() both can return you axes . Yes, they only differ by one letter ‘s’. Later, you notice that plt.axes() can call axes as well. It is not over yet, you highly likely will encounter the following functions fig.add_subplot() , fig.add_axes() , and fig.subplots() , which are all able to create axes for you. I totally understand how panic you are or will be as I have had the same confusion in learning matplotlib .

In this post, I will summarise the differences of these functions (i.e. plt.subplot() , plt.subplots() , plt.axes() , fig.add_subplot() , fig.subplots() , and fig.add_axes() ) and demonstrate how to use them accordingly.

In matplotlib terminology, a basic plot starts from one figure and at least one axes (if you are confused about these terms, you may find this post is useful). A close analogy with painting, figure is the canvas and axes is the artistic composition. A canvas ( figure ) can have only one type or many different types of artistic composition ( axes ) on it. In light of this, let’s first start from a simple case for figure with only one axes , then dive into figure with multiple axes .

figure with only one axes

In most circumstances, we instantiate a fig object by calling fig = plt.figure(…) and then add an axes object to the fig by calling ax = fig.add_subplot() . By default, if leave the parentheses empty, this function is passed with fig.add_subplot(1, 1, 1) , which means add # 1 axes in a 1 row by 1 column axes grid. We can also use an alternative format with 3-digit integer without comma (i.e. fig.add_subplot(111) ). However, with the 3-digit integer format, each integer cannot be greater than 9. Using this function, we add only one axes at a time.

# Create a figure
fig = plt.figure()
# Add a subplot
ax = fig.add_subplot()

# Equivalent method
ax = fig.add_subplot(111)
# Another equivalent but more general method
ax = fig.add_subplot(1, 1, 1)

Sometimes, we are able to add an axes object using ax = fig.subplots() . This function is originally devised to make it convenient to create a set of subplots in common layouts by a single call. It takes nrows and ncols as positional arguments (i.e. this function is supposed to add multiple axes at a time). However, if we leave the parentheses empty, this function is passed with fig.subplots(1, 1) by default, which means create an axes grid with 1 row by 1 column (i.e. one axes on fig ). Hence the axes only contain one item in it (i.e. len(ax) is 1).

# Create a figure
fig = plt.figure()

# Create a subplot
ax = fig.subplots()
# Equivalent method
ax = fig.subplots(1, 1)

Rarely, as for figure with one plot, we may also use ax = fig.add_axes([left, bottom, width, height]) to add an axes onto a fig . This function enables arbitrary layouts of axes on fig by taking the dimensions ( [left, bottom, width, height] ) of the new axes (you can find an example here ). All four numbers should be in fractions of figure width and height. In other words, we control the position and size of the axes . fig.add_axes([0, 0, 0.78, 0.78]) creates the same size plot as by fig.add_subplot(111) and fig.subplots(1, 1) . As you can see here, axes is not in the same size as figure . There is white space between axes and figure by default.

# Create a figure
fig = plt.figure()
# Add a subplot 
ax = fig.add_axes([0, 0, 0.78, 0.78])

After demonstrating the differences of the above three functions, it is fairly easy to understand plt.subplot() , plt.subplots() , and plt.axes() . They are from state-based interface (aka scripting layer plotting) of matplotlib , which just wrapped fig = plt.figure() with fig.add_subplot() , fig.subplots() , and fig.add_axes() , correspondingly.

plt.subplot() returns only one axes and create a figure object automatically. You can change figure -level attributes or save the figure by calling ax.figure.xxx() instead. For example, ax.figure.savefig('example.png') will save the figure as example.png .

# Creates just a figure and only one subplot
ax = plt.subplot()
# Equivalent method
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

plt.subplots() returns a tuple containing a figure and axes object(s). We need to unpack this tuple and assign its items to variables fig and ax , respectively.

# Creates just a figure and only one subplot
fig, ax = plt.subplots()
# Equivalent method
fig = plt.figure()
ax = fig.subplots(1, 1)

plt.axes() returns one axes using fig.add_subplot(1, 1, 1) when no argument is provided and, just like plt.subplot() , it creates a figure object automatically. plt.axes() also accept dimensions ( [left, bottom, width, height] ) using fig.add_axes([left, bottom, width, height]) in its backend. The following two sets of code create the same plots.

# Set one
# Creates just a figure and only one subplot
ax = plt.axes()
# Equivalent method
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
-------------------------------------------
# Set two
# Creates just a figure and only one subplot
ax = plt.axes([0, 0, 0.78, 0.78])
# Equivalent method
fig = plt.figure()
ax = fig.add_axes([0, 0, 0.78, 0.78])

figure with multiple axes

When it comes to plotting multiple axes on figure , the differences among these functions (i.e. plt.subplot() , plt.subplots() , plt.axes() , fig.add_subplot() , fig.subplots() , and fig.add_axes() ) become manifest. Again, let’s demonstrate this using examples. Since plt.subplot() , plt.subplots() , and plt.axes() are essentially the same as fig.add_subplot() , fig.subplots() , and fig.add_axes() , I will only show examples for the later three functions.

As stated above, we add one axes at a time when using fig.add_subplot() . The build-in order of axes is started from left to right and from top to bottom within the axes grid (Figure 1). How Many Ways To Call Axes In Matplotlib

The same layout could be obtained using fig.subplots() with one line of code (Figure 2). It returns a ndarray object axs (I add a ‘s’ after ax to show that it contains multiple objects). Just like an 2D array object, each ax within axs can be accessed using axs[x, y] as well (i.e. ax1 can be accessed using axs[0, 0] ). Moreover, if you like to assign names to each ax within axs , you can use tuple unpacking like this, ((ax1, ax2), (ax3, ax4)) = fig.subplots(nrows=2, ncols=2) . Flattening of 2D array is another method to access ax from axs as shown in Figure 2. How Many Ways To Call Axes In Matplotlib

Normally, fig.add_axes() is used for arbitrary layout, such as a plot with inlet subplots or subplots overlap with each other. This is because we can fine-tune the position and size of each subplot by its dimensions ( [left, bottom, width, height] ). Having said that, we still can make a common layout as shown in Figure 1 and 2 using fig.add_axes() (Figure 3). How Many Ways To Call Axes In Matplotlib

In summary, these six functions (concisely should be three functions, i.e. fig.add_subplot() , fig.subplots() , and fig.add_axes() ) can be used interchangeably when you are making a figure with only one axes . However, they have respective strengths and can be used individually or in combination when it comes to making figure with multiple axes in different layouts.

After reading this post, I hope you have a clearer understanding about how to call axes in matplotlib . All the codes in this post can be accessed from jupyter notebook . If you know other functions/cases to call axes which are not listed here, please feel free to leave your comment. Let’s learn matplotlib together.

As always, I welcome feedback, constructive criticism, and hearing about your data science projects. I can be reached on Linkedin , and now on mywebsite as well.


以上所述就是小编给大家介绍的《How Many Ways To Call Axes In Matplotlib》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C语言算法速查手册

C语言算法速查手册

程晓旭、耿鲁静、张海、王勇 / 2009-10 / 49.00元

《C语言算法速查手册》用C语言编写了科研和工程中最常用的166个算法,这些算法包括复数运算、多项式的计算、矩阵运算、线性代数方程组的求解、非线性方程与方程组的求解、代数插值法、数值积分法、常微分方程(组)初值问题的求解、拟合与逼近、特殊函数、极值问题、随机数产生与统计描述、查找、排序、数学变换与滤波等。同时结合这些算法列举了将近100个应用实例,对其进行验证和分析。 《C语言算法速查手册》适......一起来看看 《C语言算法速查手册》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

随机密码生成器
随机密码生成器

多种字符组合密码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具