2

When plotting matrix with imshow in Matplotlib, how to change colorbar legend bar size, location, font and other parameters?

Here I created an example code

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline

def plot_matrix(mat, title='example', cmap=plt.cm.Blues):
    plt.imshow(mat, interpolation='nearest', cmap=cmap)
    plt.grid(False)
    plt.title(title)
    plt.colorbar()

data = np.random.random((20, 20))

plt.figure(figsize=(8,8))
plt.tick_params(axis='both', which='major', labelsize=12)

plot_matrix(data) 

enter image description here

In a real use case, I got complex labels and the legend bar becomes much higher then the matrix itself. I want to change the legend bar to make the plot more efficiently use the space.

I found a documentation for the matplotlib.pyplot.colorbar, however have not figure out a good way to set the size, location and font size for the color legend bar.

CT Zhu
  • 52,648
  • 17
  • 120
  • 133
Bin
  • 3,645
  • 10
  • 33
  • 57
  • 1
    you have many possibilities using `make_axes_locatable`, [as detailed here](http://stackoverflow.com/a/18195921/832621) – Saullo G. P. Castro Aug 13 '15 at 22:30
  • +1, `make_axes_locatable` method calculates the location and size of axes at drawing time. Alternatively, we can specify the location and size of axes before drawing time. See below: – CT Zhu Aug 14 '15 at 15:39

1 Answers1

3

imshow enforces a 1:1 aspect (by default, but you can change it with aspect parameter), which makes things a little trickier. To always get consistent result, I might suggest manually specify the size of axes:

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline

def plot_matrix(mat, figsize, title='example', cmap=plt.cm.Blues):
    f = plt.figure(figsize=figsize)
    ax = plt.axes([0, 0.05, 0.9, 0.9 ]) #left, bottom, width, height
    #note that we are forcing width:height=1:1 here, 
    #as 0.9*8 : 0.9*8 = 1:1, the figure size is (8,8)
    #if the figure size changes, the width:height ratio here also need to be changed
    im = ax.imshow(mat, interpolation='nearest', cmap=cmap)
    ax.grid(False)
    ax.set_title(title)
    cax = plt.axes([0.95, 0.05, 0.05,0.9 ])
    plt.colorbar(mappable=im, cax=cax)
    return ax, cax

data = np.random.random((20, 20))
ax, cax = plot_matrix(data, (8,8)) 

Now you have the axis where the colorbar is plotted in, cax. You can do a lot of thing with that, say, rotate the labels, using plt.setp(cax.get_yticklabels(), rotation=45)

enter image description here

CT Zhu
  • 52,648
  • 17
  • 120
  • 133
  • Thanks CT! I followed your way to implement my methods and it works perfectly. The bar size is aligned well. I am also be able to set the font size of x and y ticks while creating them. Another thing I also want to set the font of colorbar label. Any suggestions? – Bin Aug 14 '15 at 14:46
  • You are welcome! Change font would be: `plt.setp(cax.get_yticklabels(), fontname='Times New Roman') ` assuming you have `Time New Roman` installed on your box. – CT Zhu Aug 14 '15 at 15:30