我试图用折断了y轴的seaborn来绘制带有小提琴图的猫图(因为我有一个因果过程以两个不同的比例起作用:一个在[0,0.2]之间,另一个在[2,12之间] ]我的定量y变量)。
我从this answer that there is not implemented easy feature allowing this kind of plot in seaborn (yet?)了解到 因此,我尝试了不成功的不同方法来堆叠同一数据集但具有两个不同比例的两个图。
尝试失败的尝试:
我们尝试使用标准数据集“锻炼”:
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
exercise = sns.load_dataset("exercise")
f, (ax1, ax2) = plt.subplots(ncols=2, nrows=1, sharey=True)
f = sns.catplot(x="time", y="pulse", hue="kind",data=exercise, kind="violin",ax=ax1)
f = sns.catplot(x="time", y="pulse", hue="kind",data=exercise, kind="violin",ax=ax2)
ax1.set_ylim(0, 6.5) # those limits are fake
ax2.set_ylim(13.5, 20)
plt.subplots_adjust(wspace=0, hspace=0)
plt.show()
我也尝试使用facegrid,但没有成功
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
exercise = sns.load_dataset("exercise")
g = sns.FacetGrid(exercise, col="kind",row="time")
g.map(sns.catplot, x="time", y="pulse", hue="kind",data=exercise, kind="violin")
plt.show()
在这里,它为我提供了正确的地块网格基础,但是在其他图中出现了地块。
答案 0 :(得分:1)
如果要绘制子图,则不能使用catplot
,它是图形级功能。相反,您需要直接使用violinplot
。另外,如果要使用两个不同的y比例尺,则在创建子图时不能使用sharey=True
。
其余大部分是从matplotlib's broken axes tutorial复制/粘贴的
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
exercise = sns.load_dataset("exercise")
f, (ax_top, ax_bottom) = plt.subplots(ncols=1, nrows=2, sharex=True, gridspec_kw={'hspace':0.05})
sns.violinplot(x="time", y="pulse", hue="kind",data=exercise, ax=ax_top)
sns.violinplot(x="time", y="pulse", hue="kind",data=exercise, ax=ax_bottom)
ax_top.set_ylim(bottom=125) # those limits are fake
ax_bottom.set_ylim(0,100)
sns.despine(ax=ax_bottom)
sns.despine(ax=ax_top, bottom=True)
ax = ax_top
d = .015 # how big to make the diagonal lines in axes coordinates
# arguments to pass to plot, just so we don't keep repeating them
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
ax.plot((-d, +d), (-d, +d), **kwargs) # top-left diagonal
ax2 = ax_bottom
kwargs.update(transform=ax2.transAxes) # switch to the bottom axes
ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs) # bottom-left diagonal
#remove one of the legend
ax_bottom.legend_.remove()
plt.show()