Trouble Shooting Python Quiverplots

Viewed 47

I have been trying to get a plot of vector lines going using the matplotlib library and I keep getting something like this:

image

Not sure what is happening since the code I'm running seems to follow the syntax for how to make a basic quiver plot. I've tried messing with the array type to see if that's the issue but no luck. Some points on the plot just don't seem to be getting any vector data.

import matplotlib.pyplot as plt
import numpy as np


X = np.arange(-2,2,.1)
Y = np.arange(-2,2,.1)
x,y = np.meshgrid(X,Y)

m1 =1
m2 =2
x1 =4/3
x2 =2/3
omega = 3/8

u = -(m1/(abs(x-x1))**3)*(x-x1)-(m2/(abs(x-x2))**3)*(x-x2)+ x*omega
v = -(m1/(abs(y))**3)*(y)-(m2/(abs(y))**3)*(y)+ y*omega

fig, ax = plt.subplots()
ax.quiver(x,y,u,v)
plt.show()
2 Answers

A nice way, I find, to have a look at your data is to normalise the vector field and colour it by intensity. You can always mask glyphs for which the intensity is too low by using a Numpy MaskedArray. Have a look below.

import matplotlib.colors as cl
import matplotlib.pyplot as plt
import matplotlib.ticker as tck
import numpy as np

x, y = np.meshgrid(np.linspace(-2, 2, 41), np.linspace(-2, 2, 41))

m1, m2, x1, x2, omega = (1, 2, 4 / 3, 2 / 3, 3 / 8)

u = -(m1 / abs(x - x1) ** 3 * (x - x1) - m2 / abs(x - x2) ** 3 * (x - x2)
      + x * omega)
v = y * (omega - (m1 + m2) / abs(y) ** 3)

fig, (ax, bx) = plt.subplots(ncols=2, figsize=(20, 10))

ax.quiver(x, y, u, v, antialiased=True, scale=1e4, width=6e-3, headwidth=3,
          headlength=4, headaxislength=3.5, pivot='tail',
          edgecolors='xkcd:white', linewidths=1)
ax.set_aspect('equal')

w = np.sqrt(u ** 2 + v ** 2)
quiv = bx.quiver(x, y, u / w, v / w, w, antialiased=True, scale=3e1,
                 width=6e-3, headwidth=3, headlength=4, headaxislength=3.5,
                 pivot='tail', edgecolors='xkcd:white', linewidths=1,
                 norm=cl.LogNorm(vmin=1e-1, vmax=1e3))
bx.set_aspect('equal')
fig.colorbar(quiv, cax=fig.add_axes([0.93, 0.1, 0.02, 0.8]),
             extend='both', ticks=tck.LogLocator(),
             format=tck.LogFormatterSciNotation())

enter image description here

Some of the y values are close to 0 so that you get crazily large v values. I would check the equation because the plot is actually correct (the arrows are infinitely large when y ~= 0).

Related