Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rotate sns heatmap using pandas df

I’m using pandas to import 256×256 numbers of data which I want to plot using (something similar to) sns.heatmap().

I read the data into a pandas dataframe like this:

df_Data = pd.read_csv(datapath,sep="\t",header=None)

As a trial run to see clearly what’s happening my data looks like this (it doesn’t have any labels as it’s extracted directly from other software with just the raw numbers):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

100 100 100 100 100 100 100 100 0 0 0 0 0 ..... 0
100 100 100 100 100 100 100 100 0 0 0 0 0 ..... 0
100 100 100 100 100 100 100 100 0 0 0 0 0 ..... 0
100 100 100 100 100 100 100 100 0 0 0 0 0 ..... 0
100 100 100 100 100 100 100 100 0 0 0 0 0 ..... 0
0 . ... ... ... ... ... ... ... ... ... ... ... 0
... ... ... ... ... ... ... ... ... ... ... ... .
0 . ... ... ... ... ... ... ... ... ... ... ... 0

which produces this plot:
Plot in original format

I would like to rotate it by 270 degrees so that the highlighted part is in the bottom left corner, so that it looks like this (ideally with more sensible axis label formatting instead of what I did in ppt, but that’s a detail):
Rotate image

I plot it with seaborn like this:

sns.heatmap(df_IntegratedData)

What’s the most convenient way of doing this?

>Solution :

You can rotate the array in the dataframe using rot90:

df_rot90 = pd.DataFrame(np.rot90(df_IntegratedData),
                        index=reversed(df_IntegratedData.columns),
                        columns=df_IntegratedData.index)
sns.heatmap(df_rot90)

Example:

import pandas as pd
import numpy as np
import seaborn as sns

data = np.zeros((10,15))
data[0,0] = 1
df = pd.DataFrame(data)
sns.heatmap(df)

enter image description here

df1 = pd.DataFrame(np.rot90(df), index=reversed(df.columns), columns=df.index)
sns.heatmap(df1)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading