英文:
How do I prevent a second plot on an axis from rescaling a seaborn FacetGrid?
问题
我正在尝试在分类小提琴图上叠加一个分类条带图。我绘制了小提琴图,然后是条带图,但坐标轴重新缩放到了条带图。我想保留小提琴图的原始坐标轴。
以下是小提琴图的代码片段:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
data = pd.DataFrame.from_dict({
'y_violin': list(range(16)),
'y_strip': [8] * 16,
'x': [1, 1, 0, 0] * 4,
'col': [1, 1, 1, 1, 0, 0, 0, 0] * 2,
'hue': [1] * 8 + [0] * 8,
})
g = sns.catplot(
kind='violin', data=data,
x='x', y='y_violin', col='col', hue='hue',
inner='points', dodge=False,
)
plt.show()
这将生成以下图像:
小提琴图
然后,我尝试在每个坐标轴上绘制更多点:
for col, ax in g.axes_dict.items():
sns.stripplot(
data=data.loc[data.col == col],
x='x', y='y_strip',
jitter=False, legend=False, palette=['#00FF00'], ax=ax
)
plt.show()
新的点位于正确的位置,但图形已重新缩放。如何保持第一次绘图的原始缩放?
重新缩放
英文:
I am trying to overlay a categorical strip plot on top of a categorical violin plot. I plot the violin plot, and then the strip plot, but the axes are rescaled to the strip plot. I would like to keep the original axes from the violin plots.
Here is the code snippet for the violin plot:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
data = pd.DataFrame.from_dict({
'y_violin': list(range(16)),
'y_strip': [8] * 16,
'x': [1, 1, 0, 0] * 4,
'col': [1, 1, 1, 1, 0, 0, 0, 0] * 2,
'hue': [1] * 8 + [0] * 8,
})
g = sns.catplot(
kind = 'violin', data = data,
x = 'x', y = 'y_violin', col = 'col', hue = 'hue',
inner = 'points', dodge = False,
)
plt.show()
which produces the following image:
Violin plot
Then, I try to plot some more points on each axis:
for col, ax in g.axes_dict.items():
sns.stripplot(
data = data.loc[data.col == col],
x = 'x', y = 'y_strip',
jitter = False, legend = False, palette = ['#00FF00'], ax = ax
)
plt.show()
The new points are in the correct location, however the figure is rescaled. How do I keep the original scaling from the first plotting?
Rescaled
答案1
得分: 2
以下是翻译好的部分:
一种选项是将set_ylim
设置为它们的原始值:
for col, ax in g.axes_dict.items():
ylim = ax.get_ylim()
sns.stripplot(
data=data.loc[data.col == col],
x='x', y='y_strip',
jitter=False, legend=False, palette=['#00FF00'], ax=ax
)
ax.set_ylim(ylim)
另外,最好使用.map_dataframe
而不是使用循环:
g = sns.catplot(kind='violin', data=data, x='x', y='y_violin', col='col', hue='hue', inner='points', dodge=False)
ylim = g.axes.flat[0].get_ylim()
g.map_dataframe(sns.stripplot, x='x', y='y_strip', jitter=False, legend=False, color='#00FF00')
g.set(ylim=ylim)
英文:
One option is to set_ylim
to their original values:
for col, ax in g.axes_dict.items():
ylim = ax.get_ylim()
sns.stripplot(
data = data.loc[data.col == col],
x = 'x', y = 'y_strip',
jitter = False, legend = False, palette = ['#00FF00'], ax = ax
)
ax.set_ylim(ylim)
As an aside, it's better to use .map_dataframe
than to use a loop.
g = sns.catplot(kind='violin', data=data, x='x', y='y_violin', col='col', hue='hue', inner='points', dodge=False)
ylim = g.axes.flat[0].get_ylim()
g.map_dataframe(sns.stripplot, x='x', y='y_strip', jitter=False, legend=False, color='#00FF00')
g.set(ylim=ylim)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论