skip to Main Content

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
fig = plt.imshow(data1, interpolation='nearest', cmap='binary', vmin=0, vmax=1)

# Plotting just the nonzero values of data2
data2_x = numpy.nonzero(data2)[0]
data2_y = numpy.nonzero(data2)[1]

pts = plt.scatter(data2_x, data2_y, marker='s', c=data2[data2_x, data2_y])

plt.colorbar(pts)
plt.colorbar(fig, orientation="horizontal")

And this is the plot that I get:

enter image description here

However, I would like to reposition the color bars to have something like this (made with Photoshop):

enter image description here

Is that possible?

Thank you in advance.

2

Answers


  1. 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())
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search