[Matplotlib-users] fixed colorbar position for repeated plots

Heiland, Randy heiland at iu.edu
Tue May 8 11:28:19 EDT 2018


I have a script that lets a user step through and plot data in files and I want to fix the position, i.e. re-use, a single colorbar. How is this done? The following script illustrates the problem:

# adapted from https://matplotlib.org/examples/pylab_examples/colorbar_tick_labelling_demo.html
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import randn

fig, ax = plt.subplots()
data = np.clip(randn(250, 250), -1, 1)
cax = ax.imshow(data, interpolation='nearest')
cbar = fig.colorbar(cax, ticks=[-1, 0, 1])

data = np.clip(randn(250, 250), -1, 1)
cax = ax.imshow(data, interpolation='nearest')
cbar = fig.colorbar(cax, ticks=[-1, 0, 1])

plt.show()



More information about the Matplotlib-users mailing list