1

Edit - I hope this clarifies. Below is code from this post that is very similar to what I'm trying to do.

fig, axes = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True)
for ax in axes.flat:
    im = ax.imshow(np.random.random((10,10)), vmin=0, vmax=1)

cax,kw = mpl.colorbar.make_axes([ax for ax in axes.flat])
plt.colorbar(im, cax=cax, **kw)

plt.show()

Vs.

    fig, axes = plt.subplots(nrows = 1, ncols = 3, sharex = True, sharey = True)

    for ax, z_val, unif, rangenum in zip(axes.flat, z_vals, UI, ranges):
    plot = plt.scatter(x_vals, y_vals, s = 5.5 * max(x_vals), c = z_val, cmap = 'rainbow')
        if efficiency_or_not:
            plot.vmin = 0
            plot.vmax = 1
            plot.xlabel = 'Uniformity: ' + unif
        else:
            plot.xlabel = 'Uniformity: ' + unif + '   ' + rangenum + ' ppm'

    cax, kw = mpl.colorbar.make_axes([ax for ax in axes.flat])
    plt.colorbar(plot, cax = cax, **kw)    
    plt.savefig('./'+ figname + '.jpg', dpi = 100)
    plt.close()

What I want is 3 plots horizontally tiled and to add the colorbar below.

Original post below

I found this and I would like to use this technique....I'm having trouble translating it for my situation, though.

I have 3 separate runs that are the same type of data. I would like to take those 3 plots and turn them into one figure with one colorbar. I need to do this 5 times per report, and there is one set that I want to force the vmin and vmax on.

My current function is below...the errors I'm getting are:

plot_that_2(x_vals, y_vals, plots_list[j], plots_list[j+1], plots_list[j+2],plots_names_list[j], Plots_dictionary[plot_strings_list[i]][0],Plots_dictionary[plot_strings_list[i]][1])

cax, kw = mpl.colorbar.make_axes([ax for ax in axes.flat])

pb = parent.get_position(original=True).frozen() AttributeError: 'list' object has no attribute 'get_position'

The first objection is listed in module, the second in the program and the third in make_axes. The x, y, and z values are all lists from numpy arrays. figname is a string, units is a string, and efficiency_or_not is a Boolean.

def plot_that_2(x_vals, y_vals, z_1_vals, z_2_vals, z_3_vals, figname, units, efficiency_or_not):
    UI = [uniformity_calc(z_1_vals), uniformity_calc(z_2_vals), uniformity_calc(z_3_vals)]
    ranges = [ str(int(np.max(z_1_vals) - np.min(z_1_vals))), str(int(np.max(z_2_vals) - np.min(z_2_vals))), str(int(np.max(z_3_vals) - np.min(z_3_vals)))]
    z_vals = [z_1_vals, z_2_vals, z_3_vals]
    fig, axes = plt.subplots(nrows = 1, ncols = 3, sharex = True, sharey = True)

    for ax, z_val, unif, rangenum in zip(axes.flat, z_vals, UI, ranges):
    plot = plt.scatter(x_vals, y_vals, s = 5.5 * max(x_vals), c = z_val, cmap = 'rainbow')
        if efficiency_or_not:
            plot.vmin = 0
            plot.vmax = 1
            plot.xlabel = 'Uniformity: ' + unif
        else:
            plot.xlabel = 'Uniformity: ' + unif + '   ' + rangenum + ' ppm'

    cax, kw = mpl.colorbar.make_axes([ax for ax in axes.flat])
    plt.colorbar(plot, cax = cax, **kw)    
    plt.savefig('./'+ figname + '.jpg', dpi = 100)
    plt.close()
Community
  • 1
  • 1
mauve
  • 2,707
  • 1
  • 20
  • 34
  • 1
    This is very hard to read/parse. Can you reduce this to the _minimum_ amount of code that will reproduce your problem? – tacaswell Apr 10 '14 at 01:14

1 Answers1

2

To move the colorbar to the bottom you only have to add orientation="horizontal" or location='bottom'. By taking user1442911's example:

fig, axes = plt.subplots(nrows=1, ncols=3)

for ax in axes.flat:
    im = ax.imshow(np.random.random((10,10)), vmin=0, vmax=1)

cax, kw = mpl.colorbar.make_axes([ax for ax in axes.flat], location='bottom')
plt.colorbar(im, cax=cax, **kw)

plt.show()

enter image description here

Rutger Kassies
  • 61,630
  • 17
  • 112
  • 97
  • 3
    I'm getting an error in the make_axes statement. `pb = parent.get_position(original=True).frozen() AttributeError: 'list' object has no attribute 'get_position'` – mauve Apr 10 '14 at 14:10