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.

2
  • 1
    you have many possibilities using make_axes_locatable, as detailed here Commented Aug 13, 2015 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: Commented Aug 14, 2015 at 15:39

1 Answer 1

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

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

2 Comments

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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.