设置 seaborn 散点图中点的大小

问题描述 投票:0回答:1

我正在尝试根据代表其标签的列的值来设置点的大小,但我得到的是一个空图。

此外,我想知道如何统一设置点的大小(即不考虑第三列的值)。

对于可重现的例子:

plot_data.to_json()
'{"x1":{"0":-0.2019455769,"1":0.1350610218,"2":-0.1128417956,"3":-0.1481016799,"4":0.1293273221,"5":-0.0266437776,"6":0.0100572041,"7":0.0037355635,"8":-0.0203400136,"9":0.1363267107},"x2":{"0":-0.1938001473,"1":-0.1353617432,"2":-0.0381057072,"3":-0.0874488661,"4":-0.2152329772,"5":0.0275324833,"6":-0.174604808,"7":-0.1872132566,"8":0.1172552524,"9":0.0166454137},"label":{"0":1,"1":0,"2":1,"3":0,"4":0,"5":1,"6":0,"7":0,"8":1,"9":0}}'

plt.figure(figsize = (20, 10))
sns.scatterplot(x ='x1', y='x2', hue = 'label', size = 'label', sizes = {0:1, 1:3} , data = plot_data)
plt.axis('equal')
plt.show()
python seaborn size scatter-plot
1个回答
4
投票

您的代码非常接近:尺寸太小而无法使点容易看到。这里用

sizes=(40, 40)
编码,这使得最小和最大大小相同(参见 docs)并给出统一的点大小:

import pandas as pd, seaborn as sns, matplotlib.pyplot as plt

plot_data = pd.read_json('{"x1":{"0":-0.2019455769,"1":0.1350610218,"2":-0.1128417956,"3":-0.1481016799,"4":0.1293273221,"5":-0.0266437776,"6":0.0100572041,"7":0.0037355635,"8":-0.0203400136,"9":0.1363267107},"x2":{"0":-0.1938001473,"1":-0.1353617432,"2":-0.0381057072,"3":-0.0874488661,"4":-0.2152329772,"5":0.0275324833,"6":-0.174604808,"7":-0.1872132566,"8":0.1172552524,"9":0.0166454137},"label":{"0":1,"1":0,"2":1,"3":0,"4":0,"5":1,"6":0,"7":0,"8":1,"9":0}}')

plt.figure(figsize = (10, 5))
sns.scatterplot(x='x1', y='x2', hue='label', size='label', sizes=(40, 40),
  data=plot_data)
plt.axis('equal')
plt.show()

这里是结果:

© www.soinside.com 2019 - 2024. All rights reserved.