使用 matplotlib 面向对象的接口与 seaborn 绘图 [英] Plotting with seaborn using the matplotlib object-oriented interface

查看:38
本文介绍了使用 matplotlib 面向对象的接口与 seaborn 绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

问题:如何以这种方式使用seaborn?或者,如何将这个例子改为OOP风格?如何告诉 seaborn 绘图函数,例如 lmplot 它绘制到哪个 FigureAxes ?

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?

推荐答案

这在一定程度上取决于您使用的是哪个 seaborn 函数.

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

seaborn 中的绘图函数大致分为两类

The plotting functions in seaborn are broadly divided into two classes

  • 轴级"函数,包括 regplotboxplotkdeplot
  • 图级"函数,包括relplotcatplotdisplotpairplotjointplot和一个或另外两个
  • "Axes-level" functions, including regplot, boxplot, kdeplot, and many others
  • "Figure-level" functions, including relplot, catplot, displot, pairplot, jointplot and one or two others

通过采用显式的 ax 参数并返回一个 Axes 对象来标识第一组.正如这表明的那样,您可以在面向对象"中使用它们.通过将您的 Axes 传递给他们来设置样式:

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 上绘制并且不会弄乱图形,因此它们可以在面向对象的 matplotlib 脚本中完美地共存.

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.

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

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.

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

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.

这篇关于使用 matplotlib 面向对象的接口与 seaborn 绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆