简体   繁体   English

如何更改 seaborn 对图中 plot 元素的 z 顺序

[英]How to change the z-order of the plot elements in a seaborn pairplot

Here is a snippet, to reproduce my example image:这是一个片段,用于重现我的示例图像:

import pandas as pd
import numpy as np
import seaborn as sns

np.random.seed(42)
df = pd.DataFrame(np.random.rand(10,2), columns=['x', 'y'])
df['label'] = ['cat', 'mouse', 'dog', 'mouse', 'cat', 'cat', 'mouse', 'mouse','dog', 'cat']
sns.pairplot(df, hue='label');

It produces the following seaborn pair plot, with some dummy data:它产生以下 seaborn 对 plot,以及一些虚拟数据:

在此处输入图像描述

In the upper right plot, one marker of the 'dog' category is below an overlaying marker of the 'mouse' category.在右上角 plot 中,“狗”类别的一个标记位于“鼠标”类别的覆盖标记下方。

Can I somehow change the z-order of the scatter plot markers, so that all markers of the 'dog' category are best visible on top?我可以以某种方式更改散点图 plot 标记的 z 顺序,以便“狗”类别的所有标记在顶部最清晰可见吗?

edit: I already tried hue_order=['mouse', 'cat', 'dog'] and hue_order=['dog', 'mouse', 'cat'] , but they only influence the order in the legend and the color.编辑:我已经尝试过hue_order=['mouse', 'cat', 'dog']hue_order=['dog', 'mouse', 'cat'] ,但它们只影响图例中的顺序和颜色。 Not the z-order of the markers in the scatter plot.不是散点图 plot 中标记的 z 顺序。

  • This answer provides a full explanation of my comment from 2022-01-07.这个答案提供了对我 2022-01-07 评论的完整解释。
  • The dots are drawn in the order they are encountered in the dataframe.这些点是按照它们在 dataframe 中遇到的顺序绘制的。 You can change that order, eg via pd.concat([df[df['label'],= 'dog'], df[df['label'] == 'dog']]) .您可以更改该顺序,例如通过pd.concat([df[df['label'],= 'dog'], df[df['label'] == 'dog']])
  • The order in the dataframe also sets the default order of the labels in the legend. dataframe 中的顺序还设置了图例中标签的默认顺序。 Further note that in future seaborn versions this behavior might change.进一步注意,在未来的 seaborn 版本中,此行为可能会改变。
import pandas as pd
import numpy as np
import seaborn as sns

np.random.seed(42)
df = pd.DataFrame(np.random.rand(10, 2), columns=['x', 'y'])
df['label'] = ['cat', 'mouse', 'dog', 'mouse', 'cat', 'cat', 'mouse', 'mouse', 'dog', 'cat']
sns.pairplot(pd.concat([df[df['label'] != 'dog'], df[df['label'] == 'dog']]), hue='label')

sns.pairplot 改变点的顺序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM