3

I had a large colorbar I'd like to shrink to match my plot size. So I followed these two threads, thread1 and thread2, tried the magic code

"colorbar(im,fraction=0.046, pad=0.04)".

However, it does not seem to work. I am wondering where I did wrong? Following is my code. I am totally new to python. Thanks.

from matplotlib.pyplot import imshow
from matplotlib.pyplot import colorbar
from matplotlib.pyplot import close
from numpy import zeros as zeros

close('all')
img = zeros((250, 800))
im = imshow(img, cmap='gray'); colorbar(im,fraction=0.046, pad=0.04)

The colorbar still looks large: enter image description here

Importantly:

I'd prefer only to shorten the colorbar without making it thinner. Also, when the figure size changes, the colarbar can be size-changing accordingly.....like the MATLAB did, something like this: enter image description here

when figure size changes, the colorbar resizes accordingly: enter image description here

Nick X Tsui
  • 2,737
  • 6
  • 39
  • 73

1 Answers1

0

You can adjust the size of the colorbar with shrink within colorbar easily.

Have a look at this:

img = zeros((250, 800))
im = imshow(img, cmap='gray')
colorbar(im,fraction=0.046, pad=0.04,shrink=0.46)

Leads to: enter image description here

See colorbar documentation for more information.

  • I was thinking of only shortening the colorbar without making it thinner. Also, when the figure size changes, the colarbar can be size-changing accordingly.....like the MATLAB did. – Nick X Tsui Mar 27 '18 at 15:59
  • I mean your answer is great, but I guess there is gotta be a relatively easy way to do it. I apologize that I should have emphasize in the original thread. – Nick X Tsui Mar 27 '18 at 16:00