0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

matplotlibで数学のグラフを描画してみた

Last updated at Posted at 2023-10-25

参考サイト
https://www.yutaka-note.com/entry/matplotlib_pyplot
https://tech-market.org/matplotlib-horizontal-vertical-line/
https://www.delftstack.com/ja/howto/matplotlib/how-to-plot-a-circle-in-matplotlib/
http://ailaby.com/contour/
https://collatz.hatenablog.com/entry/2021/02/09/105042

現在数学の勉強しているのでmatplotlibで数学のグラフを描画してみた

一次関数

mathGraph.py
# x範囲を指定
x = np.arange(-5.0, 5.0, 0.1)
 
# 方程式を入力
y = x
 
# プロット作成
plt.plot(x, y)

# x軸,y軸を表示
plt.axhline(y=0,color='k')
plt.axvline(x=0,color='k')

plt.show()

01.png

二次関数

mathGraph.py
import numpy as np
import matplotlib.pyplot as plt
 
# x範囲を指定
x = np.arange(-5.0, 5.0, 0.1)
 
# 方程式を入力
y =  x**2 - 5
 
# プロット作成
plt.plot(x, y)

# x軸,y軸を表示
plt.axhline(y=0,color='k')
plt.axvline(x=0,color='k')

plt.show()

02.png

三次関数

mathGraph.py
import numpy as np
import matplotlib.pyplot as plt
 
# x範囲を指定
x = np.arange(-2.0, 1.5, 0.1)
 
# 方程式を入力
y = x**3 + x**2 - x
 
# プロット作成
plt.plot(x, y)

# x軸,y軸を表示
plt.axhline(y=0,color='k')
plt.axvline(x=0,color='k')

plt.show()

03.png

四次関数

mathGraph.py
import numpy as np
import matplotlib.pyplot as plt
 
# x範囲を指定
x = np.arange(-5.0, 3.0, 0.1)
 
# 方程式を入力
y = x**4 + 5 * x**3 + 5 * x**2 + 5 * x 
 
# プロット作成
plt.plot(x, y)

# x軸,y軸を表示
plt.axhline(y=0,color='k')
plt.axvline(x=0,color='k')

plt.show()

04.png

mathGraph.py
import numpy as np
import matplotlib.pyplot as plt

# グラフ範囲を設定
a = np.linspace(-10, 10, 100)
b = np.linspace(-10, 10, 100)

x, y = np.meshgrid(a, b)

# x軸の中心(逆符号) + y軸の中心(逆符号) - 円の半径
C = (x - 5) ** 2 + (y + 5) ** 2 - 5 ** 2

figure, axes = plt.subplots()

axes.contour(x, y, C, [0])
# 縦横比1:1
axes.set_aspect(1)

# x軸,y軸を表示
plt.axhline(y=0, color='k')
plt.axvline(x=0, color='k')

plt.show()

circle2.png

直角三角形

45/45/90

mathGraph.py
import numpy as np
import matplotlib.pyplot as plt

# 三角形の頂点 (0,0), (1,1), (1,0)  # 45度/45度
p = np.matrix([[0, 1, 1, 0], [0, 1, 0, 0]])
p = np.array(p)  
plt.plot(p[0], p[1])
plt.axis('equal') 
plt.show()

45-45-90.png

30/60/90

mathGraph.py
import numpy as np
import matplotlib.pyplot as plt

# 三角形の頂点 (0,0), (sqrt(3),1), (sqrt(3),0)  # 30度/60度
p = np.matrix([[0, np.sqrt(3), np.sqrt(3), 0], [0, 1, 0, 0]])
p = np.array(p) 
plt.plot(p[0], p[1])
plt.axis('equal')
plt.show()

30-60-90.png

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?