matplotlib / matplotlib/matplotlib

[ENH]: Handling of closed streamlines in streamplot

Open
#27,261 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Documentation Documentation: examples topic: contour
Dominant language
Python
Stars
23.2k
Forks
8.5k
Avg merge
1d 6h
Merged PRs (30d)
66

Description

### Problem

With the current streamplot implementation, when asking for unbroken streamlines with given start points on fields with theoretically closed streamlines, the integration process spirals in practice until the maximum trajectory length is reached. The issue is more detailed, with an example and a workaround in [this stack overflow question and answer](https://stackoverflow.com/questions/77344931/how-can-i-avoid-spiraling-streamlines-in-matplotlib-pyplot-streamplot). When it is theoretically known that some streamlines should loop over themselves, we would like to be able to specify it and integrate the streamlines so that they are actually closed or, at least, stop the integration when a "quasi-loop" has been detected, by passing again close enough to a start point and/or when a 360 degrees rotation has been observed for the velocity vector.

### Proposed solution

A minimal and quite easy solution for single-direction integration could be to keep track of the accumulated rotation of the velocity vector along the trajectory, to continuously check the distance between the starting point and additional segments and to stop the integration at the closest point to the starting point after about one complete plus or minus 360 degrees velocity vector rotation and if the closest point is close enough to the starting point. Something similar though a bit more complicated could be done for two-direction integration.

A second-level solution would be to do exactly the same but while additionally modifying "on the fly" the original vector field with a very small rotation so that, after the integration, "the closest point to the starting point" becomes as close as possible to the starting point. The optimal "very small rotation" angle could be theoretically predicted and/or iteratively determined. Though this may appear as tampering with the source data, if there are good theoretical reasons to believe that the streamlines should be closed (the user should know what she or he is doing when using the "force_closed_streamlines" option), this is overall more likely to correct accumulated integration errors than to distort the result.

Having looked at the code, it seems to me that I should be able to implement this and maybe I will do so anyway for my personal use as the workaround that I am currently using is very impractical. However, it would certainly be done much better by matplotlib developers. Please let me know if this feature if of interest, if some matplotlib developers would consider implementing it, and if yes, how long it could take.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the streamplot implementation and the linked Stack Overflow example and workaround. Compare the proposed handling for single-direction and two-direction integration, then define how closed or quasi-looping trajectories should stop; done means closed streamlines no longer spiral until the maximum trajectory length is reached.

Written by the indexing model from the issue text.

Assessment

Tech stack
matplotlib, python
Domain
data-visualization
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.