1
2

More than 3 years have passed since last update.

Matplotlibで指数関数グラフ

Last updated at Posted at 2020-06-14

Pythonで指数関数グラフを描画してみた

 ■ a>1の指数関数(1)

スクリーンショット 2020-06-14 12.33.04.jpgの指数関数を表示するソースコードを作成してみました。
スクリーンショット 2020-06-14 12.25.55.jpg

NumpyとMatplotlibを使用してのサンプルを以下に掲載します。

  • サンプルPython1
ExponentialGraph.py
import numpy as np  # numpy の読み込み
import matplotlib.pyplot as plt # Matplotlib の読み込み

# x座標を-10 から 10 まで 0.1 きざみに設定
x = np.arange(-10, 10, 0.1)
# y座標を 指数関数(2の x 乗)として計算
y = 2 ** x

# y座標の範囲を -1 から 20 までに設定
plt.ylim([-1, 25])
# x軸のラベル表示
plt.xlabel('x')
# y軸のラベル表示
plt.ylabel('y', rotation=0)
# x軸とy軸のスケールを揃える
plt.gca().set_aspect('equal')
# グリッド(目盛軸)の表示
plt.grid()
# x, y をプロット
plt.plot(x, y)
# グラフ表示
plt.show()

 ■ a>1の指数関数(2)

スクリーンショット 2020-06-14 13.11.44.jpgの表示も同様に可能です。
スクリーンショット 2020-06-14 13.13.14.jpg

 ■ 0 < a < 1 の指数関数

次の関数もトライしてみたので掲載します。
スクリーンショット 2020-06-14 12.38.09.jpgのグラフ
スクリーンショット 2020-06-14 13.28.07.jpg

  • サンプルPython2
ExponentialGraph_05.py
import numpy as np  # numpy の読み込み
import matplotlib.pyplot as plt # Matplotlib の読み込み

# x座標を-10 から 10 まで 0.1 きざみに設定
x = np.arange(-10, 10, 0.1)
# y座標を 指数関数(0.5の x 乗)として計算
y = (1/2) ** x

# y座標の範囲を -1 から 20 までに設定
plt.ylim([-1, 25])
# x軸のラベル表示
plt.xlabel('x')
# y軸のラベル表示
plt.ylabel('y', rotation=0)
# x軸とy軸のスケールを揃える
plt.gca().set_aspect('equal')
# グリッド(目盛軸)の表示
plt.grid()
# x, y をプロット
plt.plot(x, y)
# グラフ表示
plt.show()

まとめ

MatplotlibとNumpyは数学計算でとても役立つことがわかりました。
いろいろ試してみたいと思います。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2