Return to 参考:初等関数のグラフを描く

参考:SymPy Plotting Backends で初等関数のグラフを描く(The Graphic Module 編)

SymPy および SPB の import

In [1]:
from sympy.abc import *
from sympy import *

# SymPy Plotting Backends (SPB)
from spb import *

# 日本語がトーフになる場合
# import japanize_matplotlib

# グラフを SVG で Notebook にインライン表示
%config InlineBackend.figure_formats = ['svg']

# デフォルト設定のため
import matplotlib.pyplot as plt
# mathtext font の設定
plt.rcParams['mathtext.fontset'] = 'cm'

べき関数

まず,$y = x^{-1}$ のグラフ。

In [2]:
graphics(line(1/x), xlim = (-5, 5), ylim = (-10, 10));

$y=x^{-1}$ のグラフの $x=0$ 付近,$y \rightarrow -\infty$ と $y \rightarrow +\infty$ を縦の直線でつないでしまう。

不連続点をつなげない対策

detect_poles=True オプションをつけて,さらに n の値を大きめにすると,以下のようになる。

In [3]:
graphics(
    line(1/x, detect_poles=True, n = 1e4), 
    xlim = (-5, 5), ylim = (-10, 10)
);

$y = x^{-2}, \ x^{-1}, \ x^2, \ x^3$ のグラフ例。(凡例 label に $\LaTeX$ 記法を使っています。)

In [4]:
graphics(
    line(1/x**2, label = '$x^{-2}$'), 
    line(1/x, label = '$x^{-1}$', detect_poles = True, n = 1e4), 
    line(x**2),
    line(x**3), 
    xlim = (-5, 5), ylim = (-10, 10), 
    title = 'べき関数のグラフ'
);

$\displaystyle y = \sqrt{x}, \ \frac{1}{\sqrt{x}}$ のグラフ例。
\ で始まる $\LaTeX$ コマンドを使う際は,念のため r から始めます。)

In [5]:
graphics(
    line(sqrt(x), (x, 0, 5), r'$\sqrt{x}$'),
    line(1/sqrt(x), (x, 0.0001, 5), r'$1/\sqrt{x}$'),
    xlim = (0, 5), ylim = (0, 5)
);

指数関数

$y = e^{-x}, \ e^x$ のグラフ例。

In [6]:
graphics(
    line(exp(-x), (x, -5, 5), '$e^{-x}$'),
    line(exp(x), (x, -5, 5), '$e^{x}$'),
    xlim = (-5, 5), ylim = (0, 5)
);

対数関数

$y = \log x$ のグラフ例。

In [7]:
graphics(
    line(log(x), (x, 0.0001, 10), r'$\log x$'),
    xlim = (-0.5, 10), ylim = (-5, 5), legend = True
);

三角関数

$ y = \sin x, \ \cos x, \ \tan x $ のグラフ例。

不連続点をつなげない対策

$\tan x$ の不連続点の処理は detect_poles = True で。

主目盛・副目盛のカスタマイズ

グラフのスタイルの詳細な設定変更は SPB には備わっていないようですので,バックエンドの Matplotlib の ax で詳細設定を行ってみます。

ここでは,横軸主目盛を $\pi$ ごと,横軸副目盛を $\frac{\pi}{2}$ ごと,縦軸副目盛を $1$ ごとにしてみます。

参考:グリッドのカスタマイズ

上の出力結果をみると,主目盛と副目盛ではグリッドのスタイルが異なっているようです。このへんのカスタマイズは

  • ax.grid()

参考:$x$ 軸・$y$ 軸の表示

また,Python のグラフ作成界隈では,$x$ 軸($y = 0$)や $y$ 軸($x = 0$)を(格子線とは区別して)表示させる習慣はないようです。このあたりは以下のようにして対応できます。

  • $x$ 軸($y = 0$)の表示 ax.axhline(0)
  • $y$ 軸($x = 0$)の表示 ax.axvline(0)
In [8]:
# おまじない。これで ax が使える。
fig, ax = plt.subplots()

# 横軸主目盛
Pi = float(pi)
ax.set_xticks(
    [-2*Pi,     -Pi,      0,     Pi,      2*Pi], 
    ['$-2\pi$', '$-\pi$', '$0$', '$\pi$', '$2\pi$'])

from matplotlib.ticker import MultipleLocator
# 横軸副目盛を π/4 ごとに
ax.xaxis.set_minor_locator(MultipleLocator(Pi/2))
# 縦軸副目盛を 1 ごとに
ax.yaxis.set_minor_locator(MultipleLocator(1))

# x軸 y軸は dashed に
ax.axhline(0, c='k', ls='--', lw=0.5)
ax.axvline(0, c='k', ls='--', lw=0.5)

# グリッドは主目盛副目盛とも同じスタイルで目立たなく
ax.grid(c='lightgray', ls='--', lw=0.5, which='both')

graphics(
    line(sin(x), label = '$\sin x$'),
    line(cos(x), label = '$\cos x$'),
    line(tan(x), label = r'$\tan x$', detect_poles = True),
    xlim = (-2*pi, 2*pi+1), ylim = (-5, 5),
    grid=False, # 一旦 False にして...
    ax = ax     # ax の設定を反映
);

逆三角関数

  • $y = \sin^{-1} x = \arcsin x =$ asin(x)
    • 定義域は $\displaystyle-1 \leq x \leq 1$
    • 値域は $\displaystyle -\frac{\pi}{2} \leq y \leq \frac{\pi}{2}$
  • $y = \cos^{-1} x = \arccos x =$ acos(x)
    • 定義域は $\displaystyle-1 \leq x \leq 1$
    • 値域は $\displaystyle0 \leq y \leq \pi$
  • $y = \tan^{-1} x = \arctan x =$ atan(x)
    • 定義域は $\displaystyle-\infty < x < \infty$
    • 値域は $\displaystyle-\frac{\pi}{2} \leq y \leq \frac{\pi}{2}$
In [9]:
fig, ax = plt.subplots()

# 縦軸主目盛
Pi = float(pi)
ax.set_yticks(
    [-Pi/2,      0,     Pi/2,      Pi], 
    ['$-\pi/2$', '$0$', '$\pi/2$', '$\pi$'])
# 縦軸副目盛
ax.yaxis.set_minor_locator(MultipleLocator(Pi/4))

# 横軸主目盛
ax.xaxis.set_major_locator(MultipleLocator(5))
# 横軸副目盛
ax.xaxis.set_minor_locator(MultipleLocator(1))

# x軸 y軸は dashed に
ax.axhline(0, c='k', ls='--', lw=0.5)
ax.axvline(0, c='k', ls='--', lw=0.5)

# グリッドは主目盛副目盛とも同じスタイルで目立たなく
ax.grid(c='lightgray', ls='--', lw=0.5, which='both')

graphics(
    line(asin(x), (x, -1, 1), r'$\arcsin x$'), 
    line(acos(x), (x, -1, 1), r'$\arccos x$'), 
    line(atan(x), (x, -10, 10), r'$\arctan x$'), 
    xlim = (-7, 7), ylim = (-pi/2, pi), 
    grid=False, # 一旦 False にして...
    ax = ax     # ax の設定を反映
);

双曲線関数

$y = \sinh x, \ \cosh x, \ \tanh x$ のグラフ例。

In [10]:
fig, ax = plt.subplots()

# 縦軸主目盛
ax.yaxis.set_major_locator(MultipleLocator(2))
# 縦軸副目盛
ax.yaxis.set_minor_locator(MultipleLocator(1))

# 横軸主目盛
ax.xaxis.set_major_locator(MultipleLocator(2))
# 横軸副目盛
ax.xaxis.set_minor_locator(MultipleLocator(1))

# x軸 y軸は dashed に
ax.axhline(0, c='k', ls='--', lw=0.5)
ax.axvline(0, c='k', ls='--', lw=0.5)

# グリッドは主目盛副目盛とも同じスタイルで目立たなく
ax.grid(c='lightgray', ls='--', lw=0.5, which='both')

graphics(
    line(sinh(x), (x, -5, 5), r'$\sinh x$'), 
    line(cosh(x), (x, -5, 5), r'$\cosh x$'),
    line(tanh(x), (x, -5, 5), r'$\tanh x$'), 
    xlim = (-5, 5), ylim = (-7, 7), 
    grid=False, # 一旦 False にして...
    ax = ax     # ax の設定を反映
);

逆双曲線関数

  • $ y = \sinh^{-1} x = \mbox{arsinh}\ x = $ asinh(x)
    • 定義域は $ -\infty < x < \infty$
    • 値域は $-\infty < y < \infty$
  • $ y = \cosh^{-1} x = \mbox{arcosh}\ x = $ acosh(x)
    • 定義域は $ 1 \leq x < \infty$
    • 値域は $0 \leq y < \infty$
  • $ y = \tanh^{-1} x = \mbox{artanh}\ x = $ atanh(x)
    • 定義域は $ -1 < x < 1$
    • 値域は $-\infty < y < \infty$
In [11]:
fig, ax = plt.subplots()

# 縦軸主目盛
ax.yaxis.set_major_locator(MultipleLocator(1))

# 横軸主目盛
ax.xaxis.set_major_locator(MultipleLocator(1))

# x軸 y軸は dashed に
ax.axhline(0, c='k', ls='--', lw=0.5)
ax.axvline(0, c='k', ls='--', lw=0.5)

# グリッドは主目盛副目盛とも同じスタイルで目立たなく
ax.grid(c='lightgray', ls='--', lw=0.5, which='both')

graphics(
    line(asinh(x), (x, -10, 10), r'$\sinh^{-1} x$'), 
    line(acosh(x), (x, 1, 10), r'$\cosh^{-1} x$'),
    line(atanh(x), (x, -0.99999, 0.99999), r'$\tanh^{-1} x$'), 
    xlim = (-7, 7), ylim = (-5, 5),
    grid=False, # 一旦 False にして...
    ax = ax     # ax の設定を反映
);