简体   繁体   English

matplotlib函数约定:子图与一个图

[英]matplotlib function conventions: subplots vs one figure

I'm getting really confused with the subtle differences in functions (is that what they're called?) in matplotlib when working with subplots vs single figures. 在使用子图和单个图时,我在matplotlib中对函数的微妙差异(就是它们被称为是什么?)感到非常困惑。

As an example: 举个例子:

If I want to change the xlim on a single figure I'd do: plt.xlim() 如果我想在单个数字上更改xlim,我会这样做: plt.xlim()

If I want to change the xlim on a subplot figure I'd do: ax.set_xlim() 如果我想更改子图上的xlim,我会这样做: ax.set_xlim()

Why is this? 为什么是这样? Can anyone send me to a website that explains the conventions? 任何人都可以将我发送到解释惯例的网站吗? This is an easy example because I know they're different, and how, but there are a number of ones that I don't know and I'm having trouble figuring out- for example: 这是一个简单的例子,因为我知道它们是不同的,以及如何,但有许多我不知道的,我很难搞清楚 - 例如:

1) To apply a collection to a subplot: ax.collection(collection_name) . 1)将集合应用于子图: ax.collection(collection_name) How do you do it for a single figure? 你是如何为一个人物做的?

2) To apply tick labels to a subplot: ax.xaxis.ticklabels(tick_labels) . 2)将刻度标签应用于子图: ax.xaxis.ticklabels(tick_labels) How do you do this for a single figure? 你如何为一个人物做这个?

Sorry this is such a newb question! 对不起,这是一个新问题!

You are hitting the differences between the pyplot/state machine interface and the OO interface. 您正在点击pyplot /状态机界面和OO界面之间的差异。 See Which is the recommended way to plot: matplotlib or pylab? 请参阅哪种绘图方法:matplotlib或pylab? and How can I attach a pyplot function to a figure instance? 以及如何将pyplot函数附加到图形实例? for longer explainations. 用于更长的解释。

For a much longer tutorial see Anatomy of Matplotlib 有关更长时间的教程,请参阅Matplotlib剖析

There is also some confusion about the layers of objects in mpl: 关于mpl中的对象层也存在一些混淆:

Important classes 重要的课程

Figure

The whole figure. 整个人物。 Keeps track of all the child axes , a smattering of 'special' artists (titles, figure legends, etc), and the canvas . 跟踪所有儿童 ,一些“特殊”艺术家(标题,图例等)和画布 Don't worry too much about the canvas, it is crucial as it is the object that actually does the drawing to get you your plot, but as the user it is more-or-less invisible to you. 不要过于担心画布,它是至关重要的,因为实际上绘制的对象可以让你获得你的情节,但作为用户,它对你来说或多或少是不可见的。

Axes

This is what you think of as 'a plot', it is the region of the image with the data space. 这就是您所说的“情节”,它是具有数据空间的图像区域。 It has 2 or 3 axis objects (x, y, and sometimes z), and a contains a whole bunch of artists . 它有2或3个对象(x,y,有时是z),并且包含一大堆艺术家

Axis

These are the number line like objects. 这些是像对象一样的数字线。 They take care of setting the graph limits and generating the ticks and tick labels. 他们负责设置图表限制并生成刻度和刻度标签。 They have a Locator and a Formatter for sorting out where the ticks go and how to generate the labels 他们有一个Locator和一个Formatter用于整理刻度线的位置以及如何生成标签

Artist

Basically everything you can see on the figure is an artist (even Figure , Axes , and Axis objects). 基本上你在图上看到的一切都是艺术家(甚至是FigureAxesAxis对象)。 This includes Text objects, Line2D objects, collection objects, Patch objects ... (you get the idea). 这包括Text对象, Line2D对象, collection对象, Patch对象......(你明白了)。 When the figure is rendered, all of the artists are drawn (recursively) to the canvas . 渲染图形时,所有艺术家都会(以递归方式)绘制到画布上 A given artist can only be in one Axes . 给定的艺术家只能在一个Axes

API layers API层

pylab

This is basically just a name space that a whole bunch of stuff (numpy, pyplot, mlab (ships with matplotlib)) are bulk imported into via from foo import * . 这基本上只是一个名称空间,一大堆东西(numpy,pyplot,mlab(与matplotlib一起发货)) from foo import *批量导入via。 This can be very convenient for interactive work, but you should not use it is scripts. 这对于交互式工作来说非常方便,但是你不应该使用脚本。 The original goal of this 这个的最初目标

pyplot

This is a state machine layer that keeps track of all your open figures and has a notion of 'current figure' ( plt.gcf() ) and 'current axes' ( plt.gca() ). 这是一个状态机层,可以跟踪所有打开的数字,并具有“当前数字”( plt.gcf() )和“当前轴”( plt.gca() )的概念。 Most of the functions in pyplot are very thin (programitcally generated) wrappers around calls to the OO layer ( plt.foo() -> plt.gca().foo() or plt.gcf().foo() depending on the function). pyplot中的大多数函数都是非常薄的(按照程序生成)包装器调用OO层( plt.foo() - > plt.gca().foo()plt.gcf().foo()取决于功能)。 Again, this can be convenient , but can quickly become limiting/confusing/global state causes trouble. 同样,这可能很方便 ,但很快就会变得有限/混乱/全局状态导致麻烦。

OO layer OO层

This is the layer that actually manages the creation of artists (ex ax.plot(...) creates a bunch of Line2D objects). 这是实际管理艺术家创作的层(ex ax.plot(...)创建了一堆Line2D对象)。 For writing scripts or anything you intend to re-use this is the layer you should try to use. 对于编写脚本或任何您打算重用的东西,这是您应该尝试使用的层。 To write your own plotting functions I recommend writing functions like this: 要编写自己的绘图函数,我建议编写如下函数:

def my_plotting_fun(ax, data, data, ...):
     ax.do_stuff
     return list_of_artists_added

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

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