Ticket #4976 (closed enhancement: fixed)

Opened 20 months ago

Last modified 19 months ago

[with patch, positive review] fill option for plot, polar_plot and parametric_plot

Reported by: whuss Owned by: whuss
Priority: major Milestone: sage-3.3
Component: graphics Keywords:
Cc: Author(s):
Report Upstream: Reviewer(s):
Merged in: Work issues:

Description

This patch adds the new options "fill", "fillcolor", and "fillalpha" to the plot functions, which allow to fill the area between two functions in a plot, or to fill the area between the function and the x-axis.

The syntax for fill is similar to what Mathematica uses.

I also attach a file with some examples for easier testing.

Greetings,
Wilfried

Attachments

fill_examples.sage Download (3.2 KB) - added by whuss 20 months ago.
some examples
fill1.png Download (31.6 KB) - added by whuss 20 months ago.
fill_plot.patch Download (19.4 KB) - added by whuss 19 months ago.
rebased for sage-3.3.alpha2
4976-reviewer.patch Download (0.9 KB) - added by kcrisman 19 months ago.
trac_4976_numerical_noise.patch Download (2.5 KB) - added by mabshoff 19 months ago.

Change History

Changed 20 months ago by whuss

some examples

Changed 20 months ago by whuss

follow-up: ↓ 4   Changed 19 months ago by kcrisman

I think that someone who does heavier-duty plotting (and who has a machine new enough that plot.py doesn't time out while testing) should give a final review, but it very nicely solves a lot of problems, and didn't break any of the examples I thought it might when I tried it. After some thought, probably putting generate_plot_points separately (and with very nice documentation) is a wise idea as well, and hopefully someone who originated that code will agree.

My only functional caveat is that you may want to put in a catch for fill=min and fill=max (as opposed to fill='min' and fill='max') because it parses these as functions, which they are, but it should probably raise an error (in both cases seems to have net effect of fill='axis'), since max and min are not really functions of one variable.

There should probably also be more explicit documentation of how the fillcolor option works, as opposed to seeing fillcolor='#ccc' in the tests - all legal input categories should be listed, perhaps there are others.

But that's all quibbles and my own ignorance; this is a beautiful addition, and someone else should review it very soon to avoid bitrot.

  Changed 19 months ago by jason

This patch seems to also be a big refactoring of the plot code, and also seems to change the code to plot a list of functions in multiple colors. I'm not disagreeing with it, but just commenting that this patch addresses more than the ticket says it does.

  Changed 19 months ago by jason

First, I really appreciate this patch and the beautiful functionality it adds. We've needed it for a long time. It also appears to do a much-needed refactoring of the plot code. I haven't tested the patch, though, or looked at it extremely closely.

Just a short comment about a piece of code. The patch uses map and max/min to calculate filling to the max or min of the function. It would be slightly faster and probably a lot clearer to just use list comprehensions:

sage: data=[(x,x*x) for x in [0..10,step=0.01]]
sage: timeit('min(map(lambda t: t[1], data))')
625 loops, best of 3: 1.29 ms per loop
sage: timeit('min(i[1] for i in data)')
625 loops, best of 3: 1.15 ms per loop

It would be orders of magnitude faster to use numpy to store the list of data. This would be a more major change in the plotting code, but here is the equivalent numpy code:

sage: import numpy
sage: ndata=numpy.array(data,dtype=float)
sage: timeit('numpy.max(ndata[:,1])')
625 loops, best of 3: 30.7 µs per loop

Changed 19 months ago by whuss

rebased for sage-3.3.alpha2

in reply to: ↑ 1   Changed 19 months ago by whuss

Replying to kcrisman:

My only functional caveat is that you may want to put in a catch for fill=min and fill=max (as opposed to fill='min' and fill='max') because it parses these as functions, which they are, but it should probably raise an error (in both cases seems to have net effect of fill='axis'), since max and min are not really functions of one variable.

In the new patch a warning gets printed if min or max is used as the parameter for fill.

Replying to jason:

The patch uses map and max/min to calculate filling to the max or min of the function. It would be slightly faster and probably a lot clearer to just use list comprehensions

I changed it to use generator comprehensions.

Changed 19 months ago by kcrisman

  Changed 19 months ago by kcrisman

  • summary changed from [with patch, needs review] fill option for plot, polar_plot and parametric_plot to [with patch, positive review] fill option for plot, polar_plot and parametric_plot
  • milestone changed from sage-3.4.1 to sage-3.3

Positive review off 3.3.alpha4; as we've all noted, this will be a great addition. I did try quite a bit to crack it, but couldn't; I can't check plot.py doctests, unfortunately, so there is still the potential, but I doubt it.

Two comments:

1. I added the reviewer patch because it was not clear that in parametric_plot, all fill options behave the same. This is ok behavior, I think, for the parametric case, but should be documented.

2. With the fix for max/min versus 'max'/'min', the behavior of max and min (without quotes) has changed, in a very interesting way; in fact, it behaves somewhat similarly to the parametric filling. I think it's a feature, not a bug, which should be documented and made easily available, but that should be on another ticket.

Changed 19 months ago by mabshoff

  Changed 19 months ago by mabshoff

  • status changed from new to closed
  • resolution set to fixed

Merged all three patches in Sage 3.3.alpha6.

Cheers,

Michael

Note: See TracTickets for help on using tickets.