matplotlib绘图
Let’s admit it, we’ve all been in this situation. At times, can be very confusing and frustrating because a single line plot could have number of different methods. You then asked yourselves a question, so how do I actually construct a line plot in matplotlib? If you are one of those people, fret not, I have been in your shoes(still probably is) and hopefully this will shed some light for you.
让我们承认,我们都处于这种情况。 有时,这可能会非常令人困惑和沮丧,因为单个线图可能具有许多不同的方法。 然后,您自己问了一个问题,那么,实际上如何在matplotlib中构造线图? 如果您是这些人中的一员,则不用担心,我一直在您的鞋子中(可能仍然在),希望这对您有所启发。
For starters, there are 2 common ways of plotting in matplotlib, as indicated in their documentation, those are:
首先,如其文档中所述,在matplotlib中有2种常见的绘图方式,它们是:
- State-based — pyplot API 基于状态-pyplot API
- Object oriented (OO) 面向对象(OO)
In short, state-based offers quick way of plotting but is less flexible than object oriented. Reason being is because in state based interface, the figure and axes are automatically created and you are working with it one at a time. Unlike state based interface, object oriented demands the user to explicitly create the figure(s) and axes objects. This, in fact, is very handy especially when it comes to plotting 2 or more individual plots on the same figure or maybe to customize the plot in the future.
简而言之,基于状态的图形提供了快速绘制的方法,但灵活性不如面向对象。 原因是因为在基于状态的界面中,图形和轴是自动创建的,并且您一次只能使用它。 与基于状态的界面不同,面向对象要求用户明确创建图形和轴对象。 实际上,这非常方便,尤其是在同一图形上绘制2个或多个单独的图或将来定制图时。
Before we get into it, let’s introduce a couple of terminology so that we’re on the same page, those are figure(s) and axes. Figure is basically an empty canvas where it contains the axes. Axes, as far as matplotlib is concern, is not plural form of axis, instead it is the region where you place your visualizations where it contains your data(individual scatter or line plot, title, y/x axis labels, etc). Image below best described it.
在开始讨论之前,我们先介绍几个术语,以便我们在同一页面上,即图和轴。 图基本上是一个包含轴的空画布。 就matplotlib而言,轴不是轴的复数形式,而是在其中放置可视化内容的区域,其中包含数据(单个散点图或折线图,标题,y / x轴标签等)。 下图最恰当地描述了它。
基于状态的接口(State — Based Interface)
Let’s look at this simple example of creating line plot in matplotlib using state based interface. In the code, figure instance was created before creating the line plot itself. By default, any figure(s) or axes will have their own id. To prove the theory, the id’s are printed and compared. In the figure instance, if the first parameter which is num, not provided, automatically, the figure will hold an incremented figure number starting from 1. It’s the reason why we’re seeing a list containing number 1 below. If for example we place number 5 in the parenthesis, the plt.get_fignums() will output 5 instead. I think plt.close(‘all’) is self-explanatory, whereby all it does is to close all figure(s) and erase it from the memory.
让我们看一下使用基于状态的接口在matplotlib中创建线图的简单示例。 在代码中,图形实例是在创建折线图本身之前创建的。 默认情况下,任何图形或轴都有其自己的ID。 为了证明这一理论,对ID进行了打印和比较。 在图形实例中,如果未自动提供第一个参数num(未提供),则图形将保留从1开始的递增图形编号。这就是为什么我们看到下面包含数字1的列表的原因。 例如,如果将5放在括号中,则plt.get_fignums()将改为输出5。 我认为plt.close('all')是不言自明的,它所做的就是关闭所有图形并将其从内存中删除。
Now, let’s run the code again but this time without calling the plt.figure(). What we immediately see is that, we get an output(id) both from figure and axes instances. Plus, we even have one figure in the figure list. This already suggesting to us that, by only calling plt.plot(), it automatically created both the figure and axes instances without having the need for us to explicitly calls for it. Basically, when working with state based interface we’re working with one figure at one time. To summarize, let’s finish our discussion on state based interface with this brilliant sentence from matplotlib documentation.
现在,让我们再次运行代码,但是这次不调用plt.figure()。 我们立即看到的是,我们从图形实例和轴实例中都获得了一个output(id)。 另外,在图形列表中甚至还有一个图形。 这已经向我们暗示,仅通过调用plt.plot(),它就可以自动创建图形实例和轴实例,而无需我们显式调用它。 基本上,使用基于状态的接口时,我们一次要处理一个图形。 总而言之,让我们用matplotlib文档中的这一精妙语句结束对基于状态的接口的讨论。
“In matplotlib.pyplot various states are preserved across function calls, so that it keeps track of things like the current figure and plotting area, and the plotting functions are directed to the current axes.”
“在matplotlib.pyplot中,跨函数调用保留了各种状态,因此它可以跟踪诸如当前图形和绘图区域之类的东西,并且绘图函数指向当前轴。”
面向对象的接口 (Object-Oriented Interface)
In many cases, OO is recommended due to the flexibility it offers. To be honest, OO still uses the pyplot API to call for the axes and figure(s) instances except, now the instances are assigned to variable the user specify. From there, you can work on these objects.
在许多情况下,建议使用OO,因为它提供了灵活性。 老实说,OO仍然使用pyplot API来调用轴和图形实例,但现在实例已分配给用户指定的变量。 从那里,您可以处理这些对象。
Let’s look at typical OO interface of creating lineplot.
让我们看一下创建线图的典型OO接口。
Plt.subplots() returns figure and axes instances which is why we’re seeing the command is assigned to 2 variables. Conveniently, the code allows us to customize the layout of the plot within the same figure e.g. draws a figure with 3 rows and 2 columns plotting area or any arbitrary number as we wish. By default, the figure will have only single plotting area and that’s exactly what we’re trying to do. The axes will contain the plotting method which in our case, the line plot itself. Majority of the time we will be using the axes instance to customize the plot by adding title, x and y labels, etc. Finally, more ways to plot with plt.subplots() can be found here.
Plt.subplots()返回图形和轴实例,这就是为什么我们看到将命令分配给2个变量的原因。 方便地,该代码使我们可以自定义同一图形中图形的布局,例如绘制一个具有3行2列图形区域或任意数字的图形。 默认情况下,该图将只有一个绘图区域,而这正是我们正在尝试做的。 轴将包含绘图方法,在本例中为线图本身。 大多数情况下,我们将使用axis实例通过添加标题,x和y标签等来自定义绘图。最后,可以在此处找到更多使用plt.subplots()进行绘图的方法。
Add_axes()吗? (Add_axes() ?)
It’s worth mentioning that plt.subplots() creates the layout of the plot with the same size. There are also ways for us to create axes with irregular size or maybe overlay small axes within it. The first 2 arguments within the add_axes parenthesis defines the distance from the left and bottom whereas the last 2, width and height.
值得一提的是,plt.subplots()会以相同的大小创建图的布局。 我们还有其他方法可以创建尺寸不规则的轴,也可以在其中重叠小轴。 add_axes括号中的前2个自变量定义了距左和底部的距离,而后2个则定义了宽度和高度。
结论 (Conclusion)
There’s no right or wrong way of plotting in Matplotlib. In fact, it’s only a matter of personal preference. In general, OO interface is recommended because the plot is easily customizable by initially creating figure and axes objects but if you’re looking to do a plot very quickly then most probably state based is suitable.
Matplotlib中没有正确或错误的绘图方式。 实际上,这只是个人喜好问题。 通常,建议使用OO接口,因为可以通过最初创建图形和轴对象来轻松自定义图,但是如果您希望快速进行图绘制,则很可能适合基于状态的图。
That’s it for now and I hope you find this insightful. Thank you!
到此为止,我希望您能找到有见地的人。 谢谢!
matplotlib绘图