Opened 3 years ago

Last modified 3 years ago

#21618 needs_work defect

plot in piecewise regression

Reported by: kcrisman Owned by:
Priority: minor Milestone: sage-7.4
Component: symbolics Keywords:
Cc: paulmasson Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: u/rws/21618-1 (Commits) Commit: c008c29052c1c035d1c555503c823053a77955dd
Dependencies: Stopgaps:

Description (last modified by novoselt)

P = piecewise([((0,2),x),((2,6),2)])
print P
plot(P)

Before this would have plotted the whole thing, now it defaults to the usual plotting only from -1 to 1 (which of course gives an error).

Moreover pieces are joined and detect_poles=True option skips the origin for no good reason:

P = piecewise([((-1,2),x),((2,5),1)])
print P
plot(P, -1, 5, detect_poles=True)

Change History (12)

comment:1 Changed 3 years ago by novoselt

  • Cc paulmasson added
  • Description modified (diff)

comment:2 Changed 3 years ago by paulmasson

  • Summary changed from plot in piecewise regression (?) to plot in piecewise regression

The old Piecewise has a custom plot method. This method is omitted in the current piecewise.

comment:3 Changed 3 years ago by kcrisman

Correct. So it would be nice to emulate that so everyone's code doesn't break - I had a not-so-nice time drawing graphs on the fly in class today because of this. It seems reasonable, since piecewise is an unusual situation.

comment:4 Changed 3 years ago by rws

  • Branch set to u/rws/plot_in_piecewise_regression

comment:5 Changed 3 years ago by rws

  • Commit set to c98505b6541783a93eaa84721f564bd1e311285b
  • Status changed from new to needs_review

Oops I just saw the second complaint. This will not be as easy.


New commits:

c98505b21618: plotting piecewise without need of giving domain interval

comment:6 Changed 3 years ago by rws

  • Status changed from needs_review to needs_work

comment:7 Changed 3 years ago by rws

Note that your command plot(P, -1, 5, detect_poles=True) also does not work on Sage-7.1, i.e., the old piecewise, and plot(P, detect_poles=True) also has a gap at x=0. So, this particularly is rather an enhancement of plot, not a bug in piecewise.

comment:8 Changed 3 years ago by rws

  • Branch changed from u/rws/plot_in_piecewise_regression to u/rws/21618-1

comment:9 Changed 3 years ago by rws

  • Commit changed from c98505b6541783a93eaa84721f564bd1e311285b to c008c29052c1c035d1c555503c823053a77955dd
  • Status changed from needs_work to needs_review

New commits:

c008c2921618: plotting piecewise without need of giving domain interval

comment:10 Changed 3 years ago by novoselt

  • Status changed from needs_review to needs_work
  1. Why there is commented code? Reference to some ticket explaining stuff is fine, but not strange imports etc.
  2. Why there is extra printing of pieces???
  3. If I do specify the domain, then each piece is plotted over this domain!

I'll see if I can resolve all this in the next hour or so.

comment:11 Changed 3 years ago by novoselt

Address 1&2 and simplify a bit to get

        def plot(cls, self, parameters, variable, *args, **kwds):
            from sage.plot.all import plot, Graphics
            g = Graphics()
            for (dom, fun) in self.items():
                g += plot(fun, (variable, dom.inf(), dom.sup()), *args, **kwds)
                # If it's the first piece, pass all arguments. Otherwise,
                # filter out 'legend_label' so that we don't add each
                # piece to the legend separately (trac #12651).
                kwds.pop('legend_label', None)
            return g

3 is tricky, however, because of a variety of ways to pass left and right bounds. I think we should factor out the logic of getting these bounds into a separate function which will say always set xmin/xmax keywords, then we can call this function in the top level plot as well as plot methods and have a guaranteed uniform behavior. Thoughts?

There is also

  1. No documentation and examples at all!!!

comment:12 Changed 3 years ago by rws

Andrey, please go ahead with your own branch, the plotting code is quite opaque and uninteresting to me, and I'm into other projects right now.

Note: See TracTickets for help on using tickets.