简体   繁体   中英

Seaborn heatmap re-indexing not working properly?

I've been recently working with Pandas-based heatmaps (using Seaborn for plotting). My current script is as follows:

df = dataframe.pivot("A","B","C")
df.index = pd.CategoricalIndex(df.index,categories=["c1","c2"...some custom categories...])
df.sort_index(0,inplace=True)
sns.heatmap(df).

This setting does not change heatmap's ordering as defined in step 2. How does one achieve that?

Thank you!

EDIT: actually, it changes the row order in df, yet heatmap looks the same.

Minimal example:

A = pd.DataFrame([("a","b",1),("c","c",1),("a","a",1),("b","c",1),("a","c",1)])
df  =  A.pivot(0,1,2)
df.index = pd.CategoricalIndex(df.index,categories=["b","a","c"])
df.sort_index(0,inplace=True)
sns.heatmap(df)
plt.show()

Column and row order is the same, I would wish for "bac".

EDIT 2: It seems that y-axis order changed correctly, yet x-axis remains the same, even if sort_index(1) is used. 在此处输入图片说明

You need to apply the same concept to your columns:

...
df.columns = pd.CategoricalIndex(df.columns,categories=["b","a","c"])
df.sort_index(1,inplace=True)
sns.heatmap(df)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM