[英]How to change z-order of plotting in seaborn pairplot
The following code produces a seaborn pairplot.以下代码生成 seaborn 对图。
How can I achieve that the red point (with b = 10.
) is visible in the subplot c/a (left bottom)?如何实现红点(
b = 10.
)在子图 c/a(左下角)中可见?
Presently it is almost invisible as the points with b = 4
and b = 5
seem to be plotted afterwards and hide it.目前它几乎不可见,因为
b = 4
和b = 5
的点似乎后来被绘制并隐藏了。
Sorting the DataFrame unfortunately does not help.不幸的是,对 DataFrame 进行排序没有帮助。
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
def supplyHueByB(x, bMax):
amountOfSegments = 8
myReturn = int(x * amountOfSegments / bMax)
return myReturn
myList = [
[0.854297, 1.973376, 0.187038],
[0.854297, 2.204028, 0.012476],
[0.854297, 10.0, 0.056573],
[0.854297, 5.0, 0.050635],
[0.854297, 4.0, 0.058926]
]
df = pd.DataFrame(myList)
df.columns=['a', 'b', 'c']
bMax = df.b.max()
hue = df.b.apply(lambda x: supplyHueByB(x, bMax))
g = sns.pairplot(
df,
corner=True,
diag_kws=dict(color=".6"),
vars=['a', 'b', 'c'],
plot_kws=dict(
hue=hue,
palette="coolwarm",
edgecolor='None',
s=80 # size
),
)
plt.subplots_adjust(bottom=0.1)
g.add_legend()
plt.show()
df
and hue
have to be sorted in tandem: df
和hue
必须串联排序:
>>> g = sns.pairplot(
... df.sort_values('b'),
... corner=True,
... diag_kws=dict(color=".6"),
... vars=['a', 'b', 'c'],
... plot_kws=dict(
... hue=sorted(hue),
... palette="coolwarm",
... edgecolor='None',
... s=80 # size
... ),
... )
The above produces the desired output, ie, the red point is plotted after the light blue ones.以上产生了所需的 output,即红点绘制在浅蓝色点之后。 In this example, using
sort_values
and sorted
does the trick.在这个例子中,使用
sort_values
和sorted
就可以了。 For a custom order for plotting the points, one may need to be more creative, but the key principle remains that the ordering of the df
should be consistent to that of hue
.对于绘制点的自定义顺序,可能需要更有创意,但关键原则仍然是
df
的顺序应该与hue
的顺序一致。
I was surprised to see that Seaborn does not perform the layering (ie, which point goes above another point) in the order that the points are passed, since Matplotlib definitely does that, and Seaborn is built atop Matplotlib. I was surprised to see that Seaborn does not perform the layering (ie, which point goes above another point) in the order that the points are passed, since Matplotlib definitely does that, and Seaborn is built atop Matplotlib.
Following Matplotlib's ordering, you would want the point [a=0.854297, c=0.056573]
(ie, the point being hidden) to be plotted after the other two points close to it [a=0.854297, c=0.050635]
and [a=0.854297, c=0.058926]
.按照 Matplotlib 的顺序,您希望点
[a=0.854297, c=0.056573]
(即被隐藏的点)绘制在靠近它的其他两个点[a=0.854297, c=0.050635]
和[a=0.854297, c=0.058926]
。 This is so that [a=0.854297, c=0.056573]
is plotted last and hence not masked.这是为了使
[a=0.854297, c=0.056573]
最后绘制,因此不会被屏蔽。
Since Seaborn does not seem to do this out of the box, I reordered [a=0.854297, c=0.056573]
to be plotted last.由于 Seaborn 似乎没有开箱即用,我重新排序
[a=0.854297, c=0.056573]
最后绘制。
# layer_orders is the order (first to last) in which we want the points to be plotted.
layer_order = [0, 1, 3, 4, 2]
# Extracting df['a'] and df['c'] in the order we want.
a = df['a'][layer_order]
c = df['c'][layer_order]
# Highlighting the last point in red to show it is not hidden.
colors = ['blue'] * 4 + ['red']
# Axis 3 is where we have the problem. Clearing its contents first.
g.figure.axes[3].clear()
g.figure.axes[3].scatter(a, c, color=colors)
This will give you a plot that looks like this:这将为您提供如下所示的 plot:
You might want to refactor the code to be better, but I hope this gives you the underlying idea.您可能希望重构代码变得更好,但我希望这能给您提供基本的想法。
[I have plotted the points in blue and red, but you can change them to the hex values that you like to match the other Seaborn plots.] [我用蓝色和红色绘制了这些点,但您可以将它们更改为您喜欢的十六进制值,以匹配其他 Seaborn 图。]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.