Home > OS >  Reverse color order of colorbar (not colormap) (Matplotlib)
Reverse color order of colorbar (not colormap) (Matplotlib)

Time:03-05

I have a pandas dataframe of 350 rows × 16 columns of just 0 and 1.

I create a correlation matrix

dummy.corr(method ='kendall')

I'm plotting a correlogram:

f = plt.figure(figsize=(19, 15))
plt.matshow(dummy.corr(), fignum=f.number)
plt.xticks(range(dummy.select_dtypes(['number']).shape[1]), dummy.select_dtypes(['number']).columns, fontsize=14, rotation=45)
plt.yticks(range(dummy.select_dtypes(['number']).shape[1]), dummy.select_dtypes(['number']).columns, fontsize=14)
cb = plt.colorbar()



cb.ax.tick_params(labelsize=14)



plt.grid(b=None)

I'd like to reverse the order of the colors. Most answers I've found out deal with colormap but I'm just using colorbar. How can i do it?

CodePudding user response:

The default colormap used when you call matshow is viridis. If you use the reverse viridis colormap (viridis_r) in your matshow, you will automatically get the reverse colorbar. See below:

import matplotlib.pyplot as plt
import numpy as np

fig=plt.figure(figsize=(10,5))
c=np.random.choice(10,size=(10,10))

#Not reversed
ax1=plt.subplot(121)
ax1.set_title('Not reversed')
dat1=ax1.matshow(c)
cb1=plt.colorbar(dat1)

#reversed
ax2=plt.subplot(122)
ax2.set_title('Reversed')
dat2=ax2.matshow(c,cmap='viridis_r')
cb2=plt.colorbar(dat2)

plt.show()

See result below:

enter image description here

  • Related