简体   繁体   English

Matplotlib的图和轴解释

[英]Matplotlib's Figure and Axes explanation

I am really pretty new to matplotlib, though I know that it can be very powerful. 我对matplotlib来说真的很新,不过我知道它可以非常强大。 I've been reading number of tutorials and examples and it's a real hassle to understand how does matplotlib's Figure and Axes work. 我一直在阅读一些教程和示例,了解matplotlib的图和轴如何工作真的很麻烦。 I am illustrating, what I am trying to understand, with the attached figure. 我用附图说明了我想要理解的内容。 在此输入图像描述

I know how to create a figure instance of certain size in inches. 我知道如何以英寸为单位创建一定大小的图形实例。 However, what bothers me is how can I create subplots and then axes, within each subplot, with relative coordinates (bottom=0,left=0,top=1,right=1) as illustrated. 然而,困扰我的是如何在每个子图中创建子图,然后在相对坐标(底部= 0,左= 0,顶部= 1,右边= 1)中创建轴,如图所示。 So, for example I want to create a "parent" plot area (say (6in,10in)). 所以,例如我想创建一个“父”绘图区域(比如说(6英寸,10英寸))。 Then, I want to create two subplot areas, each with size (3in,3in), with 1in space from the top, 2in space between the two vertical subplot areas and 1in from bottom. 然后,我想创建两个子图区域,每个区域的大小(3英寸,3英寸),顶部距离为1英寸,两个垂直子图区域之间为2英寸,底部为1英寸。 Then, 1in space on the left and 2in space on the write. 然后,在左边的1英寸空间和写入的2英寸空间。 In the same time, I would like to be able to get the coordinates of the subplot areas with respect to the main plot area. 同时,我希望能够获得相对于主要绘图区域的子图区域的坐标。 Then, inside the first subplot area, I'd like to create 2 axis instances, with Axis 1, having coordinates with respect to Subplot Area1 (0.1,0.7,0.7,0.2) and Axes 2 (0.1,0.2,0.7,0.5). 然后,在第一个子图区域内,我想创建2个轴实例,其中Axis 1具有相对于Subplot Area1(0.1,0.7,0.7,0.2)和Axes 2(0.1,0.2,0.7,0.5)的坐标。 And then of course I'd like to be able to plot on these axes eg, ax1.plot().... 当然,我希望能够在这些轴上绘图,例如, ax1.plot()....
If you could provide a sample code to achieve that, then I can study it. 如果你能提供一个示例代码来实现它,那么我可以研究它。 Your help will be very much appreciated! 非常感谢您的帮助!

a subplot and an Axes object are really the same thing. subplotAxes对象实际上是一回事。 There is not really a "subplot" as you describe it in matplotlib. 你在matplotlib中描述的并没有真正的“subplot”。 You can just create your three Axes objects using gridspec without the need to put them in your "subplots". 您可以使用gridspec创建三个Axes对象,而无需将它们放在“子图”中。

There are a few different ways to create Axes instances within your figure. 在图中创建Axes实例有几种不同的方法。

fig.add_axes will create an Axes instance at the position given to it (you give it [left,bottom,width,height] in figure coordinates (ie 0,0 is bottom left, 1,1 is top right). fig.add_axes将在给定的位置创建一个Axes实例(在图坐标中给出它[left,bottom,width,height] (即0,0是左下角, 1,1是右上角)。

fig.add_subplot will also create an Axes instance. fig.add_subplot还将创建一个Axes实例。 In this case, rather than giving it a rectangle to be created in, you give it the number of rows and columns of subplots you would like, and then the plot_number , where plot_number starts at 1, increments across rows first and has a maximum of nrows * ncols . 在这种情况下,不是给它创建一个矩形,而是给它你想要的子图的行数和列数,然后是plot_number ,其中plot_number从1开始,首先在行之间递增,最大值为nrows * ncols

For example, to create the top-left Axes in a grid of 2 row and 2 columns, you could do the following: 例如,要在2行和2列的网格中创建左上角的轴,可以执行以下操作:

fig.add_subplot(2,2,1)

or the shorthand 或速记

fig.add_subplot(221)

There are some more customisable ways to create Axes as well, for example gridspec and subplot2grid which allow for easy creation of many subplots of different shapes and sizes. 还有一些可自定义的方法来创建Axes ,例如gridspecsubplot2grid ,它们可以轻松创建许多不同形状和大小的子图。

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

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