【问题标题】:Seaborn Jointplot add colors for each classSeaborn Jointplot 为每个类添加颜色
【发布时间】:2018-12-15 02:17:58
【问题描述】:

我想使用 seaborn jointplot 绘制 2 个变量的相关图。我尝试了很多不同的东西,但我无法根据班级为点添加颜色。

这是我的代码:

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
sns.set()

X = np.array([5.2945 , 3.6013 , 3.9675 , 5.1602 , 4.1903 , 4.4995 , 4.5234 ,
              4.6618 , 0.76131, 0.42036, 0.71092, 0.60899, 0.66451, 0.55388,
              0.63863, 0.62504, 0.     , 0.     , 0.49364, 0.44828, 0.43066,
              0.57368, 0.     , 0.     , 0.64824, 0.65166, 0.64968, 0.     ,
              0.     , 0.52522, 0.58259, 1.1309 , 0.     , 0.     , 1.0514 ,
              0.7519 , 0.78745, 0.94873, 1.0169 , 0.     , 0.     , 1.0416 ,
              0.     , 0.     , 0.93648, 0.92801, 0.     , 0.     , 0.89594,
              0.     , 0.80455, 1.0103 ])

y = np.array([ 93, 115, 107, 115, 110, 107, 102, 113,  95, 101, 116,  74, 102,
               102,  78,  85, 108, 110, 109,  80,  91,  88,  99, 110, 108,  96,
               105,  93, 107,  98,  88,  75, 106,  92,  82,  84,  84,  92, 115,
               107,  97, 115,  85, 133, 100,  65,  96, 105, 112, 107, 107, 105])

ax = sns.jointplot(X, y, kind='reg' )
ax.set_axis_labels(xlabel='Brain scores', ylabel='Cognitive scores')
plt.tight_layout()
plt.show()

现在,我想根据类变量classes为每个点添加颜色。

【问题讨论】:

  • 请不要在问题内回答您自己的问题。如果您认为现有答案不能回答问题,并认为您有更好/不同的解决方案,请提供该解决方案作为您问题的答案。
  • 我同意。我只是按照你的建议做了。

标签: python matplotlib seaborn scatter-plot


【解决方案1】:

显而易见的解决方案是让regplot 只绘制回归线,而不是点,并通过通常的散点图添加这些点,散点图的颜色为c 参数。

g = sns.jointplot(X, y, kind='reg', scatter = False )
g.ax_joint.scatter(X,y, c=classes)

【讨论】:

  • 根据您的想法,我设法解决了我的问题。现在,在最后一步,有没有办法为颜色编码添加图例?
  • 这是一个常见问题,因为目前还没有直接简单的解决方案来为散点图添加图例。可能的方法是例如this onethis onethis one。 matplotlib may contain a better solution 我设计的未来版本。
  • 感谢您的参考
【解决方案2】:

我设法找到了一个正是我需要的解决方案。感谢@ImportanceOfBeingErnest 让我想到让regplot 只画回归线。

解决方案:

import pandas as pd

classes = np.array([1., 1., 1., 1., 1., 1., 1., 1., 2., 2., 2., 2., 2., 2., 2.,
                    2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 
                    2., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 
                    3., 3., 3., 3., 3., 3., 3.])

df = pd.DataFrame(map(list, zip(*[X.T, y.ravel().T])))
df = df.reset_index()
df['index'] = classes[:]

g = sns.jointplot(X, y, kind='reg', scatter = False )
for i, subdata in df.groupby("index"):
    sns.kdeplot(subdata.iloc[:,1], ax=g.ax_marg_x, legend=False)
    sns.kdeplot(subdata.iloc[:,2], ax=g.ax_marg_y, vertical=True, legend=False)
    g.ax_joint.plot(subdata.iloc[:,1], subdata.iloc[:,2], "o", ms = 8)
plt.tight_layout()
plt.show()

【讨论】:

    【解决方案3】:

    以欧内斯特的回答为基础:

    sns.jointplot 中设置scatter = False 后,使用sns.scatterplot 构建散点图,hue = classes 参数等于分类变量数组。我发现将您的数​​据合并到具有 xyclasses 列的 pandas 数据框中最干净,并将其用作散点图的 data,但您不必这样做。 ..

    classes = np.array([1., 1., 1., 1., 1., 1., 1., 1., 2., 2., 2., 2., 2., 2., 2.,
                        2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 
                        2., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 
                        3., 3., 3., 3., 3., 3., 3.])
    
    # make them look a little more 'categorical'
    classes = classes.astype('int')
    
    x = np.array([5.2945 , 3.6013 , 3.9675 , 5.1602 , 4.1903 , 4.4995 , 4.5234 ,
                  4.6618 , 0.76131, 0.42036, 0.71092, 0.60899, 0.66451, 0.55388,
                  0.63863, 0.62504, 0.     , 0.     , 0.49364, 0.44828, 0.43066,
                  0.57368, 0.     , 0.     , 0.64824, 0.65166, 0.64968, 0.     ,
                  0.     , 0.52522, 0.58259, 1.1309 , 0.     , 0.     , 1.0514 ,
                  0.7519 , 0.78745, 0.94873, 1.0169 , 0.     , 0.     , 1.0416 ,
                  0.     , 0.     , 0.93648, 0.92801, 0.     , 0.     , 0.89594,
                  0.     , 0.80455, 1.0103 ])
    
    y = np.array([ 93, 115, 107, 115, 110, 107, 102, 113,  95, 101, 116,  74, 102,
                   102,  78,  85, 108, 110, 109,  80,  91,  88,  99, 110, 108,  96,
                   105,  93, 107,  98,  88,  75, 106,  92,  82,  84,  84,  92, 115,
                   107,  97, 115,  85, 133, 100,  65,  96, 105, 112, 107, 107, 105])
    
    sns.jointplot(x, y, kind='reg', scatter = False )
    sns.scatterplot(x, y, hue=classes)
    

    【讨论】:

      【解决方案4】:
             label  Method 2  Method 1
      0    Label 2  1.484914 -1.069439
      1    Label 2  0.273158  1.139414
      2    Label 2  1.089244  0.161752
      3    Label 2  1.184306 -0.981758
      4    Label 2  1.424435  0.300742
      ..       ...       ...       ...
      111  Label 2 -0.201226  0.852319
      112  Label 2  0.016911  0.985805
      113  Label 2 -0.263775  0.248942
      114  Label 2  3.283341 -1.247014
      115  Label 2  0.325648  1.793694
      [116 rows x 3 columns]
      
      sns.jointplot(data=data, x="Method 1, y="Method 2", "hue="label", palette={
          'Label 1': '#d7191c',
          'Label 2': '#2b83ba'
      })
      

      使用joint_kws={"alpha": 0.5} 设置透明度。

      示例图:

      【讨论】:

        猜你喜欢
        • 2017-06-11
        • 2022-01-23
        • 2023-04-02
        • 2021-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-28
        • 1970-01-01
        相关资源
        最近更新 更多