farhawa
farhawa

Reputation: 10398

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:

enter image description here

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):

enter image description here

Upvotes: 0

Views: 2591

Answers (1)

David
David

Reputation: 696

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

Upvotes: 5

Related Questions