Matplotlib: sơ đồ một hàm y = f [x]
Trong hướng dẫn trước đây của chúng tôi, chúng tôi đã học cách vẽ một đường thẳng hoặc phương trình tuyến tính loại $ y = mx+c $.
Ở đây, chúng ta sẽ học cách vẽ một hàm được xác định $ y = f [x] $ trong python, trong một khoảng thời gian xác định.
Chúng tôi bắt đầu bằng cách vẽ phương trình bậc hai đơn giản nhất $ y = x^{2} $.
Phương trình bậc hai
Phương trình bậc hai là phương trình đa thức bậc hai của loại $ ax^{2} + bx + c = 0 $, trong đó $ x $ là một biến và $ a \ ne 0 $. Vẽ một hàm bậc hai gần giống như vẽ đường thẳng trong hướng dẫn trước.
Dưới đây là mã matplotlib để vẽ hàm $ y = x^{2} $. Nó là một mã đơn giản đơn giản; Phần lớn của nó ở giữa là để thiết lập các trục. Vì số mũ của $ x $ là $ 2 $, sẽ chỉ có các giá trị dương là $ y $, vì vậy chúng ta có thể định vị ax.spines['bottom']
ở phía dưới.
import matplotlib.pyplot as plt
import numpy as np
# 100 linearly spaced numbers
x = np.linspace[-5,5,100]
# the function, which is y = x^2 here
y = x**2
# setting the axes at the centre
fig = plt.figure[]
ax = fig.add_subplot[1, 1, 1]
ax.spines['left'].set_position['center']
ax.spines['bottom'].set_position['zero']
ax.spines['right'].set_color['none']
ax.spines['top'].set_color['none']
ax.xaxis.set_ticks_position['bottom']
ax.yaxis.set_ticks_position['left']
# plot the function
plt.plot[x,y, 'r']
# show the plot
plt.show[]
Phương trình khối
Tiếp theo, chúng ta sẽ vẽ đồ thị hàm khối đơn giản nhất $ y = x^{3} $.
Vì số mũ trong $ y = x^{3} $ là $ 3 $, nên sức mạnh bị ràng buộc có các giá trị âm cho các giá trị âm là $ x $. Do đó, đối với khả năng hiển thị của các giá trị âm trong $ y $ -axis, chúng ta cần di chuyển $ x $ -axis đến trung tâm của biểu đồ. ax.spines['bottom']
do đó được định vị vào trung tâm.
import matplotlib.pyplot as plt
import numpy as np
# 100 linearly spaced numbers
x = np.linspace[-5,5,100]
# the function, which is y = x^3 here
y = x**3
# setting the axes at the centre
fig = plt.figure[]
ax = fig.add_subplot[1, 1, 1]
ax.spines['left'].set_position['center']
ax.spines['bottom'].set_position['center']
ax.spines['right'].set_color['none']
ax.spines['top'].set_color['none']
ax.xaxis.set_ticks_position['bottom']
ax.yaxis.set_ticks_position['left']
# plot the function
plt.plot[x,y, 'g']
# show the plot
plt.show[]
Hàm lượng giác
Ở đây chúng tôi vẽ hàm hàm lượng giác $ y = \ text {sin} [x] $ cho các giá trị của $ x $ giữa $-\ pi $ và $ \ pi $. Phương pháp linspace[]
có khoảng thời gian được đặt từ $-\ pi $ đến $ \ pi $.
import matplotlib.pyplot as plt
import numpy as np
# 100 linearly spaced numbers
x = np.linspace[-np.pi,np.pi,100]
# the function, which is y = sin[x] here
y = np.sin[x]
# setting the axes at the centre
fig = plt.figure[]
ax = fig.add_subplot[1, 1, 1]
ax.spines['left'].set_position['center']
ax.spines['bottom'].set_position['center']
ax.spines['right'].set_color['none']
ax.spines['top'].set_color['none']
ax.xaxis.set_ticks_position['bottom']
ax.yaxis.set_ticks_position['left']
# plot the function
plt.plot[x,y, 'b']
# show the plot
plt.show[]
Chúng ta hãy vẽ nó cùng với hai hàm nữa, $ y = 2 \ text {sin} [x] $ và $ y = 3 \ text {sin} [x] $. Lần này, chúng tôi dán nhãn các chức năng.
import matplotlib.pyplot as plt
import numpy as np
# 100 linearly spaced numbers
x = np.linspace[-np.pi,np.pi,100]
# the function, which is y = sin[x] here
y = np.sin[x]
# setting the axes at the centre
fig = plt.figure[]
ax = fig.add_subplot[1, 1, 1]
ax.spines['left'].set_position['center']
ax.spines['bottom'].set_position['center']
ax.spines['right'].set_color['none']
ax.spines['top'].set_color['none']
ax.xaxis.set_ticks_position['bottom']
ax.yaxis.set_ticks_position['left']
# plot the functions
plt.plot[x,y, 'b', label='y=sin[x]']
plt.plot[x,2*y, 'c', label='y=2sin[x]']
plt.plot[x,3*y, 'r', label='y=3sin[x]']
plt.legend[loc='upper left']
# show the plot
plt.show[]
Và ở đây chúng tôi vẽ cùng nhau cả $ y = \ text {sin} [x] $ và $ y = \ text {cos} [x] $ trong cùng một khoảng $-\ pi $ đến $ \ pi $.
import matplotlib.pyplot as plt
import numpy as np
# 100 linearly spaced numbers
x = np.linspace[-np.pi,np.pi,100]
# the functions, which are y = sin[x] and z = cos[x] here
y = np.sin[x]
z = np.cos[x]
# setting the axes at the centre
fig = plt.figure[]
ax = fig.add_subplot[1, 1, 1]
ax.spines['left'].set_position['center']
ax.spines['bottom'].set_position['center']
ax.spines['right'].set_color['none']
ax.spines['top'].set_color['none']
ax.xaxis.set_ticks_position['bottom']
ax.yaxis.set_ticks_position['left']
# plot the functions
plt.plot[x,y, 'c', label='y=sin[x]']
plt.plot[x,z, 'm', label='y=cos[x]']
plt.legend[loc='upper left']
# show the plot
plt.show[]
Hàm số mũ
Hàm số mũ $ y = e^{x} $ sẽ không bao giờ có bất kỳ giá trị âm nào cho bất kỳ giá trị nào là $ x $. Vì vậy, chúng tôi di chuyển $ x $ -axis xuống đáy một lần nữa bằng cách đặt ax.spines['bottom']
thành
import matplotlib.pyplot as plt
import numpy as np
# 100 linearly spaced numbers
x = np.linspace[-5,5,100]
# the function, which is y = x^3 here
y = x**3
# setting the axes at the centre
fig = plt.figure[]
ax = fig.add_subplot[1, 1, 1]
ax.spines['left'].set_position['center']
ax.spines['bottom'].set_position['center']
ax.spines['right'].set_color['none']
ax.spines['top'].set_color['none']
ax.xaxis.set_ticks_position['bottom']
ax.yaxis.set_ticks_position['left']
# plot the function
plt.plot[x,y, 'g']
# show the plot
plt.show[]
0. Chúng tôi vẽ nó trong khoảng thời gian $ -2 $ đến $ 2 $.
import matplotlib.pyplot as plt
import numpy as np
# 100 linearly spaced numbers
x = np.linspace[-2,2,100]
# the function, which is y = e^x here
y = np.exp[x]
# setting the axes at the centre
fig = plt.figure[]
ax = fig.add_subplot[1, 1, 1]
ax.spines['left'].set_position['center']
ax.spines['bottom'].set_position['zero']
ax.spines['right'].set_color['none']
ax.spines['top'].set_color['none']
ax.xaxis.set_ticks_position['bottom']
ax.yaxis.set_ticks_position['left']
# plot the function
plt.plot[x,y, 'y', label='y=e^x']
plt.legend[loc='upper left']
# show the plot
plt.show[]