Python Matplotlib Streamplot providing start points

耗尽温柔 提交于 2019-12-14 02:27:38

问题


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:


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()



来源:https://stackoverflow.com/questions/43928767/python-matplotlib-streamplot-providing-start-points

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!