Reputation: 184
I have a dataframe with 8 rows and 6028 columns. I want to create a heatmap of the 8 rows for the first column (eventually I will create an animation so the map updates reading through each column)
This is a snippet of the dataframe:
value
percentage_time 0.00 0.15 0.16
region
Anterior Distal 0.111212 0.119385 0.116270
Anterior Proximal 0.150269 0.153613 0.168188
Lateral Distal 0.130440 0.137157 0.136494
Lateral Proximal 0.171977 0.182251 0.181090
Medial Distal 0.077468 0.082064 0.082553
Medial Proximal 0.194924 0.198803 0.199339
Posterior Distal 0.164124 0.171221 0.166328
Posterior Proximal 0.131310 0.145706 0.136094
I have used the following code but it gives me one plot with the indices stacked and all the data in the dataframe:
sns.heatmap(region_pressure_data)
When I try to use the following code to get just the first column, I get the following:
sns.heatmap(region_pressure_data.ix[:,0:1])
Ideally, I would like 1 map of 8 regions, with 2 rows (proximal and distal) and 4 columns (anterior, lateral, posterior, medial), displaying the data of one column.
I'd appreciate any advice on progressing with this method or if there is a better way to approach the challenge.
Thanks in advance.
Upvotes: 1
Views: 2723
Reputation: 198
The data in your indices needs to be part of the cells and you probably want a pivot. For explanation, I created some similar dataframe with less columns to illustrate what I am doing. I hope this is the structure you are using?
df = pd.DataFrame(index=["Anterior Distal", "Anterior Proximal", "Lateral Distal", "Lateral Proximal"], data={0.:[1,2,3,4], 1.:[5,6,7,8]})
print(df)
>>>
0.0 1.0
region
Anterior Distal 1 5
Anterior Proximal 2 6
Lateral Distal 3 7
Lateral Proximal 4 8
As I understand it, you want to explicitly refer to the two parts of your index, so you will need to split the index first. You can do this for example in this way which first uses a pandas method to split the strings and then transforms it to a numpy array which you can slice
index_parts = np.array(df.index.str.split().values.tolist())
index_parts[:,0]
>>> array(['Anterior', 'Anterior', 'Lateral', 'Lateral'], dtype='<U8')
Now, you can add those as new columns
df["antlat"] = index_parts[:,0]
df["distprox"] = index_parts[:,1]
print(df)
>>>
0.0 1.0 antlat distprox
region
Anterior Distal 1 5 Anterior Distal
Anterior Proximal 2 6 Anterior Proximal
Lateral Distal 3 7 Lateral Distal
Lateral Proximal 4 8 Lateral Proximal
Then you can create the pivot for the value you are interested in
df_pivot = df.pivot(index="antlat", columns="distprox", values=0.0)
print(df_pivot)
>>>
distprox Distal Proximal
antlat
Anterior 1 2
Lateral 3 4
And plot it (note that this is only 2x2, since I did not add Medial and Posterior to the example)
sns.heatmap(df_pivot)
Upvotes: 1
Reputation: 2212
Why not using directly matplotlib
? :D
import matplotlib.pyplot as plt
plt.imshow(df.reset_index(drop=True).values[:,1:].astype("float"))
Upvotes: 0