Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting vector fields in python (matplotlib)

I found this code on http://matplotlib.sourceforge.net/examples/pylab_examples/quiver_demo.html

from pylab import *
from numpy import ma

X,Y = meshgrid( arange(0,2*pi,.2),arange(0,2*pi,.2) )
U = cos(X)
V = sin(Y)

#1
figure()
Q = quiver( U, V)
qk = quiverkey(Q, 0.5, 0.92, 2, r'$2 \frac{m}{s}$', labelpos='W',
               fontproperties={'weight': 'bold'})
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])

title('Minimal arguments, no kwargs')

Now, how can I see this graphic? Better yet, how can I save it to a file as, let's, say, JPEG? The code seems to run but I can't see anything happening.

like image 570
devoured elysium Avatar asked Dec 03 '09 21:12

devoured elysium


People also ask

How do you plot a vector field in Python?

In this article, we are going to discuss how to plot a vector field in python. In order to perform this task we are going to use the quiver() method and the streamplot() method in matplotlib module. Where X, Y define the Vector location and U, V are directional arrows with respect of the Vector location.

Which function will you use to plot vectors of a field PLT vector ()?

Matplotlib provides a function, streamplot , to create a plot of streamlines representing a vector field.


3 Answers

Ideally, you would type this in an interactive shell ( like EPD Python's PyLab ) . Otherwise, you need to explicitly invoke the show() command

like image 182
doffles Avatar answered Oct 09 '22 17:10

doffles


Stick show() at the end of the script. Or to save it to a jpg file, put

savefig('output.jpg')
show()

Be sure to put the savefig() command before the show().

like image 5
unutbu Avatar answered Oct 09 '22 17:10

unutbu


You need to call show() or savefig().

like image 4
codelogic Avatar answered Oct 09 '22 16:10

codelogic