Python,Seaborn:如何复制Corrplot? [英] Python, Seaborn: how to replicate corrplot?

查看:129
本文介绍了Python,Seaborn:如何复制Corrplot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在上有关数据可视化的UDemy课程(我会向您推荐,但可能看起来像垃圾邮件),在该课程之前,我一直在使用matplotlib进行可视化,所以Seaborn是对我来说很新。在课程中,他们谈论 corrplot()函数,该函数可能会产生如下图所示的内容:

I'm taking a course on UDemy about data visualization (I'd recommend it to you, but it might look as spam), prior to that course, I've been using matplotlib to do my visualizations, so Seaborn is quite new to me. In the course they talk about corrplot() function, which can produce something like the following chart:

但是现在, corrplot()已被弃用。我一直在看Seaborn文档,在网上找到一些链接,而我做的更紧密的是这样的:

But now, corrplot() has been deprecated. I've been looking in the Seaborn documentation, and some links on the web, and the "closer" I've done is this:

当然,我更喜欢原始的 corrplot()输出及其更容易实现,使用 heatmap()或任何其他函数进行相同操作的方式是什么?

Of course, I like much more the original corrplot() output, and its way easier to implement, what would be the way to do the same using the heatmap() or any other function?

BTW:产生图表的数据是不同的,第一个是视频的捕获,而另一个是我的PC的屏幕截图,因此值并不相同。

BTW: The data that produced the charts is different, the first one comes from a capture of the video, while the other one is a screenshot of my PC, so the values aren't the same.

推荐答案

首先, corrplot()贬值的事实并不意味着您不能使用它。它很可能会在seaborn的某些将来版本中删除,或者伴随其他问题。但是,如果您对它现在所提供的功能感到满意,则仍可以使用它。

First, the fact that corrplot() is depreciated does not mean that you cannot use it. It is just likely that it will be removed in some future version of seaborn or has some other issue accompanying it. However, if you are happy with what it gives you now, you may still use it.

为了获得类似于 corrplot的结果,但是使用 heatmap ,您可能需要稍微调整图表。

In order to get a result similar to the corrplot but using a heatmap, you may need to tweak the plot a bit.

下面显示一个示例:

import numpy as np; np.random.seed(1)
import pandas as pd
import seaborn.apionly as sns
import matplotlib.pyplot as plt

# Generate a random dataset
cols = [s*4 for s in list("ABCD")]
df = pd.DataFrame(data=np.random.rayleigh(scale=5, size=(100, 4)), columns=cols)

# Compute the correlation matrix
corr = df.corr()
print(corr)
# Generate a mask for the upper triangle
mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True

# Set up the matplotlib figure
fig, ax = plt.subplots()

# Draw the heatmap with the mask and correct aspect ratio
vmax = np.abs(corr.values[~mask]).max()
sns.heatmap(corr, mask=mask, cmap=plt.cm.PuOr, vmin=-vmax, vmax=vmax,
            square=True, linecolor="lightgray", linewidths=1, ax=ax)
for i in range(len(corr)):
    ax.text(i+0.5,len(corr)-(i+0.5), corr.columns[i], 
            ha="center", va="center", rotation=45)
    for j in range(i+1, len(corr)):
        s = "{:.3f}".format(corr.values[i,j])
        ax.text(j+0.5,len(corr)-(i+0.5),s, 
            ha="center", va="center")
ax.axis("off")
plt.show()

这篇关于Python,Seaborn:如何复制Corrplot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆