Repository navigation
Description
Bug report
It is possible to set vmin and vmax values for pcolormesh such that unexpected and undesired results occur. If vmin and vmax are narrowly defined, pcolormesh will product a plot that is different whether or not a colorbar is linked to the plot.
Code for reproduction
#Helper function to set the position of the colorbar relative to a subplot (not essential but nice)
def cbar_position(current_ax,offset,cbar_width):
#get the tuple describing the lower x,y corner position
#and the width and height
x_pos, y_pos, width, height = current_ax.get_position().bounds
return [x_pos + width + offset, y_pos, cbar_width, height]
#define the data
A = np.array([[ 2., 2., 3., 4.],
[ 2., 0., 8., 5.],
[ 6., 4., 7., 1.],
[ 0., 9., 9., 2.],
[ 9., 0., 2., 9.]])
#make a set of subplots - note the changes in vmin and vmax arguments to pcolormesh
fig = plt.figure(figsize=(10,10))
cb_off = 0.01
cb_wth = 0.01
#first subplot
ax0 = fig.add_subplot(321)
cbax0 = fig.add_axes(cbar_position(ax0,cb_off,cb_wth))
pcm0 = ax0.pcolormesh(A,cmap=cmap.jet,vmin=0,vmax=9)
fig.colorbar(pcm0,cax=cbax0)
#second subplot
ax1 = fig.add_subplot(322)
cbax1 = fig.add_axes(cbar_position(ax1,cb_off,cb_wth))
pcm1 = ax1.pcolormesh(A,cmap=cmap.jet,vmin=1,vmax=8)
fig.colorbar(pcm1,cax=cbax1)
#third subplot
ax2 = fig.add_subplot(323)
cbax2 = fig.add_axes(cbar_position(ax2,cb_off,cb_wth))
pcm2 = ax2.pcolormesh(A,cmap=cmap.jet,vmin=2,vmax=7)
fig.colorbar(pcm2,cax=cbax2)
#fourth subplot
ax3 = fig.add_subplot(324)
cbax3 = fig.add_axes(cbar_position(ax3,cb_off,cb_wth))
pcm3 = ax3.pcolormesh(A,cmap=cmap.jet,vmin=3,vmax=6)
fig.colorbar(pcm3,cax=cbax3)
#fifth subplot
ax4 = fig.add_subplot(325)
cbax4 = fig.add_axes(cbar_position(ax4,cb_off,cb_wth))
pcm4 = ax4.pcolormesh(A,cmap=cmap.jet,vmin=4,vmax=5)
fig.colorbar(pcm4,cax=cbax4)
#sixth subplot
ax5 = fig.add_subplot(326)
cbax_5 = fig.add_axes(cbar_position(ax5,cb_off,cb_wth))
pcm5 = ax5.pcolormesh(A,cmap=cmap.jet,vmin=4.5,vmax=4.5)
#fig.colorbar(pcm5,cax=cbax_5)Actual outcome
Resulting output with the very last line commented out

Resulting output with the very last line enabled

While it is reasonable to have the colorbar object confused by the vmin = 4.5/vmax = 4.5 case, its presence or absence should not affect how the pcolormesh rendering is done.
Matplotlib version
- Operating System: Windows 10
- Matplotlib Version: 2.0.0
- Python Version: 2.7.12 (Anaconda 2.5.0 (64-bit))
- Jupyter Version (if applicable): 4.1.0
- Other Libraries:
These results were obtained in a Jupyter notebook, Anaconda install. The original intention for this was to illustrate the use of vmin and vmax in pcolormesh to perform visual filtering of scientific data.
Activity
phobson commentedon Apr 27, 2017
I'm seeing similar behavior on my (very similar) system
efiring commentedon Apr 27, 2017
The problem is that the call to transforms.nonsingular(vmin, vmax) is not being made in the ScalarMappable.set_clim() method, but only in Colorbar._process_values. The behavior is common to pcolor and imshow (and presumably other scalar mappables) as well as pcolormesh.
github-actions commentedon Apr 11, 2023
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
jklymak commentedon Apr 11, 2023
I marked "keep", though this is pretty degenerate. If you use vmin=4.5-0.00000001, you get something reasonable. Its unexpected that the colorbar would change the result, we maybe should just error if vmin==vmax.
QuLogic commentedon Apr 11, 2023
Would it make any sense to move that to
ScalarMappable.set_clim?jklymak commentedon Apr 11, 2023
I don't think mappables have a concept of nonsingular - and indeed vmin > vmax is allowed (to make an reversed mapping)
greglucas commentedon Apr 12, 2023
Now that Colorbar is more tied to the Axes, we could potentially remove the Colorbar expansion and let the nonsingular expansion take place by the Axis, which would emit a warning? Or we could at least bring the UserWarning into the Colorbar expansion code so that the user is notified something is going on under the hood.
matplotlib/lib/matplotlib/axis.py
Lines 1209 to 1212 in bff4681