3

I am trying to add start points to a streamline plot. I found an example code using start points here; at this link a different issue is discussed but the start_points argument works. From here I grabbed the streamline example code (images_contours_and_fields example code: streamplot_demo_features.py). I don't understand why I can define start points in one code and not the other. I get the following error when I try to define start points in the example code (streamplot_demo_features.py):

    Traceback (most recent call last):

  File "<ipython-input-79-981cad64cff6>", line 1, in <module>
    runfile('C:/Users/Admin/.spyder/StreamlineExample.py', wdir='C:/Users/Admin/.spyder')

  File "C:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/Users/Admin/.spyder/StreamlineExample.py", line 28, in <module>
    ax1.streamplot(X, Y, U, V,start_points=start_points)

  File "C:\ProgramData\Anaconda2\lib\site-packages\matplotlib\__init__.py", line 1891, in inner
    return func(ax, *args, **kwargs)

  File "C:\ProgramData\Anaconda2\lib\site-packages\matplotlib\axes\_axes.py", line 4620, in streamplot
    zorder=zorder)

  File "C:\ProgramData\Anaconda2\lib\site-packages\matplotlib\streamplot.py", line 144, in streamplot
    sp2[:, 0] += np.abs(x[0])

ValueError: non-broadcastable output operand with shape (1,) doesn't match the broadcast shape (100,)

I've notice there isn't much on the web in way of using start_points, so any additional information would be helpful.

1
  • Stack Overflow is stupid and wouldn't let me add the following link to my post: matplotlib.org/devdocs/api/_as_gen/…. From here I can see the required inputs for start_points, but there isn't much else information. Commented May 12, 2017 at 3:22

1 Answer 1

7

The main difference between the example that successfully uses start_points and the example from the matplotlib page is that the first uses 1D arrays as x and y grid, whereas the official example uses 2D arrays.

Since the documentation explicitely states

x, y : 1d arrays, an evenly spaced grid.

we might stick to 1D arrays. It's unclear why the example contradicts the docsting, but we can simply ignore that.

Now, using 1D arrays as grid, start_points works as expected in that it takes a 2-column array (first column x-coords, second y-coords).

A complete example:

import numpy as np
import matplotlib.pyplot as plt

x,y = np.linspace(-3,3,100),np.linspace(-3,3,100)
X,Y = np.meshgrid(x,y)
U = -1 - X**2 + Y
V = 1 + X - Y**2
speed = np.sqrt(U*U + V*V)

start = [[0,0], [1,2]]

fig0, ax0 = plt.subplots()

strm = ax0.streamplot(x,y, U, V, color=(.75,.90,.93))
strmS = ax0.streamplot(x,y, U, V, start_points=start, color="crimson", linewidth=2)

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, thank you. That makes much more sense knowing that within the streamplot function, the error was coming from the fact that it could not broadcast the start_points array with the x[0] and y[0] arrays, which was because those arrays were supposed to be only 1D.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.