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

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

How do I prevent a second plot on an axis from rescaling a seaborn FacetGrid?

问题

我正在尝试在分类小提琴图上叠加一个分类条带图。我绘制了小提琴图,然后是条带图,但坐标轴重新缩放到了条带图。我想保留小提琴图的原始坐标轴。

以下是小提琴图的代码片段:

  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. import seaborn as sns
  4. data = pd.DataFrame.from_dict({
  5. 'y_violin': list(range(16)),
  6. 'y_strip': [8] * 16,
  7. 'x': [1, 1, 0, 0] * 4,
  8. 'col': [1, 1, 1, 1, 0, 0, 0, 0] * 2,
  9. 'hue': [1] * 8 + [0] * 8,
  10. })
  11. g = sns.catplot(
  12. kind='violin', data=data,
  13. x='x', y='y_violin', col='col', hue='hue',
  14. inner='points', dodge=False,
  15. )
  16. plt.show()

这将生成以下图像:
小提琴图

然后,我尝试在每个坐标轴上绘制更多点:

  1. for col, ax in g.axes_dict.items():
  2. sns.stripplot(
  3. data=data.loc[data.col == col],
  4. x='x', y='y_strip',
  5. jitter=False, legend=False, palette=['#00FF00'], ax=ax
  6. )
  7. 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:

  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. import seaborn as sns
  4. data = pd.DataFrame.from_dict({
  5. 'y_violin': list(range(16)),
  6. 'y_strip': [8] * 16,
  7. 'x': [1, 1, 0, 0] * 4,
  8. 'col': [1, 1, 1, 1, 0, 0, 0, 0] * 2,
  9. 'hue': [1] * 8 + [0] * 8,
  10. })
  11. g = sns.catplot(
  12. kind = 'violin', data = data,
  13. x = 'x', y = 'y_violin', col = 'col', hue = 'hue',
  14. inner = 'points', dodge = False,
  15. )
  16. plt.show()

which produces the following image:
Violin plot

Then, I try to plot some more points on each axis:

  1. for col, ax in g.axes_dict.items():
  2. sns.stripplot(
  3. data = data.loc[data.col == col],
  4. x = 'x', y = 'y_strip',
  5. jitter = False, legend = False, palette = ['#00FF00'], ax = ax
  6. )
  7. 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设置为它们的原始值:

  1. for col, ax in g.axes_dict.items():
  2. ylim = ax.get_ylim()
  3. sns.stripplot(
  4. data=data.loc[data.col == col],
  5. x='x', y='y_strip',
  6. jitter=False, legend=False, palette=['#00FF00'], ax=ax
  7. )
  8. ax.set_ylim(ylim)

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

另外,最好使用.map_dataframe而不是使用循环:

  1. g = sns.catplot(kind='violin', data=data, x='x', y='y_violin', col='col', hue='hue', inner='points', dodge=False)
  2. ylim = g.axes.flat[0].get_ylim()
  3. g.map_dataframe(sns.stripplot, x='x', y='y_strip', jitter=False, legend=False, color='#00FF00')
  4. g.set(ylim=ylim)
英文:

One option is to set_ylim to their original values:

  1. for col, ax in g.axes_dict.items():
  2. ylim = ax.get_ylim()
  3. sns.stripplot(
  4. data = data.loc[data.col == col],
  5. x = 'x', y = 'y_strip',
  6. jitter = False, legend = False, palette = ['#00FF00'], ax = ax
  7. )
  8. ax.set_ylim(ylim)

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


As an aside, it's better to use .map_dataframe than to use a loop.

  1. g = sns.catplot(kind='violin', data=data, x='x', y='y_violin', col='col', hue='hue', inner='points', dodge=False)
  2. ylim = g.axes.flat[0].get_ylim()
  3. g.map_dataframe(sns.stripplot, x='x', y='y_strip', jitter=False, legend=False, color='#00FF00')
  4. 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:

确定