Python 在 matplotlib 中使用绘图、轴或图形绘制绘图有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37970424/
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
What is the difference between drawing plots using plot, axes or figure in matplotlib?
提问by hashcode55
I'm kind of confused what is going at the backend when I draw plots in matplotlib, tbh, I'm not clear with the hierarchy of plot, axes and figure. I read the documentation and it was helpful but I'm still confused...
当我在 matplotlib 中绘制绘图时,我对后端发生的事情感到困惑,tbh,我不清楚绘图、轴和图形的层次结构。我阅读了文档,它很有帮助,但我仍然感到困惑......
The below code draws the same plot in three different ways -
下面的代码以三种不同的方式绘制相同的图 -
#creating the arrays for testing
x = np.arange(1, 100)
y = np.sqrt(x)
#1st way
plt.plot(x, y)
#2nd way
ax = plt.subplot()
ax.plot(x, y)
#3rd way
figure = plt.figure()
new_plot = figure.add_subplot(111)
new_plot.plot(x, y)
Now my question is -
现在我的问题是——
What is the difference between all the three, I mean what is going under the hood when any of the 3 methods are called?
Which method should be used when and what are the pros and cons of using any on those?
这三个方法之间有什么区别,我的意思是当调用这 3 个方法中的任何一个时,幕后会发生什么?
什么时候应该使用哪种方法,在这些方法上使用 any 的利弊是什么?
采纳答案by Essex
Method 1
方法一
plt.plot(x, y)
This lets you plot just one figure with (x,y) coordinates. If you just want to get one graphic, you can use this way.
这使您可以仅绘制一个具有 (x,y) 坐标的图形。如果你只想得到一个图形,你可以使用这种方式。
Method 2
方法二
ax = plt.subplot()
ax.plot(x, y)
This lets you plot one or several figure(s) in the same window. As you write it, you will plot just one figure, but you can make something like this:
这使您可以在同一窗口中绘制一个或多个图形。在编写它时,您将只绘制一个图形,但您可以制作如下内容:
fig1, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
You will plot 4 figures which are named ax1, ax2, ax3 and ax4 each one but on the same window. This window will be just divided in 4 parts with my example.
您将绘制 4 个名为 ax1、ax2、ax3 和 ax4 的图形,每个图形都在同一窗口上。在我的示例中,这个窗口将被分为 4 个部分。
Method 3
方法三
fig = plt.figure()
new_plot = fig.add_subplot(111)
new_plot.plot(x, y)
I didn't use it, but you can find documentation.
我没有使用它,但您可以找到文档。
Example:
例子:
import numpy as np
import matplotlib.pyplot as plt
# Method 1 #
x = np.random.rand(10)
y = np.random.rand(10)
figure1 = plt.plot(x,y)
# Method 2 #
x1 = np.random.rand(10)
x2 = np.random.rand(10)
x3 = np.random.rand(10)
x4 = np.random.rand(10)
y1 = np.random.rand(10)
y2 = np.random.rand(10)
y3 = np.random.rand(10)
y4 = np.random.rand(10)
figure2, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
ax1.plot(x1,y1)
ax2.plot(x2,y2)
ax3.plot(x3,y3)
ax4.plot(x4,y4)
plt.show()
Other example:
其他例子:
回答by gboffi
The names of objects
对象名称
Matplotlib is strongly object oriented and its principal objects are the figureand the axes(I find the name axes
a bit misleading, but probably it's just me).
Matplotlib 是强烈的面向对象,它的主要对象是图形和轴(我觉得这个名字axes
有点误导,但可能只是我)。
You can think of the figureas a canvas, of which you typically specify the dimensions and possibly e.g., the background color etc etc. You use the canvas, the figure, essentially in two ways, placing other objects on it (mostly axes, but also text labels etc) and saving its contents with savefig
.
您可以将图形视为画布,您通常会指定其尺寸,例如,背景颜色等。您可以使用画布、图形,基本上以两种方式使用,在其上放置其他对象(主要是轴,但还有文本标签等)并使用savefig
.
You can think of an axesas a sort of Swiss Army knife, a handy object that offers a tool (e.g. .plot
, .scatter
, .hist
etc) for everything, mostly. You can place one, two, ... many axesinside a figureusing one of many different methods.
你可以把中轴线作为一种瑞士军刀,一个方便的对象,提供了一个工具(例如.plot
,.scatter
,.hist
等)的一切,大部分。您可以使用多种不同方法之一在图形中放置一个、两个、...多个轴。
The plt
interface
该plt
接口
The pltprocedural interface was originally developed to mimic the MATLAB? interface but is not really different from the object oriented interface, even if you don't make a direct reference to the main objects (i.e., a figureand an axes) these objects are automatically instantiated and each pltmethod is, essentially, translated to a call of one of the methods of the underlying fundamental objects: e.g., a plt.plot()
is a hidden_axes.plot
and a plt.savefig
is a hidden_figure.savefig
.
该PLT程序接口最初是模仿MATLAB?界面,但与面向对象的界面并没有真正不同,即使您不直接引用主要对象(即图形和轴),这些对象也会自动实例化,并且每个plt方法基本上都转换为对底层基本对象的方法之一的调用:例如, aplt.plot()
是 ahidden_axes.plot
并且 aplt.savefig
是 a hidden_figure.savefig
。
In every moment you can have an handle on these hidden objects using plt.gcf
and plt.gca
, and this is sometimes necessary when one of the object methods has not been ported to a method in the pltnamespace.
在任何时候,您都可以使用plt.gcf
and 来处理这些隐藏的对象plt.gca
,当对象方法之一尚未移植到plt命名空间中的方法时,这有时是必要的。
I'd like to add that the pltnamespace contains also a number of convenience methods to instantiate, in different ways, figureand axes.
我想补充一点,plt命名空间还包含许多方便的方法,以不同的方式实例化figure和axes。
Your examples
你的例子
1st way
plt.plot(x, y)
第一种方式
plt.plot(x, y)
Here you use only the pltinterface, you can only use a single axesin each figure, but this is what you want when you are doing an exploration of your data, a quick recipe that gets the work done...
在这里你只使用plt接口,你只能在每个图形中使用一个轴,但这就是你在探索数据时想要的,这是一个快速完成工作的秘诀......
2nd way
ax = plt.subplot() ax.plot(x, y)
第二种方式
ax = plt.subplot() ax.plot(x, y)
Here you use a convenience method in the pltnamespace to give a name (and a handle) to your axesobject, but btw there is also an hidden figure. You can later use the axesobject to plot, to make an histogram etc, all things that you can do with the pltinterface, but you can also access all its attributes and modify them with greater freedom.
在这里,您使用plt命名空间中的便捷方法为轴对象命名(和句柄),但顺便说一句,还有一个隐藏的figure。您可以稍后使用轴对象来绘制、制作直方图等,所有您可以使用plt界面执行的操作,但您也可以访问其所有属性并更自由地修改它们。
3rd way
figure = plt.figure() new_plot = figure.add_subplot(111) new_plot.plot(x, y)
第三种方式
figure = plt.figure() new_plot = figure.add_subplot(111) new_plot.plot(x, y)
Here you start instantiating a figureusing a convenience method in the pltnamespace and later you use only the object oriented interface.
在这里,您开始使用plt命名空间中的便捷方法实例化图形,然后仅使用面向对象的接口。
It is possible to bypass the pltconvenience method (matplotlib.figure.Figure
) but you then have to tweak the figure for a better interactive experience (after all, it's a conveniencemethod).
可以绕过plt便捷方法 ( matplotlib.figure.Figure
),但您必须调整图形以获得更好的交互体验(毕竟,这是一种便捷方法)。
Personal recommendations
个人推荐
I suggest bare plt.plot
, plt.scatter
in the context of an interactive session, possibly using IPythonwith its %matplotlib
magic command, and also in the context of an exploratory Jupyter notebook.
我建议在交互式会话的上下文中裸露plt.plot
,plt.scatter
可能使用IPython及其%matplotlib
魔法命令,以及在探索性 Jupyter 笔记本的上下文中。
On the other hand the object oriented approach, plus a few plt
convenience methods, is the way to go
另一方面,面向对象的方法,加上一些plt
方便的方法,是要走的路
- if you have a permanent issue to solve once for all with a customized arrangement of finely tuned subplots,
- if you want to embed Matplotlib in the UI of a program you write.
- 如果您有一个永久性问题需要通过精心调整的子图的自定义排列来一劳永逸,
- 如果您想将 Matplotlib 嵌入您编写的程序的 UI 中。
There is a large gray area between these extremes and if you ask me what to do I'd just say "It depends"...
在这些极端之间有一个很大的灰色区域,如果你问我该怎么做,我只会说“这取决于”......