Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

streamline plots #10775

Closed
jasongrout opened this issue Feb 12, 2011 · 50 comments
Closed

streamline plots #10775

jasongrout opened this issue Feb 12, 2011 · 50 comments

Comments

@jasongrout
Copy link
Member

Recently there was a thread on the matplotlib list where someone posted matplotlib code for a streamline plot. It looks very nice. If the code doesn't get included in matplotlib, we could still ask about including it in Sage to do streamline plots.

Mailing list message: http://permalink.gmane.org/gmane.comp.python.matplotlib.general/26362

Mailing list thread: http://comments.gmane.org/gmane.comp.python.matplotlib.general/26354

Example code: http://web.mit.edu/speth/Public/streamlines.py

Example plot: http://web.mit.edu/speth/Public/streamlines.png

Upstream: Completely fixed; Fix reported upstream

CC: @kcrisman @alauve @sagetrac-jakobkroeker @paulmasson @sagetrac-jhonrubia6

Component: graphics

Author: Paul Masson

Branch/Commit: 640cb6a

Reviewer: Travis Scrimshaw

Issue created by migration from https://trac.sagemath.org/ticket/10775

@jasongrout
Copy link
Member Author

comment:1

And here is another file from the same message with some fantastically nice plots:

Code: http://www.atm.damtp.cam.ac.uk/people/tjf37/streamplot.py

Result: http://www.atm.damtp.cam.ac.uk/people/tjf37/streamlines1.png

Result 2: http://www.atm.damtp.cam.ac.uk/people/tjf37/streamlines2.png

@jasongrout
Copy link
Member Author

Attachment: streamlines.py.gz

Attachment: streamplot.py.gz

@jasongrout
Copy link
Member Author

comment:2

I've attached both code files so that we have them even if the above links go down for whatever reason.

@jasongrout
Copy link
Member Author

comment:3

See http://sagenb.org/home/pub/3237/ for a short example and rough Sage code (posted at http://sage.math.washington.edu/home/jason/sage-field-plots/)

@kcrisman
Copy link
Member

comment:4

Yo, this is now in mpl!

  • the file
  • the commit
    I'm not sure how to figure out which release it's in, if it's in a release yet. It looks like it will probably be in 1.1.1, as 1.1.0 has been out for longer than this was in.

@kcrisman
Copy link
Member

Upstream: Fixed upstream, but not in a stable release.

@jasongrout
Copy link
Member Author

comment:5

See also http://sage.cs.drake.edu/home/pub/60/ for another example.

@jasongrout
Copy link
Member Author

comment:6

Attachment: DE Solutions (1).sws.gz

I attached the example I pointed out in the sage.cs.drake.edu server.

@kcrisman
Copy link
Member

kcrisman commented Mar 3, 2013

comment:7

#13693 is for the appropriate mpl upgrade for this.

@jdemeyer jdemeyer modified the milestones: sage-5.11, sage-5.12 Aug 13, 2013
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.1, sage-6.2 Jan 30, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.2, sage-6.3 May 6, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.3, sage-6.4 Aug 10, 2014
@kcrisman
Copy link
Member

comment:12

Here are some upstream examples.


Here is another example from somewhere in this discussion, allowing phase plots. Not directly related but looks nice with such things.

# based on code from oddrobot, http://sagenb.org/home/pub/1532/
from sage.calculus.desolvers import desolve_system_rk4
x,y,t=var('x y t')
class DESolution:
    def __init__(self,system,time_range,initial,stepsize=0.05):
        
        self.tvar=time_range[0]
        self._times=srange(time_range[1],time_range[2],stepsize)
        self.vars=[v for v,_ in initial]
        self.dim=len(self.vars)
        self._soln=desolve_odeint(system, ics=[v for _,v in initial], times=self._times, dvars=self.vars, ivar=self.tvar)
        
    def phase_plot(self,vars=None,color='blue',**kwargs):
        # find which indices the specified variables are
        if vars is not None:
            vars_index=[self.vars.index(v) for v in vars]
        elif self.dim<=3:
            vars_index=range(self.dim)
        else:
            vars_index=range(2)
            
        p=line(self._soln[:,vars_index],color=color,**kwargs)
        # add an arrow head showing which way we are going around the phase line
        half=int(self._soln.shape[0]/2)
        p+=arrow(self._soln[half,vars_index], self._soln[half+1,vars_index],color=color)
        if len(vars_index)==2:
            p.axes_labels([str(self.vars[v]) for v in vars_index])
        return p
        
    def coordinates(self,colors=None,**kwargs):
        if colors is None:
            colors=rainbow(len(self.vars))
        p=Graphics()
        p+=sum(line(zip(self._times,self._soln[:,i]), color=colors[i], legend_label=str(self.vars[i]),**kwargs) for i in range(self.dim))
        return p

@tscrim
Copy link
Collaborator

tscrim commented Sep 12, 2016

comment:13

I was hoping we could resurrect this ticket as I am teaching a class on ODE's this semester and would like to plot streamlines on top of the slope fields. Does someone have some time to work on this? Alas, I don't know how much time I might have to do so.

So streamlines are included in matplotlib: http://matplotlib.org/examples/images_contours_and_fields/streamplot_demo_features.html

There is also another plot package I came across that is open-source python that does streamlines: https://plot.ly/python/streamline-plots/

@tscrim
Copy link
Collaborator

tscrim commented Sep 12, 2016

Changed upstream from Fixed upstream, but not in a stable release. to Completely fixed; Fix reported upstream

@tscrim tscrim modified the milestones: sage-6.4, sage-7.4 Sep 12, 2016
@paulmasson
Copy link
Mannequin

paulmasson mannequin commented Sep 13, 2016

comment:14

Since I spent too much time today reading graphics code, I can take a crack at this. It looks like a new class is needed, modeled on PlotField but calling the pyplot method streamplot instead of quiver. It shouldn't be too difficult but won't happen overnight.

Travis, if you see a way to avoid a new class, then let me know before I dive in tomorrow.

@paulmasson
Copy link
Mannequin

paulmasson mannequin commented Sep 13, 2016

comment:15

To clarify: by new class I mean an individual file like scatter_plot.py, which calls the pyplot method scatter.

@tscrim
Copy link
Collaborator

tscrim commented Sep 23, 2016

Changed commit from 9201066 to 584486b

@paulmasson
Copy link
Mannequin

paulmasson mannequin commented Sep 23, 2016

@paulmasson
Copy link
Mannequin

paulmasson mannequin commented Sep 23, 2016

comment:27

Ah, so that's what you had in mind for the additions.

The documentation would not build without some fixes. Sphinx plots do not allow ^ for exponentiation, needs to be **, and variable declarations do not carry over between them. Documentation now builds.


New commits:

9f3a5c2Fix Sphinx plots

@paulmasson
Copy link
Mannequin

paulmasson mannequin commented Sep 23, 2016

Changed commit from 584486b to 9f3a5c2

@paulmasson
Copy link
Mannequin

paulmasson mannequin commented Sep 23, 2016

comment:28

Don't know if you noticed, Travis, but in the example with start_points the streamlines come close to the start points but do not pass through them. I thought it might be because you didn't process the start points as in this example

http://matplotlib.org/examples/images_contours_and_fields/streamplot_demo_start_points.html

but even with setting up a numpy array as indicated and transposing it, the streamlines still do not pass through the start points. In fact the resulting graph is exactly the same as not bothering to process the points, so apparently that isn't necessary.

Problem with streamplot? Thought you should know before any students ask what's going on.

@tscrim
Copy link
Collaborator

tscrim commented Sep 23, 2016

comment:29

Thanks for fixing that Paul. I guess we should add a note and example about the behavior of start_points. Also, do you want me to document the density option?

Actually, I think I should not be normalizing the input when we want to consider it as a slope field, which makes sense when considering plotting the tangent lines. I believe it just adds a unnecessary computation that cancels when considering splitting dy / dx to (dx, dy).

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 24, 2016

Branch pushed to git repo; I updated commit sha1. New commits:

78c53a0Improve documentation and example
478b1e1Code block for start_points

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 24, 2016

Changed commit from 9f3a5c2 to 478b1e1

@paulmasson
Copy link
Mannequin

paulmasson mannequin commented Sep 24, 2016

comment:31

Documentation for implemented plot options now included. I also updated your language for start_points using that from the matplotlib website and added the note.

I'm including a commented code block for the official way to handle start_points in the event it is useful later.

@tscrim
Copy link
Collaborator

tscrim commented Sep 24, 2016

comment:32

I disagree with adding start_points to the @options, where if that is changed, it is perpetuated in all future plots, which is not what we want. I will revert this back when I make my next round of changes.

Does the "official way" for start_points make any difference?

@paulmasson
Copy link
Mannequin

paulmasson mannequin commented Sep 24, 2016

comment:33

Replying to @tscrim:

Does the "official way" for start_points make any difference?

Not that I can tell. xpos_array and ypos_array need to be explicit numpy arrays to avoid errors. The documentation says that start_points is also supposed to be a numpy array, so I added this block of commented code in case that is ever enforced, but you can remove it if you feel that is more appropriate.

@tscrim
Copy link
Collaborator

tscrim commented Sep 27, 2016

comment:34

Let's use the official way. Also (not that it makes any real difference), you don't need to pop off the option:

    if 'start_points' in options:
        xstart_array, ystart_array = [], []
        for point in options['start_points']:
            xstart_array.append(point[0])
            ystart_array.append(point[1])
        options['start_points'] = numpy.array([xstart_array, ystart_array]).T

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 27, 2016

Branch pushed to git repo; I updated commit sha1. New commits:

79e8795Update start_points handling

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 27, 2016

Changed commit from 478b1e1 to 79e8795

@tscrim
Copy link
Collaborator

tscrim commented Sep 29, 2016

comment:36

Thank you for all your work on this.

@kiwifb
Copy link
Member

kiwifb commented Oct 3, 2016

comment:37

Not sure what Volker will find in his current run but I am failing to build the documentation in sage-on-gentoo

[plotting ] /scratch2/portage/sci-mathematics/sage-9999/work/sage-9999/src-python2_7/build/lib/sage/plot/streamline_plot.py:docstring of sage.plot.streamline_plot.streamline_plot:35: WARNING: Exception occurred in plotting streamline_plot-1
[plotting ] from /scratch2/portage/sci-mathematics/sage-9999/work/sage-9999/src-python2_7/doc/en/reference/plotting/sage/plot/streamline_plot.rst:
[plotting ] Traceback (most recent call last):
[plotting ] File "/usr/lib64/python2.7/site-packages/matplotlib/sphinxext/plot_directive.py", line 517, in run_code
[plotting ] six.exec_(code, ns)
[plotting ] File "/usr/lib64/python2.7/site-packages/matplotlib/externals/six.py", line 672, in exec_
[plotting ] exec("""exec _code_ in _globs_, _locs_""")
[plotting ] File "<string>", line 1, in <module>
[plotting ] File "<string>", line 2, in <module>
[plotting ] File "/scratch2/portage/sci-mathematics/sage-9999/work/sage-9999/src-python2_7/build/lib/sage/misc/decorators.py", line 554, in wrapper
[plotting ] return func(*args, **options)
[plotting ] File "/scratch2/portage/sci-mathematics/sage-9999/work/sage-9999/src-python2_7/build/lib/sage/plot/streamline_plot.py", line 316, in streamline_plot
[plotting ] for point in options['start_points']:
[plotting ] TypeError: 'NoneType' object is not iterable

I have this several times. I note that the bots have a similar problem in their logs.

@tscrim
Copy link
Collaborator

tscrim commented Oct 3, 2016

comment:38

I had thought the first part of comment:32 was done by Paul. I removed start_points from the @options and did some little doc tweaking. Paul, can you make a quick check of my changes?


New commits:

1e2b2d0Merge branch 'u/paulmasson/10775' of git://trac.sagemath.org/sage into u/tscrim/streamline_plot-10775
fa8cda9Merge branch 'u/paulmasson/streamline_plot-10775' of git://trac.sagemath.org/sage into u/tscrim/streamline_plot-10775
640cb6aSome last little touchups.

@tscrim
Copy link
Collaborator

tscrim commented Oct 3, 2016

@tscrim
Copy link
Collaborator

tscrim commented Oct 3, 2016

Changed commit from 79e8795 to 640cb6a

@paulmasson
Copy link
Mannequin

paulmasson mannequin commented Oct 3, 2016

comment:39

Documentation now builds. Setting back to positive review.

@vbraun
Copy link
Member

vbraun commented Oct 21, 2016

Changed branch from u/tscrim/streamline_plot-10775 to 640cb6a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants