Hướng dẫn can you plot a function in python? - bạn có thể vẽ một hàm trong python không?

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()
					
				
Hướng dẫn can you plot a function in python? - bạn có thể vẽ một hàm trong python không?

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ướng dẫn can you plot a function in python? - bạn có thể vẽ một hàm trong python không?

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()
					
				
Hướng dẫn can you plot a function in python? - bạn có thể vẽ một hàm trong python không?

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()
					
				
Hướng dẫn can you plot a function in python? - bạn có thể vẽ một hàm trong python không?

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ướng dẫn can you plot a function in python? - bạn có thể vẽ một hàm trong python không?

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()
					
				
Hướng dẫn can you plot a function in python? - bạn có thể vẽ một hàm trong python không?

Làm thế nào để bạn vẽ một chức năng?

Để vẽ một hàm, bắt đầu bằng cách cắm 0 cho X và sau đó giải phương trình để tìm y. Sau đó, đánh dấu vị trí đó trên trục y với một dấu chấm. Tiếp theo, tìm độ dốc của dòng, đó là số ngay trước biến.

Làm thế nào để bạn vẽ một hàm trong một biến trong Python?

Làm thế nào để vẽ một chức năng trong Python..
Trước hết, chúng tôi nhập chức năng pyplot từ mô -đun matplotlib ..
Sau đó, chúng tôi xác định các giá trị cho các biến X và Y, phụ thuộc vào loại hàm được vẽ ..
Sau đó, chúng tôi sử dụng PLT.Hàm âm mưu () để vẽ hình ..
Cuối cùng, chúng tôi sử dụng PLT ..

Là âm mưu có thể trong Python?

Matplotlib là thư viện âm mưu khoa học được sử dụng rộng rãi nhất trong Python.Vẽ dữ liệu trực tiếp từ một DataFrame của Pandas.Chọn và chuyển đổi dữ liệu, sau đó vẽ nó.Nhiều kiểu cốt truyện có sẵn: Xem bộ sưu tập đồ thị Python để biết thêm các tùy chọn.. 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.

Làm thế nào để bạn vẽ một chức năng trong gấu trúc?

Gandas - âm mưu..
Nhập pyplot từ matplotlib và trực quan hóa dataFrame của chúng tôi: nhập gấu trúc dưới dạng pd.Nhập matplotlib.pyplot như plt.....
Nhập GANDAS dưới dạng PD.Nhập matplotlib.pyplot như plt.df = pd.read_csv ('data.csv') ....
Một biểu đồ phân tán trong đó không có mối quan hệ giữa các cột: nhập pandas dưới dạng pd ..