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 $.
Nội dung chính ShowShow
- Matplotlib: sơ đồ một hàm y = f [x]
- Phương trình bậc hai
- Phương trình khối
- Hàm lượng giác
- Hàm số mũ
- Làm thế nào để bạn vẽ một chức năng?
- Làm thế nào để bạn vẽ một hàm trong một biến trong Python?
- Là âm mưu có thể trong Python?
- Làm thế nào để bạn vẽ một chức năng trong gấu trú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 khối
Hàm lượng giác
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
Hàm lượng giác
Hàm số 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
Hàm số mũ
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[]
Làm thế nào để bạn vẽ một 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[]
Làm thế nào để bạn vẽ một hàm trong một biến trong Python?
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ũ
Làm thế nào để bạn vẽ một chức năng?
Làm thế nào để bạn vẽ một hàm trong một biến trong Python?
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[]
Là âm mưu có thể trong Python?
Làm thế nào để bạn vẽ một chức năng trong gấu trúc?. Plot data directly from a Pandas dataframe. Select and transform data, then plot it. Many styles of plot are available: see the Python Graph Gallery for more options.