python - 确定相关矩阵中的颜色代码
问题描述
我看到了如下代码。这是在具有 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
解决方案
这可以使用Matplotlib Colormaps来完成,它可以使用matshow
参数添加到函数中cmap=plt.get_cmap(name)
。
例如,如果您想使用红色颜色图,例如Reds
,运行
ax.matshow(corr, cmap=plt.get_cmap('Reds'))
推荐阅读
- reactjs - 如何解决本机反应中的以下错误?
- javascript - 尝试遍历 DOM 节点,并获取文本格式的值
- javascript - Angular中mat-select中的垃圾箭头
- python - 如何根据测量维度设置opencv卡尔曼滤波器的测量矩阵【OpenCV+Python】
- angular - 带有过滤器项的角度删除
- splunk - 为任何 Splunk 的 REST 端点创建一个 REST 处理程序
- javascript - 填充 amCharts 行路径并移除 yAxes 值
- kubernetes - kubernetes (minikube) 中的 Fabric v2.0 - 在 peer 内部运行 docker 以运行链代码时出现问题
- xaml - UWP:ElementName 绑定在 EntranceThemeTransition 中不起作用
- javascript - Linio 挑战 - 打印数字 bw 1 到 100 分别用 linio、IT、liianos 替换 3 的倍数、5 的倍数和 3 和 5 的倍数