nee*_*hiv 217 python plot visualization matplotlib
我正在学习matplotlib通过学习示例来学习,并且在创建单个绘图之前,许多示例似乎包括如下所示的行...
fig, ax = plt.subplots()
Run Code Online (Sandbox Code Playgroud)
这里有些例子...
我看到这个函数用了很多,尽管这个例子只是试图创建一个图表.还有其他一些优势吗?官方演示subplots()也用于f, ax = subplots创建单个图表时,它之后只引用了ax.这是他们使用的代码.
# Just a figure and one subplot
f, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')
Run Code Online (Sandbox Code Playgroud)
jon*_*har 273
plt.subplots()是一个返回包含图形和轴对象的元组的函数.因此,在使用时fig, ax = plt.subplots(),将此元组解压缩到变量fig和ax.有fig,如果你想改变人物级别的属性或保存数字作为以后的图像文件是非常有用的(例如用fig.savefig('yourfilename.png').你当然不必使用返回的数字对象,但因此它经常可以看到很多人都用到它.此外,所有轴对象(具有绘图方法的对象)都具有父图形对象,因此:
fig, ax = plt.subplots()
Run Code Online (Sandbox Code Playgroud)
比这更简洁:
fig = plt.figure()
ax = fig.add_subplot(111)
Run Code Online (Sandbox Code Playgroud)
Dus*_*ash 38
这里只是一个补充.
以下问题是如果我想在图中有更多的子图?
正如文档中所提到的,我们可以使用fig = plt.subplots(nrows=2, ncols=2)在一个图形对象中设置一组带有网格(2,2)的子图.
然后我们知道,fig, ax = plt.subplots()返回一个元组,让我们fig, ax1, ax2, ax3, ax4 = plt.subplots(nrows=2, ncols=2)先尝试一下.
ValueError: not enough values to unpack (expected 4, got 2)
Run Code Online (Sandbox Code Playgroud)
它引发了一个错误,但不用担心,因为我们现在看到它plt.subplots()实际上返回了一个包含两个元素的元组.第一个必须是一个图形对象,另一个应该是一组子图对象.
那么让我们再试一次:
fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots(nrows=2, ncols=2)
Run Code Online (Sandbox Code Playgroud)
并检查类型:
type(fig) #<class 'matplotlib.figure.Figure'>
type(ax1) #<class 'matplotlib.axes._subplots.AxesSubplot'>
Run Code Online (Sandbox Code Playgroud)
当然,如果你使用参数(nrows = 1,ncols = 4),那么格式应该是:
fig, [ax1, ax2, ax3, ax4] = plt.subplots(nrows=1, ncols=4)
Run Code Online (Sandbox Code Playgroud)
所以请记住保持列表的构造与我们在图中设置的子图网格相同.
希望这对你有所帮助.
小智 8
除了上面的答案之外,您还可以检查使用type(plt.subplots())which 返回元组的对象类型,另一方面,type(plt.subplot())返回matplotlib.axes._subplots.AxesSubplot您无法解包的返回。
使用plt.subplots()很受欢迎,因为它为您提供了 Axes 对象并允许您使用 Axes 接口来定义绘图。
另一种方法是使用全局状态接口plt.plot等功能:
import matplotlib.pyplot as plt
# global state version - modifies "current" figure
plt.plot(...)
plt.xlabel(...)
# axes version - modifies explicit axes
ax.plot(...)
ax.set_xlabel(...)
Run Code Online (Sandbox Code Playgroud)
那么为什么我们更喜欢斧头呢?
全局状态版本以这种方式创建,以便于交互使用,并且成为 Matlab 用户熟悉的界面,但在较大的程序和脚本中,此处概述的要点倾向于使用 Axes 界面。
有一篇 matplotlib 博客文章更深入地探讨了这个主题:Pyplot vs Object Oriented Interface
处理两个世界相对容易。例如,我们总是可以询问当前轴:(ax = plt.gca()“获取当前轴”)。
作为补充的问题和答案,上面也有一个重要区别plt.subplots()和plt.subplot(),通知失踪's'底。
可以plt.subplots()一次制作所有子图,然后将子图的图形和轴(复数轴)作为元组返回。可以将图形理解为在其中绘制草图的画布。
# create a subplot with 2 rows and 1 columns
fig, ax = plt.subplots(2,1)
Run Code Online (Sandbox Code Playgroud)
而plt.subplot()如果要单独添加子图,则可以使用。它仅返回一个子图的轴。
fig = plt.figure() # create the canvas for plotting
ax1 = plt.subplot(2,1,1)
# (2,1,1) indicates total number of rows, columns, and figure number respectively
ax2 = plt.subplot(2,1,2)
Run Code Online (Sandbox Code Playgroud)
但是,plt.subplots()首选,因为它为您提供了更轻松的选项来直接自定义整个图形
# for example, sharing x-axis, y-axis for all subplots can be specified at once
fig, ax = plt.subplots(2,2, sharex=True, sharey=True)
Run Code Online (Sandbox Code Playgroud)
但是,使用时plt.subplot(),必须为每个轴分别指定,这可能会很麻烦。