问题:使用matplotlib面向对象的界面进行seaborn绘图

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

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

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

问题:如何以这种方式使用seaborn?或者,如何将此示例更改为OOP样式?如何分辨seaborn绘图功能(例如lmplot哪个Figure或哪个)Axes

I strongly prefer using matplotlib in OOP style:

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 example to OOP style? How to tell seaborn plotting functions like lmplot which Figure or Axes it plots to?


回答 0

这在某种程度上取决于您使用的是哪种功能。

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

  • “轴级”功能,包括regplotboxplotkdeplot,和许多其他
  • “图级”功能,包括lmplotfactorplotjointplot和一个或两个其他

通过采用显式ax参数并返回Axes对象来标识第一组。如此建议,您可以将它们传递Axes给它们,从而以“面向对象”的方式使用它们:

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

轴级功能将仅绘制到,Axes并且不会与图形混淆,因此它们可以在面向对象的matplotlib脚本中完美地愉快地共存。

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

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

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

The plotting functions in seaborn are broadly divided into two classes

  • “Axes-level” functions, including regplot, boxplot, kdeplot, and many others
  • “Figure-level” functions, including lmplot, factorplot, jointplot and one or two others

The first group is identified by taking an explicit ax argument and returning an Axes object. As this suggests, you can use them in an “object oriented” style by passing your Axes to them:

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 Axes and won’t otherwise mess with the figure, so they can coexist perfectly happily in an object-oriented matplotlib script.

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 lmplot onto one that already exists. Calling the function always initializes a figure and sets it up for the specific plot it’s drawing.

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.fig and FacetGrid.axes arguments. The jointplot function is very similar, but it uses a JointGrid object. 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.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。