Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python-How to have the axis ticks in both top and bottom, left and right of sns.heatmap

I am trying to draw a big heatmap with sns.heatmap function. However, since the map is too big, it's a little hard to find the xtick label or ytick label with corresponding rows and columns. Can I add the xtick and xlabels also on the top and ytick and ylabels also on the right??

I have tried many different ways. But they all didn't work.

like image 736
Yvonne.Tian Avatar asked Apr 01 '19 15:04

Yvonne.Tian


2 Answers

Not sure what you have tried, but the usual way would be via tick_params:

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(0)
import seaborn as sns

uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data)
ax.tick_params(right=True, top=True, labelright=True, labeltop=True)

plt.show()

enter image description here

like image 156
ImportanceOfBeingErnest Avatar answered Sep 24 '22 10:09

ImportanceOfBeingErnest


Axis ticks for all sides can be placed using tick_params from matplotlib

#Get sample correlation data to plot something. 'data' is a dataframe
corr=data.corr()

#Create Heatmap
axr = sns.heatmap(corr,cmap="coolwarm", annot=True, linewidths=.5,cbar=False)

#Set all sides
axr.tick_params(right=True, top=True, labelright=True, labeltop=True,rotation=0)

#Rotate X ticks
plt.xticks(rotation='vertical')

enter image description here

like image 23
Prasad Ostwal Avatar answered Sep 23 '22 10:09

Prasad Ostwal