Photon Light Photon Light - 1 year ago 335
Python Question

Plotting direction field in python

I want to plot direction field for a simple equation:

y' = 3 − 2y

I have found similar Matlab problem here (1.3). But I do not know how to rewrite it to python. My last try is:

from matplotlib.pyplot import cm
import matplotlib.pyplot as plt
import numpy as np

nx, ny = .3, .3
x = np.arange(-3, 3, nx)
y = np.arange(-2, 2, ny)
X, Y = np.meshgrid(x, y)

dy = X + np.sin(Y)
dx = np.ones((10,10))

plot2 = plt.figure()
plt.quiver(X, Y, dx, dy,

plt.title('Quiver Plot, Single Colour')

But I'm getting error:

builtins.ValueError: operands could not be broadcast together with shapes (100,) (280,)

I though it will be very simple, but after few hours of searching how to plot a simple direction field, I am quite depressed.

tom tom
Answer Source

dx and dy must be of the same shape as X and Y.

Currently, you have a shape of (14, 20) for X, Y and dy, but (10,10) for dx.

If you change the line defining dx to:

dx = np.ones(dy.shape)

Everything works fine:

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download