Reputation: 41
I have been a long-time Mathematica user and I've been in the process of converting various notebooks into python (version 3). For plotting, I've been using matplotlib. I've ran into a snag and I am not sure what's going wrong.
I'm trying to convert the following Mathematica code:
(* simple electric dipole *)
Ex[x_, y_] := (x + 1)/((x + 1)^2 + y^2) - (x - 1)/((x - 1)^2 + y^2)
Ey[x_, y_] := y/((x + 1)^2 + y^2) - y/((x - 1)^2 + y^2)
StreamPlot[{Ex[x, y], Ey[x, y]}, {x, -3.5, 3.5}, {y, -3.5, 3.5}]
This produces the following figure:
I want to reproduce this using Python code.
from pylab import *
X,Y = meshgrid( arange(-4,4,.2),arange(-4,4,.2) )
Ex = (X + 1)/((X+1)**2 + Y**2) - (X - 1)/((X-1)**2 + Y**2)
Ey = Y/((X+1)**2 + Y**2) - Y/((X-1)**2 + Y**2)
figure()
Q = quiver( Ex, Ey)
l,r,b,t = axis()
dx, dy = r-l, t-b
axis([l-0.05*dx, r+0.05*dx, b-0.05*dy, t+0.05*dy])
show()
Produces the following figure:
I'm still learning how to do plotting in python, and I'm a little unsure of how to create a vector field plot. Any insight would be appreciated.
Upvotes: 1
Views: 5989
Reputation: 1055
This issue is that you have a very high intensity in your field at the nodes, so quiver scales it poorly. If we scale quiver with the argument scale=50
we get the plot:
However, if we use the streamplot function also in the pyplot we can get the following:
A much better plot if you ask me.
Here is the code for reference.
import numpy as np
import matplotlib.pyplot as plt
#%% Plot the fields
X,Y = np.meshgrid( np.arange(-4,4,.2), np.arange(-4,4,.2) )
Ex = (X + 1)/((X+1)**2 + Y**2) - (X - 1)/((X-1)**2 + Y**2)
Ey = Y/((X+1)**2 + Y**2) - Y/((X-1)**2 + Y**2)
plt.figure()
plt.streamplot(X,Y,Ex,Ey)
plt.title('streamplot')
plt.figure()
plt.quiver(X,Y,Ex,Ey,scale=50)
plt.title('quiver')
Upvotes: 2