Arg. It's always the last thing you try:
quadmesh.set_clim(vmin=0, vmax=15)
works.
Answer from mgilson on Stack OverflowGitHub
github.com › matplotlib › matplotlib › issues › 12682
using AxesImage.set_clim() shrinks the colorbar · Issue #12682 · matplotlib/matplotlib
October 31, 2018 - fig, ax = plt.subplots() data = np.random.random(size=(50,50)) img = ax.imshow(data) cb = plt.colorbar(img) # the colorbar is the same height as the plot at this point #img.set_clim([0.25,0.75]) # running this line will produce a shrunken colorbar #img.set_clim([-0.25,1.25]) # running this line will change the limits of the colorbar # and return the colorbar to its original size
Top answer 1 of 3
40
Arg. It's always the last thing you try:
quadmesh.set_clim(vmin=0, vmax=15)
works.
2 of 3
4
Matplotlib 1.3.1 - It looks like the colorbar ticks are only drawn when the colorbar is instanced. Changing the colorbar limits (set_clim) does not cause the ticks to be re-drawn.
The solution I found was to re-instance the colorbar in the same axes entry as the original colorbar. In this case, axes[1] was the original colorbar. Added a new instance of the colorbar with this designated with the cax= (child axes) kwarg.
# Reset the Z-axis limits
print "resetting Z-axis plot limits", self.zmin, self.zmax
self.cbar = self.fig.colorbar(CS1, cax=self.fig.axes[1]) # added
self.cbar.set_clim(self.zmin, self.zmax)
self.cbar.draw_all()
Matplotlib
matplotlib.org › stable › api › _as_gen › matplotlib.pyplot.clim.html
matplotlib.pyplot.clim — Matplotlib 3.11.2 documentation
Set the color limits of the current image · If either vmin or vmax is None, the image min/max respectively will be used for color scaling
Top answer 1 of 7
245
Using vmin and vmax forces the range for the colors. Here's an example:

import matplotlib as m
import matplotlib.pyplot as plt
import numpy as np
cdict = {
'red' : ( (0.0, 0.25, .25), (0.02, .59, .59), (1., 1., 1.)),
'green': ( (0.0, 0.0, 0.0), (0.02, .45, .45), (1., .97, .97)),
'blue' : ( (0.0, 1.0, 1.0), (0.02, .75, .75), (1., 0.45, 0.45))
}
cm = m.colors.LinearSegmentedColormap('my_colormap', cdict, 1024)
x = np.arange(0, 10, .1)
y = np.arange(0, 10, .1)
X, Y = np.meshgrid(x,y)
data = 2*( np.sin(X) + np.sin(3*Y) )
def do_plot(n, f, title):
#plt.clf()
plt.subplot(1, 3, n)
plt.pcolor(X, Y, f(data), cmap=cm, vmin=-4, vmax=4)
plt.title(title)
plt.colorbar()
plt.figure()
do_plot(1, lambda x:x, "all")
do_plot(2, lambda x:np.clip(x, -4, 0), "<0")
do_plot(3, lambda x:np.clip(x, 0, 4), ">0")
plt.show()
2 of 7
140
Use the CLIM function (equivalent to CAXIS function in MATLAB):
plt.pcolor(X, Y, v, cmap=cm)
plt.clim(-4,4) # identical to caxis([-4,4]) in MATLAB
plt.show()
Python Data Science Handbook
jakevdp.github.io › PythonDataScienceHandbook › 04.07-customizing-colorbars.html
Customizing Colorbars | Python Data Science Handbook
Matplotlib allows for a large range of colorbar customization. The colorbar itself is simply an instance of plt.Axes, so all of the axes and tick formatting tricks we've learned are applicable.
Stack Overflow
stackoverflow.com › questions › 37400921 › how-to-add-a-common-colorbar-to-subplots-in-matplotlib-and-change-their-limits-t
python - How to add a common colorbar to subplots in Matplotlib and change their limits together - Stack Overflow
I'm trying to have three pseudocolor subplots side by side, with one colorbar for subplots #1 and #2 and a second colorbar for #3. I'd also like the color limits (clim) to be set so it's the same for the first two (so the first colorbar would reflect the values of both subplots #1 and #2). ... import numpy as np import matplotlib.pyplot as plt plt.ion() import matplotlib as mpl data1 = np.random.random((10,10)) data2 = 2.*np.random.random((10,10)) data3 = 3.*np.random.random((10,10)) f, (ax1, ax2, ax3) = plt.subplots(1, 3, sharey=True) imgplot1 = ax1.pcolormesh(data1, edgecolors='None') imgplo
iCert Global
icertglobal.com › home › community › how can i manually set the specific limits and range of a colorbar in matplotlib?
How can I manually set the specific limits and range of a colorbar in Matplotlib?
The most efficient way to control the colorbar range is actually at the moment of plot creation. In functions like plt.imshow(), plt.pcolormesh(), or plt.scatter(), you should use the vmin and vmax parameters. For example, plt.imshow(data, vmin=0, vmax=100) tells Matplotlib to map the colormap strictly to that range.
Stack Overflow
stackoverflow.com › questions › 65040676 › matplotlib-sliders-rescale-colorbar-when-used-to-change-clim
python - Matplotlib sliders rescale colorbar when used to change clim - Stack Overflow
November 27, 2020 - import numpy as np import matplotlib %matplotlib notebook import matplotlib.pyplot as plt from matplotlib.widgets import Slider # Generate random data x = np.linspace(-1,1,101) y = np.linspace(-1,1,101) c = np.random.random((101,101)) # Plot data ax = plt.subplot(111) plt.subplots_adjust(left=0.25, bottom=0.3) img = ax.pcolormesh(x,y,c, cmap = "inferno") cbar = plt.colorbar(img) # Add Sliders c_min = np.min(c) #the min and max range of the sliders c_max = np.max(c) ax_cmin = plt.axes([0.25, 0.1, 0.65, 0.03]) #positions sliders beneath plot ax_cmax = plt.axes([0.25, 0.15, 0.65, 0.03]) s_cmin =
GitHub
github.com › matplotlib › matplotlib › issues › 17052
Colorbar update error with clim change in multi_image.py example · Issue #17052 · matplotlib/matplotlib
April 7, 2020 - In the multi_image example, the colorbar is responding correctly to set_clim only when called on the image to which the colorbar is directly attached. Code for reproduction This is just the example, https://matplotlib.org/gallery/images_contours_and_fields/multi_image.html, with manipulation of the clim at the bottom.
Author: matplotlib
Matplotlib
matplotlib.org › stable › api › _as_gen › matplotlib.pyplot.colorbar.html
matplotlib.pyplot.colorbar — Matplotlib 3.11.2 documentation
This argument is mandatory for the Figure.colorbar method but optional for the pyplot.colorbar function, which sets the default to the current image.
Matplotlib
matplotlib.org › 3.1.1 › api › _as_gen › matplotlib.pyplot.clim.html
matplotlib.pyplot.clim — Matplotlib 3.1.2 documentation
May 1, 2020 - Set the color limits of the current image · If either vmin or vmax is None, the image min/max respectively will be used for color scaling
Matplotlib
matplotlib.org › 3.3.1 › api › _as_gen › matplotlib.pyplot.clim.html
matplotlib.pyplot.clim — Matplotlib 3.3.1 documentation
August 14, 2020 - Set the color limits of the current image · If either vmin or vmax is None, the image min/max respectively will be used for color scaling
Matplotlib
matplotlib.org › 3.3.3 › api › _as_gen › matplotlib.pyplot.clim.html
matplotlib.pyplot.clim — Matplotlib 3.3.3 documentation
November 12, 2020 - Set the color limits of the current image · If either vmin or vmax is None, the image min/max respectively will be used for color scaling
GitHub
github.com › mpld3 › mpld3 › issues › 293
Colorbar set_clim doesn't affect tick labels · Issue #293 · mpld3/mpld3
May 27, 2015 - Colorbar set_clim doesn't affect tick labels#293 · Copy link · nathanielatom · opened · on May 27, 2015 · Issue body actions · The tick labels remain between 0 and 1, no matter how the clim is set: import numpy as np import scipy.signal as sig import matplotlib.pyplot as plt import matplotlib.mlab as mlab import mpld3 %matplotlib inline # changing this determines whether the colourbar ticklabels are correctly rendered # mpld3.disable_notebook() mpld3.enable_notebook() def plot_spectrogram(t, f, l, clabel="Level [dB]", **plot_options): if l.shape[0] == t.size and l.shape[1] == f.size: l =
Author: mpld3
Matplotlib
matplotlib.org › stable › tutorials › colors › colorbar_only.html
Customized Colorbars Tutorial — Matplotlib 3.7.2 documentation
August 13, 2021 - A colorbar needs a "mappable" (matplotlib.cm.ScalarMappable) object (typically, an image) which indicates the colormap and the norm to be used.
AdamSmith.haus
adamsmith.haus › python › answers › how-to-set-colorbar-range-using-matplotlib-in-python
How to set colorbar range using Matplotlib in Python
Python answers, examples, and documentation