Ploting Bessel Function
Ploting Bessel Function
This simple example uses numpy, scipy and Matplotlib to produce a plot of the first six Bessel functions. Two plots are shown, one created
by Matplotlib and a second created by LaTeX using the plotting package pgfplots and the data exported from Matplotlib.
If you are using macOS, you may need to use the -Ppythonw option when running pylatex.sh. This is a known problem with macOS and
Matplotlib, see https://matplotlib.org/faq/osx_framework.html.
\begin{minipage}{\textwidth}
import numpy as np
\centering
import scipy.special as sp
\IfFileExists{example-04-fig.pdf}%
import matplotlib.pyplot as plt
{\includegraphics[width=6.4in]
{example-04-fig.pdf}}{Failed to create pdf plot.}
plt.matplotlib.rc(’text’, usetex = True)
\captionof{figure}{The first six Bessel functions.}
plt.matplotlib.rc(’grid’, linestyle = ’dotted’)
\end{minipage}
plt.matplotlib.rc(’figure’, figsize = (6.4,4.8)) # (width,height) inches
plt.xlim((0, 15))
plt.ylim((-0.5, 1.1))
plt.legend((’${J}_0(x)$’, ’${J}_1(x)$’, ’${J}_2(x)$’,
’${J}_3(x)$’, ’${J}_4(x)$’, ’${J}_5(x)$’), loc = 0)
plt.xlabel(’$x$’)
plt.ylabel(’${J}_n(x)$’)
plt.grid(True)
plt.tight_layout(0.5)
plt.savefig(’example-04-fig.pdf’)
0.4
Jn (x)
0.2
0.0
−0.2
−0.4
0 2 4 6 8 10 12 14
x
2
Using pgfplots
1 J0
J1
0.8
J2
J3
0.6
J4
0.4 J5
Jn (x)
0.2
−0.2
−0.4
0 2 4 6 8 10 12 14
x