Created
January 10, 2014 09:56
-
-
Save fsodogandji/8349338 to your computer and use it in GitHub Desktop.
Visualize vectors field with matplotlib & Latex legends.
Taken from http://numbercrunch.de/blog/2013/05/visualizing-vector-fields/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # import useful modules | |
| import matplotlib | |
| from numpy import * | |
| from pylab import * | |
| # use LaTeX, choose nice some looking fonts and tweak some settings | |
| matplotlib.rc('font', family='serif') | |
| matplotlib.rc('font', size=16) | |
| matplotlib.rc('legend', fontsize=16) | |
| matplotlib.rc('legend', numpoints=1) | |
| matplotlib.rc('legend', handlelength=1.5) | |
| matplotlib.rc('legend', frameon=False) | |
| matplotlib.rc('xtick.major', pad=7) | |
| matplotlib.rc('xtick.minor', pad=7) | |
| matplotlib.rc('text', usetex=True) | |
| matplotlib.rc('text.latex', | |
| preamble=[r'\usepackage[T1]{fontenc}', | |
| r'\usepackage{amsmath}', | |
| r'\usepackage{txfonts}', | |
| r'\usepackage{textcomp}']) | |
| close('all') | |
| figure(figsize=(6, 4.5)) | |
| # generate grid | |
| x=linspace(-2, 2, 32) | |
| y=linspace(-1.5, 1.5, 24) | |
| x, y=meshgrid(x, y) | |
| # calculate vector field | |
| vx=-y/sqrt(x**2+y**2)*exp(-(x**2+y**2)) | |
| vy= x/sqrt(x**2+y**2)*exp(-(x**2+y**2)) | |
| # plot vecor field | |
| quiver(x, y, vx, vy, pivot='middle', headwidth=4, headlength=6) | |
| xlabel('$x$') | |
| ylabel('$y$') | |
| axis('image') | |
| #show() | |
| savefig('visualization_vector_fields_1.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment