Python绘制参数方程图
vscode里面自动的jupyter,有毒,用不了。。。要执行下面的操作
pip install pip-autoremovepip-autoremove.exe jupyter -ypip install jupyter
新版的code,可以设置更多的选项

终于看到了我们久违的python,可以使用tab跳出了

shift+Enter

Markdown也正常使用
import matplotlib.pyplot as plt from matplotlib import animation import numpy as np import math def xin(): t = np.linspace(0, math.pi*2, 1000) # 参数方程的范围 x = np.cos(3*t) y = np.sin(2*t) # 参数式 plt.plot(x, y, color='blue', linewidth=2, label='圆') # 传入x,y,颜色是蓝色,线宽, plt.xlabel('t') plt.ylabel('h') # y,x轴的名字 plt.ylim(-1, 1) plt.xlim(-1.5,1.5) # 坐标轴的长度 plt.legend() plt.show() xin()
先完整的绘制一个图
取x点
书写表达式
绘制
美化
x = np.cos(50*t) y = np.sin(39*t)
将参数改变,再绘制一次

接下来绘制圆的参数方程
#半径r = 2.0# 圆心a, b = (0., 0.)#参数方程theta = np.arange(0, 2*np.pi, 0.01)x = a + r * np.cos(theta)y = b + r * np.sin(theta)
很完美
from math import pifrom numpy import cos, sinfrom matplotlib import pyplot as plt
if __name__ == '__main__': '''plot data margin''' angles_circle = [i * pi / 180 for i in range(0, 360)] # i先转换成double x = cos(angles_circle) y = sin(angles_circle) plt.plot(x, y, 'r')
plt.axis('equal') plt.axis('scaled') plt.show()
另外一种绘制圆形的方法~
import numpy as npfrom matplotlib import pyplot as pltr=2.0a,b=0.0,0.0# 标准方程x = np.arange(a-r, a+r, 0.01)y = b + np.sqrt(r**2 - (x - a)**2)
fig = plt.figure() axes = fig.add_subplot(111)axes.plot(x, y) # 上半部axes.plot(x, -y) # 下半部
axes.axis('equal')
plt.show()
这里是使用的圆的标准方程进行绘制
import matplotlib.pyplot as pltimport numpy as np
# create 1000 equally spaced points between -10 and 10x = np.linspace(-10, 10, 1000)
# calculate the y value for each element of the x vectory = x**2 + 2*x + 2
fig, ax = plt.subplots()ax.plot(x, y)
抛物线
import matplotlib.pyplot as plt
a=[]b=[]# y=0# x=-50
for x in range(-50,50,1): y=x**2+2*x+2 a.append(x) b.append(y) #x= x+1
fig= plt.figure()axes=fig.add_subplot(111)axes.plot(a,b)plt.show()三个参数,分别代表子图的行数,列数,图索引号
因为频繁的出现add_asubplot()
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplot.html
import matplotlib.pyplot as pltimport numpy as np'''Set the values in the variable xThe function arange helps to generate an array with the following parameters arange(start,end,increment)'''x = np.arange(-100,100,1)'''Now set the formula in the variable y'''y = x**2'''Then add the pair (x,y) to the plot'''plt.plot(x,y)'''Finally show the graph'''plt.show()import numpy as npimport matplotlib.pyplot as pltx = np.arange(0, 100)plt.subplot(221)plt.plot(x, x)plt.subplot(222)plt.plot(x, -x)plt.subplot(223)plt.plot(x, x ** 2)plt.subplot(224)plt.plot(x, np.log(x))plt.show()

import numpy as npimport matplotlib.pyplot as plt
x = np.arange(0, 100)# 首先就是生成点列,xfig = plt.figure()# 创建一个大的画布ax1 = fig.add_subplot(221)ax1.plot(x, x)# 第一个图,直接221的位置ax2 = fig.add_subplot(222)ax2.plot(x, -x)# 222的位置,-的斜率ax3 = fig.add_subplot(223)ax3.plot(x, x ** 2)# 二次函数ax4 = fig.add_subplot(224)ax4.plot(x, np.log(x))# 对数形式plt.show()

pyplot的方式中plt.subplot()参数和面向对象中的add_subplot()参数和含义都相同
这里针对,子图的绘制函数做了一个简单的绘制~
赞 (0)
