使用Seaborn在一个图中绘制多个不同的图 [英] Plotting multiple different plots in one figure using Seaborn

查看:156
本文介绍了使用Seaborn在一个图中绘制多个不同的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用seaborn

我特别想使用 seaborn 的 lmplot 创建前两个图并使用 boxplot 创建第二个图来重新创建它.主要问题是 lmplot 根据

DataFrame 的示例数据:

df_melt = {'教育': {0: '1.<高中毕业生",1:'4.大学毕业",2:'3.一些大学',3:'4.大学毕业",4:'2.HS 毕业'},'值':{0: 18, 1: 24, 2: 45, 3: 43, 4: 50},'variable':{0:年龄",1:年龄",2:年龄",3:年龄",4:年龄"},工资":{0:75.043154017351497,1:70.476019646944508,2:130.982177377461,3:154.68529299562999,4:75.043154017351497}}df_wage = {'教育':{0:'1.<HS毕业',1:'4.大学毕业生',2:'3.一些大学3:'4.大学毕业生',4:'2.HS Grad'},'工资':{0:75.043154017351497,1:70.476019646944508,2:130.982177377461,3:154.68529299562999,4:75.043154017351497}}

解决方案

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

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

像这样:

 将matplotlib.pyplot导入为plt将 seaborn 作为 sns 导入无花果,斧头= 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='教育',y='工资', 数据=df_melt, ax=axs[2])

I am attempting to recreate the following plot from the book Introduction to Statistical learning using seaborn

I specifically want to recreate this using seaborn's lmplot to create the first two plots and boxplot to create the second. The main problem is that lmplot creates a facetgrid according to this answer which 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_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 yields

Sample data for DataFrames:

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}}

解决方案

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=.

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:

import matplotlib.pyplot as plt
import seaborn as sns

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])

这篇关于使用Seaborn在一个图中绘制多个不同的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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