Skip to content Skip to sidebar Skip to footer

Positioning Color Bars - Matplotlib

I have a plot in which I merge two datas. Given that, I have to show two different color bars, one for each data. I'm currently plotting my datas as follows: plt.figure() # Data 1

Solution 1:

Probably the 'easiest' way to do this is to lay the axes to be used for the color bars out by hand (via cbax = fig.add_axes([....])). You can then pass that axes to the color bar calls:

Something like:

from matplotlib import pyplot as plt
import numpy as np

fig = plt.figure(figsize=(8, 8))
ax = fig.add_axes([.1, .1, .8, .8])
im = ax.imshow(np.random.rand(150, 150), cmap='gray', interpolation='none')
sc = ax.scatter(2 + 146 * np.random.rand(150), 2 + 146 * np.random.rand(150),
                c=np.random.rand(150), cmap='Accent', s=50, lw=0)

ax_cb1 = fig.add_axes([.1, .05, .8, .02])
ax_cb2 = fig.add_axes([.92, .1, .02, .8])

cb1 = fig.colorbar(im, cax=ax_cb1, orientation='horizontal')
cb1.ax.xaxis.set_label_position('top')
cb2 = fig.colorbar(sc, cax=ax_cb2, orientation='vertical')

enter image description here

Solution 2:

you can link the colorbar to the axes with the ax-keyword, plt.gca() gives you the current axes:

plt.colorbar(object1, ax=plt.gca())

Post a Comment for "Positioning Color Bars - Matplotlib"