Indexing issue after Transposing dataframe

Question:

I have the following code below, to produce the heatmap in the image. However, as there are many ‘Indicators’ – I would like the heat map to be long horizontal and not tall. I.e., the Indicators on the X axis, and the Criteria (robustness…etc) along the left side of the y axis.

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

df = pd.read_csv('Res_Gov.csv')

df1 = df.transpose()

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

colors = ["#f4a261","#2a9d8f"] 
cmap = LinearSegmentedColormap.from_list('Custom', colors, len(colors))


# Draw the heatmap with the mask and correct aspect ratio
df1 = sns.heatmap(df1.set_index('Indicators'), cmap=cmap, square=True, linewidths=.5, cbar_kws={"shrink": .5})

# Set the colorbar labels
colorbar = ax.collections[0].colorbar
colorbar.set_ticks([0.25,0.75])
colorbar.set_ticklabels(['Not Applicable', 'Applicable'])

Without the transpose function, I receive this:

heatmap

But, when I use it, I receive just a blank square graph.

I know there is an indexing issue happing but am not sure what it is. Any help would be appreciated! Thank you!

Df snippet – before transpose

,Indicators,Robustness,Flexibility,Resourcefulness,Redundancy,Diversity,Independence,Foresight Capacity,Coordination Capacitiy,Collaboration Capacity,Connectivity & Interdependence,Agility,Adaptability,Self-Organization,Creativity & Innovation,Efficiency,Equity
0,G1,1,1,1,0,0,1,1,1,1,1,1,1,0,1,1,1
1,G2,1,0,0,0,0,1,0,1,1,1,1,0,0,1,1,1
2,G3,1,0,1,0,0,1,1,1,1,1,1,1,1,1,0,1
3,G4,1,1,1,0,1,0,1,1,1,1,1,1,1,1,0,1
4,G5,1,0,1,0,0,0,0,1,0,1,1,0,0,0,1,0
5,G6,1,0,1,0,1,0,1,0,0,0,0,0,0,1,0,1
6,G7,1,1,0,1,0,1,0,0,0,0,1,0,0,0,0,0
7,G8,1,1,0,0,0,1,1,1,1,0,1,1,0,0,0,0
8,G9,1,0,1,0,0,1,1,1,1,0,1,1,0,1,0,1
9,G10,1,1,1,0,0,0,1,1,1,1,1,0,0,0,1,1
10,G11,1,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1
11,G12,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,0
12,G13,1,1,1,0,1,0,1,1,0,1,1,0,0,0,0,0
13,G14,1,0,1,0,1,0,1,1,1,1,1,1,0,0,1,1
14,G15,1,1,1,0,1,0,1,1,1,1,1,1,1,1,0,1
15,G16,1,0,1,0,1,1,0,1,1,1,0,1,1,1,0,1
16,G17,1,1,1,0,0,0,0,0,0,0,1,1,0,1,1,0
17,G18,1,0,1,0,1,1,1,1,1,1,0,1,1,1,0,1
18,G19,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
19,G20,1,1,0,1,1,0,0,0,1,0,0,0,1,0,0,1
20,G21,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1
21,G22,1,1,1,0,0,0,1,1,1,1,1,1,0,1,0,1
22,G23,1,0,1,0,0,1,1,0,1,0,0,1,1,1,0,0

Asked By: JamesArthur

||

Answers:

You need set Indicators to index before tranpose and then remove from sns.heatmap:

#create index by first column (from sample data, in real should be removed `index_col`)
df = pd.read_csv('Res_Gov.csv', index_col=0)

#create index before transpose
df1 = df.set_index('Indicators').T

Or:

df = pd.read_csv('Res_Gov.csv', index_col='Indicators')

#create index before transpose
df1 = df.T

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

colors = ["#f4a261","#2a9d8f"] 
cmap = LinearSegmentedColormap.from_list('Custom', colors, len(colors))


# Draw the heatmap with the mask and correct aspect ratio
#remove create index
df1 = sns.heatmap(df1, cmap=cmap, square=True, linewidths=.5, cbar_kws={"shrink": .5})

# Set the colorbar labels
colorbar = ax.collections[0].colorbar
colorbar.set_ticks([0.25,0.75])
colorbar.set_ticklabels(['Not Applicable', 'Applicable'])

pic

Answered By: jezrael

I made some minor modifications:

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

df = pd.read_csv('Res_Gov.csv', index_col=0)  # HERE

df1 = df.set_index('Indicators').T  # HERE

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

colors = ["#f4a261","#2a9d8f"] 
cmap = LinearSegmentedColormap.from_list('Custom', colors, len(colors))

# Draw the heatmap with the mask and correct aspect ratio
df1 = sns.heatmap(df1, cmap=cmap, square=True,
                  linewidths=.5, cbar_kws={"shrink": .5})  # HERE

# Set the colorbar labels
colorbar = ax.collections[0].colorbar
colorbar.set_ticks([0.25,0.75])
colorbar.set_ticklabels(['Not Applicable', 'Applicable'])
fig.tight_layout()  # HERE
plt.show()

Note: if you have not the first unnamed column with 0, 1, 2, 3, …, you can remove index_col=0 from pd.read_csv

enter image description here

Answered By: Corralien
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.