如何防止在 seaborn FacetGrid 上的轴上绘制第二个图时重新缩放轴?

huangapple go评论66阅读模式
英文:

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)

输出:
如何防止在 seaborn FacetGrid 上的轴上绘制第二个图时重新缩放轴?

另外,最好使用.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)

Output:
如何防止在 seaborn FacetGrid 上的轴上绘制第二个图时重新缩放轴?


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)

huangapple
  • 本文由 发表于 2023年6月16日 03:34:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定