Skip to content Skip to sidebar Skip to footer

Matplotlib - 2 Problems. Common Colorbar / Labels Not Showing Up

I finally forced the 3 plots I want into one plot with 3 subplots...now I need to add a common colorbar, preferably horizontally oriented. Also, now that I have them as subplots, I

Solution 1:

To set the xlabel, use ax.set_xlabel('Uniformity: ' + unif) See more information here in the documentation for axes.

The example you linked to uses the add_axes method of a figure as an alternative to add_subplot. The documentation for figures explains what the numbers in add_axes are: "Add an axes at position rect [left, bottom, width, height] where all quantities are in fractions of figure width and height."

rect = l,b,w,h
fig.add_axes(rect)

Solution 2:

To answer your question about the colorbar axis, the numbers represent

[bottom_left_x_coord, bottom_left_y_coord, width, height]

An appropriate colorbar might be

# x    y    w     h
[0.2, 0.1, 0.6, 0.05]

Here's your code, somewhat reworked which adds a colorbar:

import numpy as np
import matplotlib.pyplot as plt

WIDTH = 9defuniformity_calc(x):
    return x.mean()

defplotter(x, y, zs, name, units, efficiency=True):
    fig, axarr = plt.subplots(1, 3, figsize=(WIDTH, WIDTH/3), 
                              subplot_kw={'aspect':1})
    fig.suptitle(name)

    UI = map(uniformity_calc, zs)
    ranges = map(lambda x: int(np.max(x)-np.min(x)), zs)

    for ax, z, unif, rangenum inzip(axarr, zs, UI, ranges):
        scat = ax.scatter(x, y, c=z, s=100, cmap='rainbow')
        label = 'Uniformity: %i'%unif
        ifnot efficiency:
            label += '    %i ppm'%rangenum
        ax.set_xlabel(label)

    # Colorbar [left, bottom, width, height
    cax = fig.add_axes([0.2, 0.1, 0.6, 0.05])
    cbar = fig.colorbar(scat, cax, orientation='horizontal')
    cbar.set_label('This is a colorbar')
    plt.show()


defmain():
    x, y = np.meshgrid(np.arange(10), np.arange(10))
    zs = [np.random.rand(*y.shape) for _ inrange(3)]
    plotter(x.flatten(), y.flatten(), zs, 'name', None)

if __name__ == "__main__":
    main()

enter image description here

Post a Comment for "Matplotlib - 2 Problems. Common Colorbar / Labels Not Showing Up"