简体   繁体   English

Python-将NxN矩阵绘制为渐变颜色网格

[英]Python - plot a NxN matrix as a gradient colors grid

I want to visualize the correlation between columns that I get with datafrome.corr() method. 我想可视化使用datafrome.corr()方法获得的列之间的相关性。

The result looks like: 结果看起来像:

在此处输入图片说明

What I am trying to do here is to draw that matrix with gradient colors based on the values of the data frame. 我在这里要做的是根据数据帧的值用渐变颜色绘制该矩阵。

Something like (Just an example from the web): 类似的东西(仅是网络示例):

在此处输入图片说明

If you can import your data into numpy here is a simple solution using matplotlib and should produce a heatmap similar to what you posted. 如果您可以将数据导入numpy,那么这是使用matplotlib的简单解决方案,并且会产生类似于您发布的热图。 You will just need to replace the dummy data with your data. 您只需要用数据替换虚拟数据即可。

import numpy as np
import matplotlib.pyplot as plt

# Generate some test data
data = np.arange(100).reshape((10,10))

plt.title('Actual Function')
heatmap = plt.pcolor(data)
plt.show()

Edit: Here is a bit fancier version with your x and y axis labels. 编辑:这是带有您的x和y轴标签的更高级的版本。 I chose to put them into two lists so that you could change each one independently. 我选择将它们分为两个列表,以便您可以分别更改每个列表。

import numpy as np
import matplotlib.pyplot as plt

# Generate some test data
data = np.arange(100).reshape((10,10))

xlabels = ['capacity', 'failure_rate', 'id', 'margin', 'price', 'prod_cost', 'product_type', 'quality', 'warranty', 'market_share', 'attractiveness']
ylabels = ['capacity', 'failure_rate', 'id', 'margin', 'price', 'prod_cost', 'product_type', 'quality', 'warranty', 'market_share', 'attractiveness']

fig, ax = plt.subplots()

ax.set_xticks(np.arange(data.shape[1]) + 0.5, minor=False)
ax.set_yticks(np.arange(data.shape[0]) + 0.5, minor=False)

ax.xaxis.tick_top()
plt.xticks(rotation=90)

ax.set_xticklabels(xlabels, minor=False)
ax.set_yticklabels(ylabels, minor=False)
heatmap = ax.pcolor(data)

ax = plt.gca()

for t in ax.xaxis.get_major_ticks():
    t.tick1On = False
    t.tick2On = False
for t in ax.yaxis.get_major_ticks():
    t.tick1On = False
    t.tick2On = False

plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM