如何更改 seaborn pairplot 中的绘图顺序?
How to change order of plotting in seaborn pairplot?
以下代码生成一个 seaborn pairplot。
我怎样才能使红点(b = 10.
)在子图 c/a(左下角)中可见?
目前它几乎不可见,因为带有 b = 4
和 b = 5
的点似乎是后来绘制并隐藏的。
不幸的是,对 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()
我很惊讶地看到 Seaborn 没有按照点的传递顺序执行分层(即哪个点在另一个点之上),因为 Matplotlib 确实这样做了,而且 Seaborn 是在 Matplotlib 之上构建的。
按照 Matplotlib 的顺序,您可能希望点 [a=0.854297, c=0.056573]
(即被隐藏的点)在靠近它的其他两个点 [a=0.854297, c=0.050635]
和 [a=0.854297, c=0.058926]
之后绘制。这样 [a=0.854297, c=0.056573]
最后绘制,因此不会被屏蔽。
由于 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)
这会给你一个看起来像这样的情节:
您可能想重构代码以使其更好,但我希望这能为您提供基本思路。
[我用蓝色和红色绘制了点,但您可以将它们更改为您喜欢的十六进制值以匹配其他 Seaborn 图。]
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
... ),
... )
以上生成了所需的输出,即红点绘制在浅蓝色点之后。在这个例子中,使用 sort_values
和 sorted
就可以了。对于绘制点的自定义顺序,可能需要更有创意,但关键原则仍然是 df
的顺序应与 hue
.
的顺序一致
以下代码生成一个 seaborn pairplot。
我怎样才能使红点(b = 10.
)在子图 c/a(左下角)中可见?
目前它几乎不可见,因为带有 b = 4
和 b = 5
的点似乎是后来绘制并隐藏的。
不幸的是,对 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()
我很惊讶地看到 Seaborn 没有按照点的传递顺序执行分层(即哪个点在另一个点之上),因为 Matplotlib 确实这样做了,而且 Seaborn 是在 Matplotlib 之上构建的。
按照 Matplotlib 的顺序,您可能希望点 [a=0.854297, c=0.056573]
(即被隐藏的点)在靠近它的其他两个点 [a=0.854297, c=0.050635]
和 [a=0.854297, c=0.058926]
之后绘制。这样 [a=0.854297, c=0.056573]
最后绘制,因此不会被屏蔽。
由于 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)
这会给你一个看起来像这样的情节:
您可能想重构代码以使其更好,但我希望这能为您提供基本思路。
[我用蓝色和红色绘制了点,但您可以将它们更改为您喜欢的十六进制值以匹配其他 Seaborn 图。]
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
... ),
... )
以上生成了所需的输出,即红点绘制在浅蓝色点之后。在这个例子中,使用 sort_values
和 sorted
就可以了。对于绘制点的自定义顺序,可能需要更有创意,但关键原则仍然是 df
的顺序应与 hue
.