2

I'm using pandas to import 256x256 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):

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?

1
  • Would transposing/transforming the input data help you accomplish this? Or does it not get you to the orientation you want? Commented Jul 7, 2022 at 15:12

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! In the end I did exactly the same thing but using numpy instead of pandas and it works exactly as well. Thank you!
2

Ideally this should be a transformation of the visualization instead of the underlying data, which is an inversion of your y-axis. However, since matplotlib does not implement a swap_axes type of API, you'll need to transpose your data.

To rotate a heatmap using pandas & matplotlib methods, you can draw a heatmap of the transposed dataframe, and then invert your y-axis:

Transpose & Invert the y-axis

import seaborn as sns import numpy as np import pandas as pd data = np.zeros((10,15)) data[0,0] = 1 df = pd.DataFrame(data) ax = sns.heatmap(df.T) ax.invert_yaxis() 

enter image description here

Side-by-side example

import seaborn as sns import numpy as np import pandas as pd import matplotlib.pyplot as plt data = np.zeros((10,15)) data[0,0] = 1 df = pd.DataFrame(data) fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(16, 4)) sns.heatmap(df, ax=ax1) ax1.set_title('no changes') sns.heatmap(df.T, ax=ax2) ax2.set_title('transposed') sns.heatmap(df.T, ax=ax3) ax3.invert_yaxis() ax3.set_title('transposed w/ inverted y-axis') 

enter image description here

2 Comments

I think OP wants to rotate, not flip; see the axis ticks in the desired output and the text.
Thanks for pointing that out! some data manipulation is required after all, and I've updated my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.