Search code examples
pythonseabornsizescatter-plot

Setting the size of points in a seaborn scatterplot


I am trying to set the size of the points in accordance with the value of a column that represents their labels but I am getting an empty plot.

Moreover I wonder how I can set the size of the points uniformly (i.e. regardless of the value of the third column).

For a reproducible example:

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

Solution

  • Your code was quite close: the sizes were just too small to make the points easily visible. Here code with sizes=(40, 40), which makes the minimum and maximum size the same (see docs) and gives uniform point size:

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

    Here the result:enter image description here