【问题标题】:Setting the size of points in scatterplot in Seaborn with Python使用 Python 在 Seaborn 中设置散点图中点的大小
【发布时间】:2019-05-28 09:44:29
【问题描述】:

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

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

对于一个可重现的例子:

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-3.x size seaborn scatter-plot


    【解决方案1】:

    您的代码非常接近:尺寸太小,无法轻松看到这些点。这里的代码使用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()
    

    结果如下:

    【讨论】:

      猜你喜欢
      • 2021-02-09
      • 1970-01-01
      • 2016-08-23
      • 2019-09-26
      • 2020-05-14
      • 1970-01-01
      • 2020-12-04
      • 2019-03-18
      • 1970-01-01
      相关资源
      最近更新 更多