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

参考:SymPy Plotting Backends で初等関数のグラフを描く

SymPy + SymPy Plotting Backends 編

Python の SymPy + SymPy Plotting Backends (SPB) で初等関数のグラフを描く。関数は全て SymPy の関数を使う。

In [1]:
from sympy import *
# 1文字変数の Symbol の定義が省略できる
from sympy.abc import *
# SymPy Plotting Backends
from spb import *
# ticks の設定用
import matplotlib.ticker as tck
# π
from sympy import pi
Pi = float(pi)

import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [6, 4.5]

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

べき関数

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

特にオプションの設定が不要であれば,以下の1行で4本の曲線のグラフが描ける。

In [2]:
plot(x**(-2), x**(-1), x**2, x**3, (x, -5, 5), ylim=(-5, 5));

以下では「参考:初等関数のグラフを描く」シリーズの他のページの見てくれに合わせて,いくつかオプションを設定してグラフを描く例を紹介する。

In [3]:
p = plot(x**(-2), x**(-1), x**2, x**3, (x, -5, 5), 
         xlim = (-5, 5), ylim=(-5, 5), show = False)

ax = p.ax
# x の目盛を 1 ごとに
ax.xaxis.set_major_locator(tck.MultipleLocator(1))
# y の目盛を 1 ごとに
ax.yaxis.set_major_locator(tck.MultipleLocator(1))
# グリッドを細めの灰色点線で
ax.grid(which="major", color="lightgray", dashes=(3, 5), linewidth=0.5)
# x軸 y軸
ax.axhline(0, color='black', dashes=(3, 5), linewidth=0.5)
ax.axvline(0, color='black', dashes=(3, 5), linewidth=0.5);

$\displaystyle y = \sqrt{x}, \ \frac{1}{\sqrt{x}}$ のグラフ例。

In [4]:
# x = 0 は除く
p = plot(sqrt(x), 1/sqrt(x), (x, 0.001, 5), 
         xlim = (0, 5), ylim=(0, 5), show = False)

ax = p.ax
# グリッドを細めの灰色点線で
ax.grid(which="major", color="lightgray", dashes=(3, 5), linewidth=0.5);

指数関数

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

In [5]:
p = plot(exp(-x), exp(x), (x, -5, 5), 
         xlim = (-5, 5), ylim=(-0.1, 5), show = False)

ax = p.ax
# x の目盛を 1 ごとに
ax.xaxis.set_major_locator(tck.MultipleLocator(1))
# グリッドを細めの灰色点線で
ax.grid(which="major", color="lightgray", dashes=(3, 5), linewidth=0.5)
# x軸 y軸
ax.axhline(0, color='black', dashes=(3, 5), linewidth=0.5)
ax.axvline(0, color='black', dashes=(3, 5), linewidth=0.5);

三角関数

$ y = \sin x, \ \cos x, \ \tan x $ のグラフ例。$\tan x$ の不連続点の処理と,$x$ 軸の目盛 (ticks) を $\pi/2$ ごとにしてラベルをつける例。

In [6]:
# tan(x) の発散対策で detect_poles=True 必要なら adaptive_goal=0.001 も
p = plot((sin(x), "sin $x$"), (cos(x), "cos $x$"), (tan(x), "tan $x$"), 
         (x, -2*pi, 2*pi+0.8), detect_poles = True, 
         xlim = (-2*pi, 2*pi+0.8), ylim=(-3, 3), show = False)

ax = p.ax
# x の目盛を π/2 ごとに
ax.set_xticks([-2.0*Pi, -1.5*Pi, -1.0*Pi, -0.5*Pi, 0, 
                0.5*Pi,  1.0*Pi,  1.5*Pi, 2.0*Pi])
ax.set_xticklabels(["-2.0$\pi$", "-1.5$\pi$", "-1.0$\pi$", "-0.5$\pi$", "0",
                    " 0.5$\pi$", " 1.0$\pi$", " 1.5$\pi$", " 2.0$\pi$"])

# グリッドを細めの灰色点線で
ax.grid(which="major", color="lightgray", dashes=(3, 5), linewidth=0.5)
# x軸 y軸
ax.axhline(0, color='black', dashes=(3, 5), linewidth=0.5)
ax.axvline(0, color='black', dashes=(3, 5), linewidth=0.5);

$x$ 軸の目盛 (ticks) を $\pi/2$ ごとにしてラベルをつけるもう一つの例。

上記の例のように xticklabels を手で書き連ねるのは面倒という場合に,Formatter を使って目盛ラベルを設定する例。SPB の plot() のオプションである tx を使う。

In [7]:
# πで割った値をかえす
def divbypi(x):
    return x/Pi
In [8]:
# tx, ty は x or y の値に作用させる数値関数を指定できる
p = plot((sin(x), "sin $x$"), (cos(x), "cos $x$"), (tan(x), "tan $x$"), 
         (x, -2*pi, (2+0.3)*pi), detect_poles=True,
         tx = divbypi, 
         # xlim は divbypi を考慮して
         xlim = (-2, 2+0.3), ylim=(-3, 3), show = False)

ax = p.ax
# x の目盛を π/2 ごとに
ax.yaxis.set_major_locator(tck.MultipleLocator(0.5))
ax.xaxis.set_major_formatter(tck.FormatStrFormatter('%g $\pi$'))
# y の目盛を 1 ごとに
ax.yaxis.set_major_locator(tck.MultipleLocator(1))

# グリッドを細めの灰色点線で
ax.grid(which="major", color="lightgray", dashes=(3, 5), linewidth=0.5)
# x軸 y軸
ax.axhline(0, color='black', dashes=(3, 5), linewidth=0.5)
ax.axvline(0, color='black', dashes=(3, 5), linewidth=0.5);

逆三角関数

$y = \sin^{-1} x = \arcsin x =$ asin(x) の定義域は $-1 \leq x \leq 1$
$y = \cos^{-1} x = \arccos x =$ acos(x) の定義域は $-1 \leq x \leq 1$
$y = \tan^{-1} x = \arctan x =$ atan(x) の定義域は $-\infty < x < \infty$

$y$ 軸がラジアンで表されるので,$\pi/4$ ごとに目盛をつけてみる例。SPB の plot() のオプション ty を使う。

In [9]:
# tx, ty は x or y の値に作用させる数値関数を指定できる
p = plot((asin(x), "arcsin $x$"), (acos(x), "arccos $x$"), (x, -1, 1), 
         ty = divbypi,
         # ylim は divbypi を考慮して
         xlim = (-1, 1), ylim=(-0.5, 1), show = False)

ax = p.ax
# x の目盛を 0.5 ごとに
ax.xaxis.set_major_locator(tck.MultipleLocator(0.5))
# y の目盛を π/4 ごとに
ax.yaxis.set_major_locator(tck.MultipleLocator(1/4))
ax.yaxis.set_major_formatter(tck.FormatStrFormatter('%.2f $\pi$'))

# グリッドを細めの灰色点線で
ax.grid(which="major", color="lightgray", dashes=(3, 5), linewidth=0.5)
# x軸 y軸
ax.axhline(0, color='black', dashes=(3, 5), linewidth=0.5)
ax.axvline(0, color='black', dashes=(3, 5), linewidth=0.5);

In [10]:
# tx, ty は x or y の値に作用させる数値関数を指定できる
p = plot(atan(x), "arctan $x$", (x, -10, 10), 
         ty = divbypi, legend=True,
         # ylim は divbypi を考慮して
         xlim = (-10, 10), ylim=(-0.5, 0.5), show = False)

ax = p.ax
# x の目盛を 2 ごとに
ax.xaxis.set_major_locator(tck.MultipleLocator(2))
# y の目盛を π/4 ごとに
ax.yaxis.set_major_locator(tck.MultipleLocator(1/4))
ax.yaxis.set_major_formatter(tck.FormatStrFormatter('%.2f $\pi$'))

# グリッドを細めの灰色点線で
ax.grid(which="major", color="lightgray", dashes=(3, 5), linewidth=0.5)
# x軸 y軸
ax.axhline(0, color='black', dashes=(3, 5), linewidth=0.5)
ax.axvline(0, color='black', dashes=(3, 5), linewidth=0.5);

双曲線関数

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

In [11]:
p = plot((sinh(x), "sinh $x$"), (cosh(x), "cosh $x$"), (x, -4, 4), 
         xlim = (-4, 4), ylim=(-30, 30), show = False)

ax = p.ax
# x の目盛を 1 ごとに
ax.xaxis.set_major_locator(tck.MultipleLocator(1))
# y の目盛を 10 ごとに
ax.yaxis.set_major_locator(tck.MultipleLocator(10))

# グリッドを細めの灰色点線で
ax.grid(which="major", color="lightgray", dashes=(3, 5), linewidth=0.5)
# x軸 y軸
ax.axhline(0, color='black', dashes=(3, 5), linewidth=0.5)
ax.axvline(0, color='black', dashes=(3, 5), linewidth=0.5);

In [12]:
p = plot((tanh(x), "tanh $x$"), (x, -5, 5), legend = True,
         xlim = (-5, 5), ylim=(-1, 1), show = False)

ax = p.ax
# x の目盛を 1 ごとに
ax.xaxis.set_major_locator(tck.MultipleLocator(1))
# y の目盛を 0.2 ごとに
ax.yaxis.set_major_locator(tck.MultipleLocator(0.2))

# グリッドを細めの灰色点線で
ax.grid(which="major", color="lightgray", dashes=(3, 5), linewidth=0.5)
# x軸 y軸
ax.axhline(0, color='black', dashes=(3, 5), linewidth=0.5)
ax.axvline(0, color='black', dashes=(3, 5), linewidth=0.5);

逆双曲線関数

$ y = \sinh^{-1} x = \mbox{arsinh}\ x = $ asinh(x)
$ y = \cosh^{-1} x = \mbox{arcosh}\ x = $ acosh(x)
$ y = \tanh^{-1} x = \mbox{artanh}\ x = $ atanh(x)

In [13]:
p = plot(asinh(x), "sinh$^{-1}\, x$", (x, -5, 5), legend = True,
         xlim = (-5, 5), ylim=(-3, 3), show = False)

ax = p.ax
ax.xaxis.set_major_locator(tck.MultipleLocator(1))
ax.yaxis.set_major_locator(tck.MultipleLocator(1))

# グリッドを細めの灰色点線で
ax.grid(which="major", color="lightgray", dashes=(3, 5), linewidth=0.5)
# x軸 y軸
ax.axhline(0, color='black', dashes=(3, 5), linewidth=0.5)
ax.axvline(0, color='black', dashes=(3, 5), linewidth=0.5);

In [14]:
p = plot(acosh(x), "cosh$^{-1}\, x$", (x, 1, 5), legend = True,
         xlim = (1, 5), ylim=(0, 3), show = False)

ax = p.ax
# グリッドを細めの灰色点線で
ax.grid(which="major", color="lightgray", dashes=(3, 5), linewidth=0.5);

In [15]:
# x = 1 は除く
p = plot(atanh(x), "tanh$^{-1}\, x$", (x, 0, 0.999), legend = True,
         xlim = (0, 1), ylim=(0, 3), show = False)

ax = p.ax
# グリッドを細めの灰色点線で
ax.grid(which="major", color="lightgray", dashes=(3, 5), linewidth=0.5);