使用Python在Seaborn中设置散点图中的点大小

时间:2019-01-01 10:02:54

标签: python-3.x size seaborn scatter-plot

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

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

有关可重现的示例:

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()

1 个答案:

答案 0 :(得分:2)

您的代码非常接近:大小太小而无法轻易看到这些点。此处使用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()

结果如下:enter image description here