Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我的相关矩阵显示的是全白色图片?_Python_Python 3.x_Matplotlib_Seaborn_Correlation - Fatal编程技术网

Python 为什么我的相关矩阵显示的是全白色图片?

Python 为什么我的相关矩阵显示的是全白色图片?,python,python-3.x,matplotlib,seaborn,correlation,Python,Python 3.x,Matplotlib,Seaborn,Correlation,我使用以下代码绘制了相关矩阵: sns.set_theme(style="white") # Compute the correlation matrix corr = final_df.T.corr() # Generate a mask for the upper triangle mask = np.triu(np.ones_like(corr, dtype=bool)) # Set up the matplotlib figure f, ax = plt.sub

我使用以下代码绘制了相关矩阵:

sns.set_theme(style="white")

# Compute the correlation matrix
corr = final_df.T.corr()

# Generate a mask for the upper triangle
mask = np.triu(np.ones_like(corr, dtype=bool))

# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(24, 12))

# Generate a custom diverging colormap
cmap = sns.diverging_palette(230, 20, as_cmap=True)
# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3, center=0,
            square=True, linewidths=.5, cbar_kws={"shrink": .5})
结果是:

我不明白为什么会这样。我尝试了另一种方法:

corr = df.corr()
fig, ax = plt.subplots(figsize=(24, 12))
ax.matshow(corr, vmin=0,vmax=1)
plt.xticks(range(len(corr.columns)), corr.columns, rotation='vertical', fontsize=8);
plt.yticks(range(len(corr.columns)), corr.columns, fontsize=8);
但结果仍然是:

我的数据似乎不坏。下面是相关矩阵的样子:

有人能解释一下为什么会发生这种情况,以及如何解决它吗

事先非常感谢

编辑

回应@mwaskom的评论。我删除了
linewidth=.5
参数,问题似乎部分解决了。我之所以这样说,部分原因是因为一些点开始出现在情节中,但不可能真实地想象任何东西

这是删除
linewidth
参数后得到的结果:

我试过用一个更大的无花果尺寸(36,24)在它(24,12)之前,但它仍然是一样的。这是一个截图(截图无法捕获整个数字)

有没有办法让这个图有助于可视化相关性

更新

使用Arty的答案得出的结果仍然非常相似。以下是截图:

我在评论中也尝试了约翰的建议。这一次更接近产生有意义的结果,但仍然:

您有什么建议吗


谢谢你

我想你只有一种方法可以看到很多有意义的热图,因为我尝试用非常小的数组复制你的图片,并成功了。尝试一次显示较少的值。同时设置
vmin=-1,vmax=1
。我的代码是:

输出:


您需要删除
线宽=.5
;看起来您的数据太密集,无法在单元格之间画出非常细的线,而这些线无法填满空间。您好@mwaskom谢谢您的回答。我试过你说的话,但仍然不可能从那个阴谋中得到一些有用的信息。我已经编辑了问题以更新我现在的结果。请问您还有什么其他建议来改进绘图可视化吗?感谢您查看删除不感兴趣的行和列的方法。由于行太多,您需要
annot=False
(这也可以避开变量、
extreme_1
等以及该帖子中的注释矩阵)。您可以将
cut\u off=
变量设置为一个更大的数字,以删除更多的行/列。您的热图没有意义,看起来像灰色三角形,只是因为有很多值。您有大约100x100个值。要看到视觉效果,请尝试使用10x10的值数组绘制热图。你会发现一切都是正确和美好的。您只需要一次输出较少的值。您好@Arty非常感谢您的回答。我尝试了你的建议,但还是得到了同样的结果。我已经更新了问题,以显示我所做的got@Miguel2488你问题中的最后一张图片有什么问题?约翰的那个。看起来像是正确的东西。你有一张稀疏的热图。因为你有很多元素,所以你有非常小的正方形。在我的回答中,正方形非常大,因为只有3x3个值。在您的情况下,尝试从非常小的阵列开始。例如,为10x10阵列绘制图像。试着在我的答案中使用我的代码,它几乎和你的一样。把你的10x10数据输入我的代码。你会看到非常有意义的结果。
import numpy as np, matplotlib.pyplot as plt, seaborn as sns

sns.set_theme(style="white")

# Compute the correlation matrix
corr = np.array([[1, 0, 0], [-0.2, 1, 0], [0.3, 0.7, 1]], dtype = np.float64)

# Generate a mask for the upper triangle
mask = np.triu(np.ones_like(corr, dtype=bool))

# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(24, 12))

# Generate a custom diverging colormap
cmap = sns.diverging_palette(230, 20, as_cmap=True)
# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(corr, mask=mask, cmap=cmap, vmin=-1, vmax=1, center=0,
            square=True, cbar_kws={"shrink": .5})
            
plt.show()