Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ENH]: Allow contour levels to be decreasing #31227

@kmhess

Description

@kmhess

Problem

I'm plotting contours on spatial images where the data can be positive or negative. In my case, they are astronomical images where I can have emission (positive) or absorption (negative).

When plotting positive contours matplotlib allows me to specify an array with values well beyond (more positive) than those in the map so long as the first one is in the range of the data and can be plotted, and so long as the contour levels are increasing. This is a good because I don't necessarily know the strength of the emission beforehand, but I know the lowest contour (closest to 0) that I'd like to plot. The lowest contour represents some multiple of the noise in the image.

However, the opposite isn't true for negative contours: I'd like to be able to plot the value closest to zero first and then plot contours of decreasing value (becoming more negative). This is desirable because I don't necessarily know how deep the absorption is ahead of time. I can't do this at the moment because matplotlib requires that the levels are increasing.

My current code looks like this:

# Plot positive contours
if np.isfinite(base_contour):
    ax1.contour(hdulist_hi[0].data, cmap='Oranges', linewidths=1, levels=base_contour * 2 ** np.arange(10),
            transform=ax1.get_transform(cubew))
# Plot negative contours
if np.nanmin(hdulist_hi[0].data) < -base_contour and np.isfinite(base_contour):
    ax1.contour(hdulist_hi[0].data, cmap='BuPu_r', linewidths=1.2, linestyles='dashed',
                levels=-base_contour * 2 ** np.arange(10, -1, -1), transform=ax1.get_transform(cubew))

It fails if -base_contour * 2 ** 10 is not in the map. Yes, I can try to detect ahead of time what the first valid negative contour would be...but the ability to plot decreasing sequential contours would be mathematically more elegant, and would have a lot of practical applications.

Proposed solution

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions