【问题标题】:Determine color code in correlation matrix确定相关矩阵中的颜色代码
【发布时间】:2019-11-21 15:24:11
【问题描述】:

我看到了如下代码。这是在具有 python 版本 3.7.3 的 Jupyter 上运行的。我在之前的教程中看到,列与自身的相关性为 1,因此在矩阵的那个单元格中看到了颜色 RED,但现在我看到自己尝试了一下,发现它是黄色的。这是因为颜色代码发生了变化还是由于 python 版本变化?如代码部分 "ax.matshow(corr)" 所示,这里似乎发生了一些变化。有没有办法定义我自己的颜色代码与强相关(0---->1)

def plot_corr(df,size):
    '''
    Function plots graphical corelation matrix for each pair of column in dataframe
    Input: 
          df: pandas dataframe
          size: vertical and horizontal size of the plot
    Displays: 
            matrix of corelation between columns. Blue-cyan-yellow-red-darkred => less to more corelated
                                                  0-------------------->1
                                                  Expect a dark red line running from top left to bottom right
    '''

    corr=df.corr()  #data frame corelation funnction
    fig,ax= plt.subplots(figsize=(size,size))
    ax.matshow(corr)  #color code the matrix rectangles by corelation value
    plt.xticks(range(len(corr.columns)),corr.columns) #draw xticks mark

【问题讨论】:

标签: python dataframe matplotlib correlation


【解决方案1】:

这可以使用Matplotlib Colormaps 来完成,可以在函数matshow 中添加参数cmap=plt.get_cmap(name)

例如如果您想使用红色的颜色图,例如 Reds,请运行

ax.matshow(corr, cmap=plt.get_cmap('Reds'))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-09
    • 2015-06-16
    • 1970-01-01
    • 2014-06-20
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多