Visualizing a vector field with Matplotlib

(13 comments)

Matplotlib provides a function, streamplot, to create a plot of streamlines representing a vector field. The following program displays a representation of the electric field vector resulting from a multipole arrangement of charges. The multipole is selected as a power of 2 on the command line (1=dipole, 2=quadrupole, etc.)

It requires Matplotlib 1.5+ because of the choice of colormap (plt.cm.inferno): this can be replaced with another (for example plt.cm.hot) if using an older version of Matplotlib.

import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle

def E(q, r0, x, y):
    """Return the electric field vector E=(Ex,Ey) due to charge q at r0."""
    den = np.hypot(x-r0[0], y-r0[1])**1.5
    return q * (x - r0[0]) / den, q * (y - r0[1]) / den

# Grid of x, y points
nx, ny = 64, 64
x = np.linspace(-2, 2, nx)
y = np.linspace(-2, 2, ny)
X, Y = np.meshgrid(x, y)

# Create a multipole with nq charges of alternating sign, equally spaced
# on the unit circle.
nq = 2**int(sys.argv[1])
charges = []
for i in range(nq):
    q = i%2 * 2 - 1
    charges.append((q, (np.cos(2*np.pi*i/nq), np.sin(2*np.pi*i/nq))))

# Electric field vector, E=(Ex, Ey), as separate components
Ex, Ey = np.zeros((ny, nx)), np.zeros((ny, nx))
for charge in charges:
    ex, ey = E(*charge, x=X, y=Y)
    Ex += ex
    Ey += ey

fig = plt.figure()
ax = fig.add_subplot(111)

# Plot the streamlines with an appropriate colormap and arrow style
color = np.log(np.hypot(Ex, Ey))
ax.streamplot(x, y, Ex, Ey, color=color, linewidth=1, cmap=plt.cm.inferno,
              density=2, arrowstyle='->', arrowsize=1.5)

# Add filled circles for the charges themselves
charge_colors = {True: '#aa0000', False: '#0000aa'}
for q, pos in charges:
    ax.add_artist(Circle(pos, 0.05, color=charge_colors[q>0]))

ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
ax.set_aspect('equal')
plt.show()

The electric field of a dipole: $ python efield.py 1 Electric field of a dipole

The electric field of an octopole: $ python efield.py 3 Electric field of an octopole

Current rating: 4.6

Comments

Stafford 1 year, 2 months ago

What clever program, Loved it. I had to change the 'inferno' to 'hot' as we were warned in the opening comments. Didn't
realise I was using an outdated version of matplotlib.

Link | Reply
Current rating: 5

christian 1 year, 2 months ago

Thanks, Stafford – the issue of colormaps is one that exercises some people a great deal: for a good article about how awful the default colormap, jet, is, take a look here: https://jakevdp.github.io/blog/2014/10/16/how-bad-is-your-colormap/

Link | Reply
Current rating: 1

Steven Armour 5 months, 1 week ago

Would you be opposed to me publishing a reproduction of this example that I made with the python yt lib (http://yt-project.org/) to the yt repository examples archive? (they need non-astro examples like nothing else) I have already made a prototype of what I am purposing for my E&M class last fall and would also like you to review the photo notebook as well as the final notebook to make sure there are no errors in trying to visualize the vector field volumetrically.

Sincerly
Steven Armour

Link | Reply
Currently unrated

christian 5 months, 1 week ago

Not at all – you're welcome to the example and any code here you find useful. I'd be happy to take a look at the Notebook you are preparing for your EM class.
Best wishes,
Christian

Link | Reply
Currently unrated

Youcef 3 months, 1 week ago

Hello,
i have some trouble running this program, it gives me this error if you can help me figure out what's the problem:

nq = 2**int(sys.argv[1])
IndexError: list index out of range

Thanks for your time

Link | Reply
Currently unrated

christian 3 months, 1 week ago

Hi Youcef,
The program expects the user to provide the order of the multipole on the command line (e.g. 1 for dipole, 2 for quadrupole etc.), so you should run the code with e.g.

python multipole.py 3

to plot a figure for an octopole.

Link | Reply
Current rating: 5

Youcef 3 months, 1 week ago

Hello again Christian,

I did what you told me and it worked, so thanks a lot for the help and the quick response !

good day

Link | Reply
Currently unrated

Lina 1 month, 3 weeks ago

Is it possible to get the coordinates of the different streamlines ?
Also how can I interpolate between the streamlines ?

Link | Reply
Currently unrated

christian 1 month, 3 weeks ago

Hi Lina,
The call to streamplot returns a container object, stream_container, from which you can obtain the lines making up the plot as stream_container.lines, and get their coordinates from there.

If you want the lines to be closer together, you can set the density argument to streamplot when you call it, as described in the documentation: [http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.streamplot]

Link | Reply
Currently unrated

Mark 1 month, 1 week ago

Perfect for creating custom field plots to add into my teaching notes.

Link | Reply
Currently unrated

christian 1 month, 1 week ago

I'm glad you find it useful – thanks for dropping by.

Link | Reply
Currently unrated

Keith 1 month ago

In a couple of places hypot(x,y) should be used instead of sqrt(x**2+y**2).

Link | Reply
Currently unrated

christian 1 month ago

That's a good idea – I've made the changes.
Thanks, Christian

Link | Reply
Currently unrated

New Comment

required

required (not published)

optional

required