import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
set(style="ticks", font_scale=1.5) sns.
Streamplot
Streamplot of a two-dimensional linear system
Introduction
Streamplot of a two-dimensional linear system, with eigenvectors and nullclines. Python shows LaTeX equations beautifully.
Main features: meshgrid, streamplot, contour, legend, LaTeX
The code
make graph look pretty
# learn how to configure:
# http://matplotlib.sourceforge.net/users/customizing.html
= {
params 'font.family': 'serif',
'ps.usedistiller': 'xpdf',
'text.usetex': True,
# include here any neede package for latex
'text.latex.preamble': r'\usepackage{amsmath}',
} plt.rcParams.update(params)
define parameters, system of equations, and equation for eigenvectors
%matplotlib widget
= plt.subplots(figsize=(8,6))
fig, ax =0.08, right=0.68, top=0.87, bottom=0.10,
fig.subplots_adjust(left=0.02, wspace=0.02)
hspace# parameters as a dictionary
= {'a': -1.0, 'b': +0.2,
p 'c': +1.2, 'd': -1.5}
# the equations
def system_equations(x,y):
return [p['a'] * x + p['b'] * y,
'c'] * x + p['d'] * y,
p[
]# eigenvectors
= 100 * np.array([
eigen_vec 'a'] - p['d'] -
[(p['a'] - p['d']) ** 2 +
np.sqrt((p[4.0 * p['b'] * p['c'])) /
2.0 * p['c']), 1.0],
('a'] - p['d'] +
[(p['a'] - p['d']) ** 2 +
np.sqrt((p[4.0 * p['b'] * p['c'])) /
2.0 * p['c']), 1.0],
(
])
= [-1, 1]
min_x, max_x = [-4, 4]
min_y, max_y = 50j
divJ = 50
div # 1st way
# Y, X = np.mgrid[min_y:max_y:div,min_x:max_x:div]
# 2nd way
= np.meshgrid(np.linspace(min_x, max_x, div),
X, Y
np.linspace(min_y, max_y, div))
# streamplot
= 2 * [0.80]
density = 0.2
minlength = 3 * [0.5]
arrow_color 0], system_equations(X, Y)[1],
ax.streamplot(X, Y, system_equations(X, Y)[=density, color=arrow_color, arrowsize=2,
density=2, minlength=minlength)
linewidth
# eigenvectors
= ax.plot([eigen_vec[0, 0],-eigen_vec[0, 0]],
eigen_0, 0, 1],-eigen_vec[0, 1]],
[eigen_vec[='red', lw=3, ls="--")
color= ax.plot([eigen_vec[1, 0],-eigen_vec[1, 0]],
eigen_1, 1, 1],-eigen_vec[1, 1]],
[eigen_vec[='orange', lw=3, ls="--")
color= (7, 2, 1, 2)
dash
eigen_0.set_dashes(dash)
eigen_1.set_dashes(dash)
# nullclines
= ax.contour(X, Y, system_equations(X, Y)[0],
null_0 =[0], colors='black', linewidths=3)
levels= ax.contour(X, Y,system_equations(X, Y)[1],
null_1 =[0], colors='blue', linewidths=3)
levels= ax.plot([100,101], [100,101], color='black', linewidth=3)
n0, = ax.plot([100,101], [100,101], color='blue', linewidth=3)
n1,
# some text
0.0, 1.02, (r"$\displaystyle\frac{d}{dt}\begin{pmatrix}x\\y\end{pmatrix}=$"
ax.text(r"$\begin{pmatrix}a&b\\c&d\end{pmatrix}\cdot$"
r"$\begin{pmatrix}x\\y\end{pmatrix}$"),
=ax.transAxes, va="bottom")
transform1.0, 1.1, r"$a={:.1f}\qquad b={:.1f}$".format(p['a'], p['b']),
ax.text(=ax.transAxes, ha="right")
transform1.0, 1.03, r"$c={:.1f}\qquad d={:.1f}$".format(p['c'], p['d']),
ax.text(=ax.transAxes, ha="right")
transform
ax.legend([n0, n1, eigen_0, eigen_1],r'$dx/dt=0$', r'$dy/dt=0$',
[r"eigenvector 1", r"eigenvector 2"],
="center left",
loc=(1.0,0.5),
bbox_to_anchor=False, fancybox=False, shadow=False, ncol=1,
frameon=0.5, labelspacing=0.5, handlelength=3, handletextpad=1.1,
borderpad=0.3, columnspacing=2)
borderaxespad
r"$y$", rotation='horizontal')
ax.set_ylabel(r"$x$", labelpad=5)
ax.set_xlabel(
ax.axis([min_x, max_x, min_y, max_y])
"streamplot.png", dpi=300) fig.savefig(