简体   繁体   中英

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.

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. 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. 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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