Python 使用 matplotlib 面向对象的接口与 seaborn 绘图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23969619/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:45:34  来源:igfitidea点击:

Plotting with seaborn using the matplotlib object-oriented interface

pythonoopmatplotlibseaborn

提问by Frozen Flame

I strongly prefer using matplotlibin OOP style:

我非常喜欢matplotlib在 OOP 风格中使用:

f, axarr = plt.subplots(2, sharex=True)
axarr[0].plot(...)
axarr[1].plot(...)

This makes it easier to keep track of multiple figures and subplots.

这样可以更轻松地跟踪多个图形和子图。

Question: How to use seaborn this way? Or, how to change this exampleto OOP style? How to tell seabornplotting functions like lmplotwhich Figureor Axesit plots to?

问题:如何以这种方式使用seaborn?或者,如何将此示例更改为 OOP 样式?如何告诉seaborn绘图函数,如lmplot哪个FigureAxes它绘图?

采纳答案by mwaskom

It depends a bit on which seaborn function you are using.

这在一定程度上取决于您使用的 seaborn 函数。

The plotting functions in seaborn are broadly divided into two classes

seaborn 中的绘图功能大致分为两类

  • "Axes-level" functions, including regplot, boxplot, kdeplot, and many others
  • "Figure-level" functions, including lmplot, factorplot, jointplotand one or two others
  • “轴级”功能,包括regplotboxplotkdeplot,和许多其他
  • “图级”功能,包括lmplotfactorplotjointplot和一个或两个其他

The first group is identified by taking an explicit axargument and returning an Axesobject. As this suggests, you can use them in an "object oriented" style by passing your Axesto them:

第一组通过采用显式ax参数并返回一个Axes对象来标识。正如这表明的那样,您可以通过将您的信息传递Axes给他们,以“面向对象”的风格使用它们:

f, (ax1, ax2) = plt.subplots(2)
sns.regplot(x, y, ax=ax1)
sns.kdeplot(x, ax=ax2)

Axes-level functions will only draw onto an Axesand won't otherwise mess with the figure, so they can coexist perfectly happily in an object-oriented matplotlib script.

轴级函数只会绘制到Axes图形上,不会弄乱图形,因此它们可以在面向对象的 matplotlib 脚本中完美地共存。

The second group of functions (Figure-level) are distinguished by the fact that the resulting plot can potentially include several Axes which are always organized in a "meaningful" way. That means that the functions need to have total control over the figure, so it isn't possible to plot, say, an lmplotonto one that already exists. Calling the function always initializes a figure and sets it up for the specific plot it's drawing.

第二组函数(图形级)的特点是生成的图可能包含多个轴,这些轴总是以“有意义”的方式组织。这意味着函数需要完全控制图形,因此不可能绘制,例如,lmplot在已经存在的图形上。调用该函数总是初始化一个图形并为它绘制的特定绘图设置它。

However, once you've called lmplot, it will return an object of the type FacetGrid. This object has some methods for operating on the resulting plot that know a bit about the structure of the plot. It also exposes the underlying figure and array of axes at the FacetGrid.figand FacetGrid.axesarguments. The jointplotfunction is very similar, but it uses a JointGridobject. So you can still use these functions in an object-oriented context, but all of your customization has to come after you've called the function.

但是,一旦您调用了lmplot,它将返回一个类型为 的对象FacetGrid。这个对象有一些对结果图进行操作的方法,这些方法对图的结构有所了解。它还在FacetGrid.figFacetGrid.axes参数处公开底层图形和轴数组。该jointplot函数非常相似,但它使用了一个JointGrid对象。因此,您仍然可以在面向对象的上下文中使用这些函数,但所有自定义都必须在调用该函数之后进行。