Python 使用 Seaborn 在一张图中绘制多个不同的图

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

Plotting multiple different plots in one figure using Seaborn

pythonmatplotlibseaborn

提问by Ted Petrou

I am attempting to recreate the following plot from the book Introduction to Statistical learning using seaborn enter image description here

我试图从使用 seaborn 的统计学习简介一书中重新创建以下情节 在此处输入图片说明

I specifically want to recreate this using seaborn's lmplotto create the first two plots and boxplotto create the second. The main problem is that lmplot creates a facetgridaccording to this answerwhich forces me to hackily add another matplotlib axes for the boxplot. I was wondering if there was an easier way to achieve this. Below, I have to do quite a bit of manual manipulation to get the desired plot.

我特别想使用 seaborn 重新创建它lmplot来创建前两个图并boxplot创建第二个图。主要问题是 lmplotfacetgrid根据这个答案创建了一个,这迫使我为箱线图添加另一个 matplotlib 轴。我想知道是否有更简单的方法来实现这一目标。下面,我必须做相当多的手动操作才能获得所需的情节。

seaborn_grid = sns.lmplot('value', 'wage', col='variable', hue='education', data=df_melt, sharex=False)
seaborn_grid.fig.set_figwidth(8)

left, bottom, width, height = seaborn_grid.fig.axes[0]._position.bounds
left2, bottom2, width2, height2 = seaborn_grid.fig.axes[1]._position.bounds
left_diff = left2 - left
seaborn_grid.fig.add_axes((left2 + left_diff, bottom, width, height))

sns.boxplot('education', 'wage', data=df_wage, ax = seaborn_grid.fig.axes[2])
ax2 = seaborn_grid.fig.axes[2]
ax2.set_yticklabels([])
ax2.set_xticklabels(ax2.get_xmajorticklabels(), rotation=30)
ax2.set_ylabel('')
ax2.set_xlabel('');

leg = seaborn_grid.fig.legends[0]
leg.set_bbox_to_anchor([0, .1, 1.5,1])

Which yieldsenter image description here

哪个产量在此处输入图片说明

Sample data for DataFrames:

DataFrame 的示例数据:

df_melt = {'education': {0: '1. < HS Grad',
  1: '4. College Grad',
  2: '3. Some College',
  3: '4. College Grad',
  4: '2. HS Grad'},
 'value': {0: 18, 1: 24, 2: 45, 3: 43, 4: 50},
 'variable': {0: 'age', 1: 'age', 2: 'age', 3: 'age', 4: 'age'},
 'wage': {0: 75.043154017351497,
  1: 70.476019646944508,
  2: 130.982177377461,
  3: 154.68529299562999,
  4: 75.043154017351497}}

df_wage={'education': {0: '1. < HS Grad',
  1: '4. College Grad',
  2: '3. Some College',
  3: '4. College Grad',
  4: '2. HS Grad'},
 'wage': {0: 75.043154017351497,
  1: 70.476019646944508,
  2: 130.982177377461,
  3: 154.68529299562999,
  4: 75.043154017351497}}

回答by Diziet Asahi

One possibility would be to NOT use lmplot(), but directly use regplot()instead. regplot()plots on the axes you pass as an argument with ax=.

一种可能性是不使用lmplot(),而是直接使用regplot()regplot()在您作为参数传递的轴上绘图ax=

You lose the ability to automatically split your dataset according to a certain variable, but if you know beforehand the plots you want to generate, it shouldn't be a problem.

您失去了根据某个变量自动拆分数据集的能力,但是如果您事先知道要生成的图,那应该不成问题。

Something like this:

像这样的东西:

fig, axs = plt.subplots(ncols=3)
sns.regplot(x='value', y='wage', data=df_melt, ax=axs[0])
sns.regplot(x='value', y='wage', data=df_melt, ax=axs[1])
sns.boxplot(x='education',y='wage', data=df_melt, ax=axs[2])