Python 如何在 matplotlib 中获得多个子图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31726643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:28:25  来源:igfitidea点击:

How do I get multiple subplots in matplotlib?

pythonmatplotlibsubplot

提问by bugsyb

I am a little confused about how this code works:

我对这段代码的工作方式有点困惑:

fig, axes = plt.subplots(nrows=2, ncols=2)
plt.show()

How does the fig, axes work in this case? What does it do?

在这种情况下,无花果轴如何工作?它有什么作用?

Also why wouldn't this work to do the same thing:

还有为什么这不能做同样的事情:

fig = plt.figure()
axes = fig.subplots(nrows=2, ncols=2)

采纳答案by Diziet Asahi

There are several ways to do it. The subplotsmethod creates the figure along with the subplots that are then stored in the axarray. For example:

有几种方法可以做到。该subplots方法创建图形以及随后存储在ax数组中的子图。例如:

import matplotlib.pyplot as plt

x = range(10)
y = range(10)

fig, ax = plt.subplots(nrows=2, ncols=2)

for row in ax:
    for col in row:
        col.plot(x, y)

plt.show()

enter image description here

在此处输入图片说明

However, something like this will also work, it's not so "clean" though since you are creating a figure with subplots and then add on top of them:

但是,这样的事情也可以工作,但它并不是那么“干净”,因为您正在创建一个带有子图的图形,然后在它们之上添加:

fig = plt.figure()

plt.subplot(2, 2, 1)
plt.plot(x, y)

plt.subplot(2, 2, 2)
plt.plot(x, y)

plt.subplot(2, 2, 3)
plt.plot(x, y)

plt.subplot(2, 2, 4)
plt.plot(x, y)

plt.show()

enter image description here

在此处输入图片说明

回答by Diziet Asahi

read the documentation: matplotlib.pyplot.subplots

阅读文档:matplotlib.pyplot.subplots

pyplot.subplots()returns a tuple fig, axwhich is unpacked in two variables using the notation

pyplot.subplots()返回一个元组fig, ax,该元组使用符号解包为两个变量

fig, axes = plt.subplots(nrows=2, ncols=2)

the code

编码

fig = plt.figure()
axes = fig.subplots(nrows=2, ncols=2)

does not work because subplots()is a function in pyplotnot a member of the object Figure.

不起作用,因为subplots()pyplot不是对象的成员中的函数Figure

回答by ImportanceOfBeingErnest

You might be interested in the fact that as of matplotlib version 2.1 the second code from the question works fine as well.

您可能对这样一个事实感兴趣,即从 matplotlib 2.1 版开始,问题中的第二个代码也能正常工作。

From the change log:

更改日志

Figure class now has subplots method The Figure class now has a subplots() method which behaves the same as pyplot.subplots() but on an existing figure.

Figure 类现在有 subplots 方法 Figure 类现在有一个 subplots() 方法,其行为与 pyplot.subplots() 相同,但在现有图形上。

Example:

例子:

import matplotlib.pyplot as plt

fig = plt.figure()
axes = fig.subplots(nrows=2, ncols=2)

plt.show()

回答by Khalil Al Hooti

import matplotlib.pyplot as plt

fig, ax = plt.subplots(2, 2)

ax[0, 0].plot(range(10), 'r') #row=0, col=0
ax[1, 0].plot(range(10), 'b') #row=1, col=0
ax[0, 1].plot(range(10), 'g') #row=0, col=1
ax[1, 1].plot(range(10), 'k') #row=1, col=1
plt.show()

enter image description here

在此处输入图片说明

回答by Colin Anthony

  • You can also unpack the axes in the subplots call

  • And set whether you want to share the x and y axes between the subplots

  • 您还可以在 subplots 调用中解压轴

  • 并设置是否要在子图之间共享 x 和 y 轴

Like this:

像这样:

import matplotlib.pyplot as plt
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True)
ax1.plot(range(10), 'r')
ax2.plot(range(10), 'b')
ax3.plot(range(10), 'g')
ax4.plot(range(10), 'k')
plt.show()

enter image description here

在此处输入图片说明