Project 2: Drawing graphs of mathematical functions

In Python, two modules, Numpy and Matplotlib, are generally used to draw function images, and these two modules need to be installed separately.

numpy module:

        It is a scientific computing package that includes many mathematical functions, such as trigonometric functions, matrix calculation methods, vector operations, linear algebra, etc. An arithmetic sequence can be created through the arange function in this module. If a value is taken every 0.01 between 0-2π, it can be represented by arange(0,2* numpy.pi,0.01), where numpy.pi represents π.

Commonly used functions in the numpy module:

The following code can generate several key points of sin(x):

import numpy as np  #加载numpy模块并取一个简洁的别名np,便于后续引用
x=np. arange(0,2*np.pi,0.01)  #x在0到2π之间,每隔0.01取一个点
y=np.sin(x)   #通过解析式计算列表x对应的列表y的值

matplotlib module:

        The matplotlib module is a plotting library. When calling matplotlib.pyplot, the coordinate system can be automatically generated based on the numerical range. The drawing principle of matplotlib is very simple. Using the plot line drawing function, you can easily connect the (x, y) coordinate point pairs into a smooth curve in the rectangular plane. For example: Add the following statements at appropriate locations in the above code to connect the key points just generated.

Commonly used functions in the matplotlib module:

import matplotlib.pyplot as plt  #加载matplotlib.pyplot并取名p1t
plt.plot(x,y)                    #将点对连线
plt.show()                       #将绘制的图像窗口显示出来

Use Python to draw a sinusoidal curve: import the numpy module, and use the arange function in this module to create an arithmetic sequence, which is to take the x value in the point drawing method, and then use y=np.sin(x) Calculate the value of y. Then call the matplotlib module to draw the function image. Call matplotlib.pyplot to easily connect (x, y) coordinate point pairs into smooth curves in the rectangular plane.
The procedure is as follows:

import numpy as np        #加载numpy模块并取一个简洁的别名为np,                                                 便于后续引用
import matplotlib.pyplot as plt   #加载matplotlib.pyplot并取别名为pltx=np.arange(0,2*np.pi,0.01)    #x在0到2π之间,每隔0.01取一个点
y=np.sin(x)                       #求sin(x)对应的y值
plt.plot(x,y)                     #绘制sin(x)图像,系统自动配置蓝色
plt.title('sin(x)')               #设置图像标题
plt.xlabel('X')                   #设置X轴标题
plt.ylabel('Y')                   #设置Y轴标题
plt.show()                        #将绘制的函数图像窗口显示出来

Draw the graph of sin(x), sin(-x), sin(2x)/2

import  numpy  as  np     #加载numpy模块并取别名为np
import matplotlib.pyplot as plt     #加载matplotlib.pyplot并取别名为plt
x=np.arange(0,2*np.pi,0.01)  #列表x在0到2π之间,毎隔0.01取一个点
y1=np.sin(x)      #求sin(x)对应的列表y1的值
y2=np.sin(-x)	  #求sin(-x)对立的列表y2的值
y3=np.sin(2*x)/2  #求sin(2x)/2对应的列表y3的值
plt.plot(x,y1)    #绘制sin(x)的图像
plt.plot(x,y2)    #绘制sin(-x)的图像
plt.plot(x,y3)    #绘制sin(2x)/ 2的图像
plt.title(‘sin(x)’)	   #设置图像标题
plt.xlabel(‘X’)		   #设置X轴标题
plt.ylabel(‘Y’) 	   #设置Y轴标题
plt.show( )			   #将绘制的函数图像窗口显示出来

The running effect is as follows:

Guess you like

Origin blog.csdn.net/qq_28782419/article/details/127755615