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

Streamplot ignores rightmost column and topmost row of velocity data #11452

Closed
pmackenz opened this issue Jun 17, 2018 · 9 comments
Closed

Streamplot ignores rightmost column and topmost row of velocity data #11452

pmackenz opened this issue Jun 17, 2018 · 9 comments
Milestone

Comments

@pmackenz
Copy link
Contributor

Bug report

Bug summary

streamplot ignores the rightmost column (and topmost row) in U and V and replaces them by a copy of the second to last column (row). This yields wrong stream lines in the rightmost column and topmost row of cells. The issue is most noticeable on coarse grids. (see example plots)

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-1, 1, 3)
y = np.linspace(0, 1, 2)

U = np.array([[.2,.2,.2],[.2,1,.2]])
V = np.array([[-1,2,-1],[-1,0.,-1]])

plt.streamplot(x,y, U,V)
plt.quiver(x,y, U,V)
plt.xlim(-1.5,1.5)
plt.ylim(-0.5,1.5)
plt.savefig('StreamPlotIssue1.png')
plt.close()

x = np.linspace(-1, 1, 5)
y = np.linspace(0, 1, 4)

U = np.array([[.2,.2,.2,.2,.2],[.2,.2,.2,.2,.2],[.2,.2,.2,.2,.2],[.2,1.,1.,1,.2]])
V = np.array([[-1,.5,2,.5,-1],[-1,.5,2,.5,-1],[-1,.5,2,.5,-1],[-1,0.,0.,0.,-1]])

plt.streamplot(x,y, U,V)
plt.quiver(x,y, U,V)
plt.xlim(-1.5,1.5)
plt.ylim(-0.5,1.5)
plt.savefig('StreamPlotIssue2.png')
plt.close()

Actual outcome

The first image uses just 2 cells (2 in x, 1 in y direction). The field should create an antisymmetric wave. The left cell is OK, the right one is garbage. (The example is chosen such that the top row issue doesn't show too badly). The overlaid quiver plot shows the correct velocity.

streamplotissue1

The second image uses 4 cells in x and 3 cells in y-direction for the same velocity field. Quiver shows the correct velocity as overlay. The rightmost column and top row of cells show the wrong streamlines.

streamplotissue2

For quick reference: the variables used

# image 1
#x:
[-1.  0.  1.]
# y:
[0. 1.]
# U:
[[0.2 0.2 0.2]
 [0.2 1.  0.2]]
# V:
[[-1.  2. -1.]
 [-1.  0. -1.]]

# image 2

#x:
[-1.  -0.5  0.   0.5  1. ]
# y:
[0.         0.33333333 0.66666667 1.        ]
# U:
[[0.2 0.2 0.2 0.2 0.2]
 [0.2 0.2 0.2 0.2 0.2]
 [0.2 0.2 0.2 0.2 0.2]
 [0.2 1.  1.  1.  0.2]]
# V:
[[-1.   0.5  2.   0.5 -1. ]
 [-1.   0.5  2.   0.5 -1. ]
 [-1.   0.5  2.   0.5 -1. ]
 [-1.   0.   0.   0.  -1. ]]

Expected outcome

A streamline plot consistent with velocity shown in quiver overlay.

Matplotlib version

  • Operating system:
    MacOS High Sierra (10.13.5)
  • Matplotlib version:
    matplotlib-2.1.2-py36h6d6146d_0
  • Matplotlib backend (print(matplotlib.get_backend())):
    MacOSX
  • Python version:
    3.6
  • Jupyter version (if applicable):
    n/a
  • Other libraries:
    numpy

Python and matplotlib installed using anaconda3 package

@pharshalp
Copy link
Contributor

pharshalp commented Jun 18, 2018

The interpolation (in the case of streamplot) might be failing because of fewer number of grid values specified (the V component changes sign and only 3 points are used to capture this change).

If I increase the number of points to 9 (adding evenly spaced values) then the result seems to be correct...

streamplotissue_more_points

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-1, 1, 9)
y = np.linspace(0, 1, 2)

U = np.array([[.2, .2, .2, .2, .2, .2, .2, .2, .2],
[.2, 0.4, 0.6, 0.8, 1, 0.8, 0.6, 0.4, .2]])
V = np.array([[-1, -0.25, 0.5, 1.25, 2, 1.25, 0.5, -0.25, -1],
[-1, -0.75, -0.5, -0.25, 0., 0.25, 0.5, 0.75, -1]])

plt.streamplot(x, y, U, V)
plt.quiver(x, y, U, V)
plt.xlim(-1.5, 1.5)
plt.ylim(-0.5, 1.5)
plt.savefig('StreamPlotIssue_more_points.png')
plt.close()

@jklymak
Copy link
Member

jklymak commented Jun 18, 2018

A streamline is defined as the integral of the crossstream velocity, so I wouldn’t be surprised that there are edge effects for a digital representation. If you need a smoother stream plot interpolate the data first.

@pmackenz
Copy link
Contributor Author

pmackenz commented Jun 18, 2018

pharshalp, your example shows the same faulty streamplot. Per definition, tangents to streamlines are the velocity. Look at the top, second from the right. There are about 100 degrees between the tangent and the velocity. The integration is simply wrong for the top and the right. Making it larger just makes the faulty domain smaller, but it does not make it right.

jklymak, if the tangent to the streamline does not line up with the velocity, the streamline is wrong. No maybe, no "would not be surprised", just wrong. To make the point stronger: the streamlines are perfect on the left and at the bottom and anywhere but the top and right side.

As for the remaining suggestions: 1 cell (2x2 points) defines a unique interpolated velocity field that can be integrated on a cell by cell base, so even for a single cell. matplotlib uses only the bottom left value when using a single cell. It ignores to top and right.

Proposed issue: somewhere in the code an index goes to number of cells instead of number of nodes, which is cells+1. It is a faulty loop or indexing problem on the upper limit of the horizontal and the vertical limit. FYI, Wolfram mathematica shows how to do it right, even on a single cell.

@jklymak
Copy link
Member

jklymak commented Jun 18, 2018

Any finite representation will have gridscale inaccuracies compared to a formal definition. If you have a proposed algorithm that deals with those inaccuracies in a different way than the matplotlib implementation of streamline a pull request would receive due attention.

@pmackenz
Copy link
Contributor Author

pmackenz commented Jun 18, 2018 via email

@pmackenz
Copy link
Contributor Author

Pull Request with bug fix submitted :)

Here the new, now correct, output of the above sample code:
streamplotissue1
streamplotissue2

@pmackenz
Copy link
Contributor Author

... and here the correct streamlines for your data set:
streamplotissue_more_points

@ImportanceOfBeingErnest
Copy link
Member

Link to potential fix: #11455

@dstansby dstansby added this to the v3.0 milestone Jun 25, 2018
@efiring
Copy link
Member

efiring commented Jul 3, 2018

closed by #11455

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

No branches or pull requests

6 participants