python - Align ticklabels in matplotlib colorbar -
i have colorbar positive , negative values generated automatically (i not setting them). unfortunately minus sign breaks vertical alignment of text. how can align text in ticklabels right or alternatively insert space before positive numbers make good?
i ask same question, found yours. i'd add own work-around.
at first used exact same solution given rutger kassies. however, when plotting in ipython
, same amount of shift in x
position give wildly different results when viewing small window, or setting figure full screen.
i've settled on setting pad
of colorbar's tick labels. borrowing blatantly stealing sample code of rutger:
a = np.random.randn(10,10) fig, ax = plt.subplots() im = ax.imshow(a, interpolation='none') cb = plt.colorbar(im) ticklabs = cb.ax.get_yticklabels() cb.ax.set_yticklabels(ticklabs,ha='right') cb.ax.yaxis.set_tick_params(pad=45) # number may vary
this workaround seems give more consistent result when using across various window sizes , aspects.
here's 4 outputs demonstrate mean: top row position shifting solution, bottom row padding solution. on left default figure window sizes, right side savefig
ged after popping figure window full screen:
Comments
Post a Comment