FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

contour edge case with all data below levels and a surrounding field of zeros · Issue #20203 · matplotlib/matplotlib · GitHub

Repository navigation

contour edge case with all data below levels and a surrounding field of zeros #20203

Closed

Description

Bug report

Bug summary

If I attempt to make a contour plot with data that is entirely below the lowest level, and the data has a field of zeros around it, I get an unexpected contour between the zeros and anything positive.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

y = np.linspace(-10, 10, 20)
x_2d, y_2d = np.meshgrid(y, y)

data = np.cos(x_2d * np.pi/10.) + np.cos(y_2d * np.pi/10.)
np.place(data, data < 0., 0.)

levels = [3, 6]
colors = ['b', 'r']

plt.contour(data, levels=levels, colors=colors)

plt.show()

Actual outcome

Expected outcome

The plot should be blank, which is what happens if the call to np.place is skipped.

Matplotlib version

  • Operating system: RHEL7.9
  • Matplotlib version (import matplotlib; print(matplotlib.__version__)): 3.4.1
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.8.8
  • Jupyter version (if applicable): N/A
  • Other libraries: numpy 1.20.2

Activity

  1. ianthomas23 commented on May 11, 2021

    Member

    I've confirmed that this is a bug. If the levels for a line contour plot are all outside of the z limits, the levels are replaced by the lower z limit, here:

    if not self.filled:
    inside = (self.levels > self.zmin) & (self.levels < self.zmax)
    levels_in = self.levels[inside]
    if len(levels_in) == 0:
    self.levels = [self.zmin]
    _api.warn_external(
    "No contour levels were found within the data range.")

    Here is a simple reproducer:

    import matplotlib.pyplot as plt
    
    z = [[0, 0], [0, 1]]
    cs = ax[0].contour(z, levels=levels)
    ax[0].set_title(f'levels_in={levels}, levels_out={cs.levels}')
    
    levels = [2]
    cs = ax[1].contour(z, levels=levels)
    ax[1].set_title(f'levels_in={levels}, levels_out={cs.levels}')
    
    fig.tight_layout()
    plt.show()


    Under most circumstances we'd get away with this behaviour. However, if there is a quad which has the lower-z value on 3 of its 4 corners then half the area of the quad is at the lower level and the contour line is calculated on the upper edge of this triangle (see #19691 for detailed explanation).

    I presume that if the user specifies the contour levels then we should not modify them, even if this means that no contour lines are plotted. But I suspect there may be issues about colorbars being displayed without any contents?

  2. rcomer commented on May 16, 2021

    MemberAuthor

    Thanks @ianthomas23 for taking a look at this and providing an explanation. With this knowledge we can make a simple workaround in the user code by setting a new, artificial, localised minimum before plotting. E.g.

    data[0, 0] = -1
  3. linked a pull request that will close this issueRemove contour warning for "no-valid-levels". #24912on Jan 9, 2023
  4. added this to the v3.7.0 milestone on Jan 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL