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:
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: