Opened 6 years ago

Closed 10 months ago

Last modified 10 months ago

#21042 closed defect (fixed)

Contour plot of zero fails with rich representation warning

Reported by: Paul Masson Owned by:
Priority: major Milestone: sage-9.5
Component: graphics Keywords:
Cc: Michael Orlitzky Merged in:
Authors: Michael Orlitzky Reviewers: Dima Pasechnik
Report Upstream: N/A Work issues:
Branch: 2d51836 (Commits, GitHub, GitLab) Commit:
Dependencies: Stopgaps:

Status badges

Description (last modified by Thierry Monteil)

Attempting the contour plot

var('x y')
contour_plot(0, (x,-1,1), (y,-1,1))

does not show a plot and fails with the error

/home/sc_serv/sage/local/lib/python2.7/site-packages/sage/repl/rich_output/display_manager.py:590: RichReprWarning: Exception in _rich_repr_ while displaying object: zero-size array to reduction operation minimum which has no identity
  RichReprWarning,

Graphics object consisting of 1 graphics primitive

Contour plots of nonzero values display as expected. It was reported on this ask question.

Change History (13)

comment:1 Changed 6 years ago by Thierry Monteil

Description: modified (diff)

comment:2 Changed 14 months ago by Michael Orlitzky

Cc: Michael Orlitzky added

It's not just the zero function, something else weird is going on here. This produces the line x=y like you'd expect:

contour_plot(x-y, (0,1), (0,1), contours=[0], fill=False)

But then this produces... nothing:

contour_plot(abs(x-y), (0,1), (0,1), contours=[0], fill=False)

Even though it should be the same plot. Which is weird, but not as weird as being able to fix it with...

contour_plot(abs(x-y)-0.000001, (0,1), (0,1), contours=[0], fill=False)

which gives you (roughly) the plot of x=y again.

comment:3 Changed 14 months ago by Samuel Lelièvre

I think contour plot and implicit plot work by detecting sign changes to know where something vanishes.

So no surprise they don't work well with absolute values and squares of some quantities even though those have the same vanishing loci.

comment:4 Changed 14 months ago by Samuel Lelièvre

It would make sense to document that and add examples.

comment:5 Changed 14 months ago by Michael Orlitzky

Actually... I don't think matplotlib expects levels (our contours) to contain only one element. We can probably fix this by using contours=[-epsilon, epsilon] instead of contours=[0], which does who-knows-what under the hood.

comment:6 Changed 14 months ago by Michael Orlitzky

Update: using contours=[-epsilon, epsilon] doesn't break any existing implicit plots, but how good it looks depends on epsilon and the function you're plotting. Some functions require a lot more plot points for a given epsilon, or a smaller xrange and yrange.

I wonder if there's a cheap heuristic we could use instead of a fixed tolerance.

comment:7 Changed 12 months ago by Michael Orlitzky

Authors: Michael Orlitzky
Branch: u/mjo/ticket/21042
Commit: b7e688b9a3f278d59a754948ab996873d661bece
Status: newneeds_review

This heuristic is real hacky, but it can only improve things; and the important part is that we now warn the user anyway.


New commits:

49be220Trac #21042: heuristic improvements for single-level contour plots.
c4c2852Trac #21042: add warnings to implicit and contour plot docstrings.
b7e688bTrac #21042: hide expected warnings from the sphinx plotter.

comment:8 Changed 12 months ago by git

Commit: b7e688b9a3f278d59a754948ab996873d661bece2d518364df619eb6211d4714e6b13ad85e651651

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

f02bee1Trac #21042: heuristic improvements for single-level contour plots.
7ca28d9Trac #21042: add warnings to implicit and contour plot docstrings.
2d51836Trac #21042: hide expected warnings from the sphinx plotter.

comment:9 Changed 10 months ago by Dima Pasechnik

Milestone: sage-7.3sage-9.5

Do we want this in? The milestone was off...

comment:10 Changed 10 months ago by Dima Pasechnik

Reviewers: Dima Pasechnik
Status: needs_reviewpositive_review

OK, looks good

comment:11 Changed 10 months ago by Volker Braun

Branch: u/mjo/ticket/210422d518364df619eb6211d4714e6b13ad85e651651
Resolution: fixed
Status: positive_reviewclosed

comment:12 Changed 10 months ago by Frédéric Chapoton

Commit: 2d518364df619eb6211d4714e6b13ad85e651651

This has broken our linter, by using a bad comparison to False.

comment:13 in reply to:  12 Changed 10 months ago by Samuel Lelièvre

Replying to chapoton:

This has broken our linter, by using a bad comparison to False.

See follow-up ticket for that at #33021.

Note: See TracTickets for help on using tickets.