问题描述
到目前为止,我已经尝试了以下代码:
#导入以处理绘制
seaborn as sns $的情况b
$ b#导入pyplot,内联图形,设置样式,绘图对图
导入matplotlib.pyplot as plt
#使图形空间
fig = plt。 Figure(figsize =(2,4))
gs = fig.add_gridspec(2,4)
ax1 = fig.add_subplot(gs [0,:])
ax2 = fig.add_subplot (gs [1,:])
#加载示例车祸数据集
tips = sns.load_dataset( tips)
#绘制频率计数按时间分组
sns.catplot(x ='sex',hue ='smoker',
kind ='count',
col ='time',
data = tips ,
ax = ax1)
#查看数据
sns.catplot(x ='sex',y ='total_bill',hue ='smoker',
kind ='小提琴',
col ='time',
split ='True',
cut = 0,
bw = 0.25,
scale ='area',
scale_hue = False,
inner ='quartile',
data = tips,
ax = ax2)
plt.close(2)
plt.close(3)
plt.show()
这似乎分别叠加了每种类型的分类图,彼此之间。
我想要的是以下代码在单个图中的生成图,其中第一行的计数图和第二行的小提琴图。
#导入以处理绘图
以seas的形式将seaborn输入为sns
#导入pyplot,内联图形,设置样式,绘图对绘图
as plt
#导入matplotlib.pyplot#加载示例车祸数据集
tips = sns.load_dataset( tips)
#绘制频率按时间分组的计数
sns.catplot(x ='sex',hue ='smoker',
kind ='count',
col ='time',
data =提示)
#查看数据
sns.catplot(x ='sex',y ='total_bill',hue ='smoker',
kind ='violin',
col ='time',
split ='True',
cut = 0,
bw = 0.25,
scale ='area',
scale_hue = False,
inner ='quartile',
data = tips)
实际分类我想跨越图的第一行的countplot,其中也包含分类小提琴图(参考资料图片3):
我想要跨越该图的第二行的实际分类小提琴图也包含一个类别计数图(参考图2):
我尝试了以下代码,迫使这些图位于同一图中。不利的一面是图形/轴的子项没有转移,即轴标签,图例和网格线。我对这种骇客感到很亲密,但需要其他推动或灵感来源。另外,我不再能够关闭旧的/不需要的数字。
#Import可以处理绘制
的绘图seaborn as sns
#导入pyplot,数字内联,设置样式,绘图pairplot
导入matplotlib.pyplot as plt
#设置某些样式
sns .set_style( whitegrid)
#加载示例车祸数据集
tips = sns.load_dataset( tips)
#绘制分组的频率计数按时间
a = sns.catplot(x ='sex',hue ='smoker',
kind ='count',
col ='time',
data = tips )
numSubs_A = len(a.col_names)
for i in range(numSubs_A):
for a in facet_axis(0,i).patches :
a.facet_axis(0,i).annotate(str(p.get_height()),(p.get_x()+ 0.15,p.get_height()+ 0.1))
#查看数据
b = sns.catplot(x ='sex',y ='total_bill',hue ='smoker',
kind ='小提琴',
col ='时间',
split ='True',
cut = 0,
bw = 0.25,
scale ='区域',
scale_hue = False,
inner ='quartile',
data = tips)
numSubs_B = len(b.col_names)
#子图迁移
f = plt.figure()
for i in range(numSubs_A):
f._axstack.add(f._make_key(a.facet_axis(0,i)) ,a.facet_axis(0,i))
for i in range(numSubs_B):
f._axstack.add(f._make_key(b.facet_axis(0,i)),b.facet_axis( 0,i))
#子图大小调整
f.axes [0] .set_posit ion([0,1,1,1])$ b $ b f.axes [1] .set_position([1,1,1,1])$ b $ b f.axes [2] .set_position([0 ,0,1,1])$ b $ b f.axes [3] .set_position([1,0,1,1])$ b $ b
一般来说,不可能合并几个航海人物的输出级功能整合为一个图形。请参阅(
So far I have tried the following code:
# Import to handle plotting
import seaborn as sns
# Import pyplot, figures inline, set style, plot pairplot
import matplotlib.pyplot as plt
# Make the figure space
fig = plt.figure(figsize=(2,4))
gs = fig.add_gridspec(2, 4)
ax1 = fig.add_subplot(gs[0, :])
ax2 = fig.add_subplot(gs[1, :])
# Load the example car crash dataset
tips = sns.load_dataset("tips")
# Plot the frequency counts grouped by time
sns.catplot(x='sex', hue='smoker',
kind='count',
col='time',
data=tips,
ax=ax1)
# View the data
sns.catplot(x='sex', y='total_bill', hue='smoker',
kind='violin',
col='time',
split='True',
cut=0,
bw=0.25,
scale='area',
scale_hue=False,
inner='quartile',
data=tips,
ax=ax2)
plt.close(2)
plt.close(3)
plt.show()
This seems to stack the categorial plots, of each kind respectively, on top of eachother.
What I want are the resulting plots of the following code in a single figure with the countplot in row one and the violin plot in row two.
# Import to handle plotting
import seaborn as sns
# Import pyplot, figures inline, set style, plot pairplot
import matplotlib.pyplot as plt
# Load the example car crash dataset
tips = sns.load_dataset("tips")
# Plot the frequency counts grouped by time
sns.catplot(x='sex', hue='smoker',
kind='count',
col='time',
data=tips)
# View the data
sns.catplot(x='sex', y='total_bill', hue='smoker',
kind='violin',
col='time',
split='True',
cut=0,
bw=0.25,
scale='area',
scale_hue=False,
inner='quartile',
data=tips)
The actual categorical countplot that I would like to span row one of a figure that also contains a categorical violin plot (Ref. Image 3):
The actual categorical violin plot that I would like to span row two of a figure that also contains a categorical countplot (Ref. Image 2):
I tried the following code which forced the plots to be in the same figure. The downside is that the children of the figure/axes did not transfer, i.e. axis-labels, legend, and grid lines. I feel pretty close with this hack but need another push or source for inspiration. Also, I'm no longer able to close the old/unwanted figures.
# Import to handle plotting
import seaborn as sns
# Import pyplot, figures inline, set style, plot pairplot
import matplotlib.pyplot as plt
# Set some style
sns.set_style("whitegrid")
# Load the example car crash dataset
tips = sns.load_dataset("tips")
# Plot the frequency counts grouped by time
a = sns.catplot(x='sex', hue='smoker',
kind='count',
col='time',
data=tips)
numSubs_A = len(a.col_names)
for i in range(numSubs_A):
for p in a.facet_axis(0,i).patches:
a.facet_axis(0,i).annotate(str(p.get_height()), (p.get_x()+0.15, p.get_height()+0.1))
# View the data
b = sns.catplot(x='sex', y='total_bill', hue='smoker',
kind='violin',
col='time',
split='True',
cut=0,
bw=0.25,
scale='area',
scale_hue=False,
inner='quartile',
data=tips)
numSubs_B = len(b.col_names)
# Subplots migration
f = plt.figure()
for i in range(numSubs_A):
f._axstack.add(f._make_key(a.facet_axis(0,i)), a.facet_axis(0,i))
for i in range(numSubs_B):
f._axstack.add(f._make_key(b.facet_axis(0,i)), b.facet_axis(0,i))
# Subplots size adjustment
f.axes[0].set_position([0,1,1,1])
f.axes[1].set_position([1,1,1,1])
f.axes[2].set_position([0,0,1,1])
f.axes[3].set_position([1,0,1,1])
It is in general not possible to combine the output of several seaborn figure-level functions into a single figure. See (this question, also this issue). I once wrote a hack to externally combine such figures, but it has several drawbacks. Feel free to use it if it works for you.
But in general, consider creating the plot you desired manually. In this case it could look like this:
import seaborn as sns
import matplotlib.pyplot as plt
sns.set()
fig, axes = plt.subplots(2,2, figsize=(8,6), sharey="row", sharex="col")
tips = sns.load_dataset("tips")
order = tips["sex"].unique()
hue_order = tips["smoker"].unique()
for i, (n, grp) in enumerate(tips.groupby("time")):
sns.countplot(x="sex", hue="smoker", data=grp,
order=order, hue_order=hue_order, ax=axes[0,i])
sns.violinplot(x='sex', y='total_bill', hue='smoker', data=grp,
order=order, hue_order=hue_order,
split='True', cut=0, bw=0.25,
scale='area', scale_hue=False, inner='quartile',
ax=axes[1,i])
axes[0,i].set_title(f"time = {n}")
axes[0,0].get_legend().remove()
axes[1,0].get_legend().remove()
axes[1,1].get_legend().remove()
plt.show()
这篇关于如何使用sharex = True在catplot(kind ='violin')的顶部对seaborn catplot(kind ='count')进行子图绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!