简体   繁体   English

在 matplotlib 中使用绘图、轴或图形绘制绘图有什么区别?

[英]What is the difference between drawing plots using plot, axes or figure in matplotlib?

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.当我在 matplotlib 中绘制绘图时,我对后端发生的事情感到困惑,tbh,我不清楚绘图、轴和图形的层次结构。 I read the documentation and it was helpful but I'm still confused...我阅读了文档,它很有帮助,但我仍然感到困惑......

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 -现在我的问题是——

  1. What is the difference between all the three, I mean what is going under the hood when any of the 3 methods are called?这三个方法之间有什么区别,我的意思是当调用这 3 个方法中的任何一个时,幕后会发生什么?

  2. Which method should be used when and what are the pros and cons of using any on those?什么时候应该使用哪种方法,在这些方法上使用 any 的利弊是什么?

Method 1方法一

plt.plot(x, y)

This lets you plot just one figure with (x,y) coordinates.这使您可以仅绘制一个具有 (x,y) 坐标的图形。 If you just want to get one graphic, you can use this way.如果你只想得到一个图形,你可以使用这种方式。

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.您将绘制 4 个名为 ax1、ax2、ax3 和 ax4 的图形,每个图形都在同一窗口上。 This window will be just divided in 4 parts with my example.在我的示例中,这个窗口将被分为 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:其他例子:

在此处输入图片说明

The names of objects对象名称

Matplotlib is strongly object oriented and its principal objects are the figure and the axes (I find the name axes a bit misleading, but probably it's just me). Matplotlib 是强烈的面向对象,它的主要对象是图形(我发现名称axes有点误导,但可能只是我)。

You can think of the figure as a canvas , of which you typically specify the dimensions and possibly eg, 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 axes as a sort of Swiss Army knife, a handy object that offers a tool (eg .plot , .scatter , .hist etc) for everything, mostly.您可以将视为一种瑞士军刀,一个方便的对象,它为.plot提供工具(例如.plot.scatter.hist等)。 You can place one, two, ... many axes inside a figure using one of many different methods.您可以使用多种不同方法之一在图形中放置一个、两个、...多个

The plt interface plt接口

The plt procedural 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 (ie, a figure and an axes ) these objects are automatically instantiated and each plt method is, essentially, translated to a call of one of the methods of the underlying fundamental objects: eg, a plt.plot() is a hidden_axes.plot and a plt.savefig is a hidden_figure.savefig . plt程序界面最初是为了模仿 MATLAB™ 界面而开发的,但与面向对象的界面并没有真正的不同,即使您不直接引用主要对象(即图形),这些对象也是自动实例化,并且每个plt方法本质上都转换为对底层基本对象的方法之一的调用:例如, plt.plot()hidden_axes.plotplt.savefighidden_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 plt namespace.在任何时候,您都可以使用plt.gcfplt.gca来处理这些隐藏的对象,当对象方法之一尚未移植到plt命名空间中的方法时,有时需plt.gca

I'd like to add that the plt namespace contains also a number of convenience methods to instantiate, in different ways, figure and axes .我想补充一点, plt命名空间还包含许多方便的方法,以不同的方式实例化figureaxes

Your examples你的例子

1st way第一种方式

plt.plot(x, y)

Here you use only the plt interface, you can only use a single axes in 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)

Here you use a convenience method in the plt namespace to give a name (and a handle) to your axes object, but btw there is also an hidden figure .在这里,您使用plt命名空间中的便捷方法为轴对象命名(和句柄),但顺便说一句,还有一个隐藏的figure You can later use the axes object to plot, to make an histogram etc, all things that you can do with the plt interface, but you can also access all its attributes and modify them with greater freedom.您可以稍后使用对象来绘制、制作直方图等,所有您可以使用plt界面执行的操作,但您也可以访问其所有属性并更自由地修改它们。

3rd way第三种方式

figure = plt.figure() new_plot = figure.add_subplot(111) new_plot.plot(x, y)

Here you start instantiating a figure using a convenience method in the plt namespace and later you use only the object oriented interface.在这里,您开始使用plt命名空间中的便捷方法实例化图形,然后仅使用面向对象的接口。

It is possible to bypass the plt convenience method ( matplotlib.figure.Figure ) but you then have to tweak the figure for a better interactive experience (after all, it's a convenience method).可以绕过plt便捷方法( matplotlib.figure.Figure ),但您必须调整图形以获得更好的交互体验(毕竟,这是一种便捷方法)。

Personal recommendations个人推荐

I suggest bare plt.plot , plt.scatter in the context of an interactive session, possibly using IPython with its %matplotlib magic command, and also in the context of an exploratory Jupyter notebook.我建议在交互式会话的上下文中使用裸plt.plotplt.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" ...在这些极端之间有一个很大的灰色区域,如果你问我该怎么做,我只会说“这取决于” ......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM