- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我喜欢 PerformanceAnalytics
R 包的 chart.Correlation
function 中的这个相关矩阵:
我如何在 Python 中创建它?我见过的相关矩阵图主要是热图,例如 this seaborn
example .
最佳答案
另一种解决方案是
import matplotlib.pyplot as plt
import seaborn as sns
def corrdot(*args, **kwargs):
corr_r = args[0].corr(args[1], 'pearson')
corr_text = f"{corr_r:2.2f}".replace("0.", ".")
ax = plt.gca()
ax.set_axis_off()
marker_size = abs(corr_r) * 10000
ax.scatter([.5], [.5], marker_size, [corr_r], alpha=0.6, cmap="coolwarm",
vmin=-1, vmax=1, transform=ax.transAxes)
font_size = abs(corr_r) * 40 + 5
ax.annotate(corr_text, [.5, .5,], xycoords="axes fraction",
ha='center', va='center', fontsize=font_size)
sns.set(style='white', font_scale=1.6)
iris = sns.load_dataset('iris')
g = sns.PairGrid(iris, aspect=1.4, diag_sharey=False)
g.map_lower(sns.regplot, lowess=True, ci=False, line_kws={'color': 'black'})
g.map_diag(sns.distplot, kde_kws={'color': 'black'})
g.map_upper(corrdot)
现在,如果您真的想要模仿那个 R 图的外观,您可以将上面的内容与您提供的一些解决方案结合起来:
import matplotlib.pyplot as plt
from scipy import stats
import seaborn as sns
import numpy as np
def corrdot(*args, **kwargs):
corr_r = args[0].corr(args[1], 'pearson')
corr_text = round(corr_r, 2)
ax = plt.gca()
font_size = abs(corr_r) * 80 + 5
ax.annotate(corr_text, [.5, .5,], xycoords="axes fraction",
ha='center', va='center', fontsize=font_size)
def corrfunc(x, y, **kws):
r, p = stats.pearsonr(x, y)
p_stars = ''
if p <= 0.05:
p_stars = '*'
if p <= 0.01:
p_stars = '**'
if p <= 0.001:
p_stars = '***'
ax = plt.gca()
ax.annotate(p_stars, xy=(0.65, 0.6), xycoords=ax.transAxes,
color='red', fontsize=70)
sns.set(style='white', font_scale=1.6)
iris = sns.load_dataset('iris')
g = sns.PairGrid(iris, aspect=1.5, diag_sharey=False, despine=False)
g.map_lower(sns.regplot, lowess=True, ci=False,
line_kws={'color': 'red', 'lw': 1},
scatter_kws={'color': 'black', 's': 20})
g.map_diag(sns.distplot, color='black',
kde_kws={'color': 'red', 'cut': 0.7, 'lw': 1},
hist_kws={'histtype': 'bar', 'lw': 2,
'edgecolor': 'k', 'facecolor':'grey'})
g.map_diag(sns.rugplot, color='black')
g.map_upper(corrdot)
g.map_upper(corrfunc)
g.fig.subplots_adjust(wspace=0, hspace=0)
# Remove axis labels
for ax in g.axes.flatten():
ax.set_ylabel('')
ax.set_xlabel('')
# Add titles to the diagonal axes/subplots
for ax, col in zip(np.diag(g.axes), iris.columns):
ax.set_title(col, y=0.82, fontsize=26)
这与 chart.Correlation()
在 R 中绘制 iris
数据集的方式非常接近:
library(PerformanceAnalytics)
chart.Correlation(data.matrix(iris[, -5]), histogram = TRUE, pch=20)
关于python - 相关矩阵图,一侧是系数,另一侧是散点图,对角线分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48139899/
我有一个 6x6 数组,并且希望始终获取接下来的四个值。举个例子: 0----- -1---- --2--- ---3-- ----4- 所以我想得到所有对角线的 (0+1+2+3) 和 (1+2+3
我想遍历一个已转换为一维的二维方阵。 问题是我想遍历它,就像我在对角条中遍历原始 2D 一样。 该数组是对角数组,我最初使用一维的 malloc 创建它,以避免分配太多内存。 数组的大小: int T
“给定一个 2D 字符数组和一个字符串。 查找特定字符串是否出现在矩阵的对角线上。 private static boolean diagonalContains(char[][] grid,Stri
我有一个由 nxn 矩阵组成的 njxnj 矩阵。我想提取 nxn 矩阵的对角 j block 。即我想提取对角线(对于 n = 2,j = 4): 最有效的方法是什么? 最佳答案 要为元素建立索引,
这是一个 self 回答的问题。给定两个数据框, x 0 1 0 1 2 1 3 4 y 0 1 2 0 5 6 7 1 8 9 X 2 Y Z 0 x 和
我试图让我的程序打印出不等于幻方规则的行、列或对角线,例如,如果矩阵是 1 9 5 2 4 3 6 8 7 第 1 行 [2, 4, 3] 不起作用 第 2 行 [6, 8, 7] 不起作用 第 0
所以我有一个像这样的 4x4 矩阵 |0 1 2 3 -+------- 0|0 1 3 6 1|2 4 7 a 2|5 8 b d 3|9 c e f 并且我是按照其中的十六进制字符指定的顺序遍历
什么是获取正方形DataFrame的对角线的有效方法。我希望结果是一个 Series 和一个 MultiIndex 有两个级别,第一个是 DataFrame 的索引,第二个级别是DataFrame 的
问题:我正在尝试在 SwiftUI 中以矩形呈现对角线线性渐变。 我实现了一个标准的多点线性渐变,它在呈现为正方形时效果很好,但是当我将框架更改为矩形时,它有一些奇怪的行为,看起来更水平,或者有一些奇
我目前正在尝试找到一种在 C# for Excel 中使用 VSTO 的方法,以使用 C# 代码在单元格中绘制对角线。但我在网上找不到任何人甚至试图这样做。 有谁知道如何实现这一目标? 谢谢 (对不起
我需要删除图像中的线条,这最终是一个表格。我找到了一种删除水平线和垂直线的方法: convert 1.jpg -type Grayscale -negate -define morphology:co
我有一个如下所示的矩阵: ` matrix = [ ['P', 'o', 'P', 'o', 'P'], ['m', 'i', 'c', 's', 'r'], ['g', 'a', 'T', 'A',
如何在Python中按如下方式堆叠矩阵,使得父矩阵的元素在子矩阵的相同 block 对角点处形成 block 对角线。例子:我有四个矩阵 AA,AB,BA,BB 我想制作如附图所示的矩阵。 最佳答案
我在做一些统计。 我有数据框: tag a b c d e f a 5 2 3 2 0 1 b 2 4 3 2 0 1
我最近做了一个 Connect4 游戏,当我的 Connect4 向右对角线连接时,我的 Connect4 没有赢得游戏。并且它只适用于某些组合,当它连接到左边的对角线时。坐标:- 左上角:(0,0)
在 numpy 中有什么方法可以获取对角数组的引用吗?我希望我的数组对角线除以某个因子谢谢 最佳答案 如果 X 是你的数组,c 是因子, X[np.diag_indices_from(X)] /= c
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我有一个形状为 (m*n, m*n) 的张量,我想提取一个大小为 (n, m*n) 的张量,其中包含对角线上大小为 n*n 的 m 个块。例如: >>> a tensor([[1, 2, 0, 0],
我目前正在使用 matplotlib/pyplot 绘制 3d 对象,如下所示: fig = plt.figure().gca(projection='3d') plot = fig.plot_sur
好的,让我们考虑一个 64 位的数字,它的位组成一个 8x8 的表。 例如 0 1 1 0 1 0 1 0 0 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 0 1 1 0 1 0 1
我是一名优秀的程序员,十分优秀!