Skip to content

pcolormesh and colorbar bug/design flaw #8540

Open

Description

@gravmath

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
pcolormesh_no_defined_colorbar

Resulting output with the very last line enabled
pcolormesh_with_defined_colorbar

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

  1. phobson commented on Apr 27, 2017

    @phobson
    Member

    I'm seeing similar behavior on my (very similar) system

  2. added this to the milestone on Apr 27, 2017
  3. efiring commented on Apr 27, 2017

    @efiring
    Member

    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.

  4. modified the milestones: , on May 3, 2017
  5. modified the milestones: , on May 12, 2017
  6. modified the milestones: , on Sep 24, 2017
  7. modified the milestones: , on Oct 9, 2017
  8. github-actions commented on Apr 11, 2023

    @github-actions

    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!

  9. added
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    on Apr 11, 2023
  10. jklymak commented on Apr 11, 2023

    @jklymak
    Member

    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.

  11. QuLogic commented on Apr 11, 2023

    @QuLogic
    Member

    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.

    Would it make any sense to move that to ScalarMappable.set_clim?

  12. jklymak commented on Apr 11, 2023

    @jklymak
    Member

    I don't think mappables have a concept of nonsingular - and indeed vmin > vmax is allowed (to make an reversed mapping)

  13. greglucas commented on Apr 12, 2023

    @greglucas
    Contributor

    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.

    if v0 == v1:
    _api.warn_external(
    f"Attempting to set identical low and high {name}lims "
    f"makes transformation singular; automatically expanding.")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    keepItems to be ignored by the “Stale” Github Actionstatus: confirmed bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions