创作灵感来自于帮同学编的一个程序,需要的两个python库是numpy和matplotlib。首先要生成矩形框的散点
import numpy as np
import matplotlib.pyplot as plt
def func1(a,b,nmax):
ze = list(np.linspace(-a, a, nmax))
ye=list(np.linspace(0,b,nmax))
xx1=ze
yy1=[ye[0]]*nmax
xx2=ze
yy2=[ye[-1]]*nmax
xx3=[ze[0]]*nmax
yy3=ye
xx4=[ze[-1]]*nmax
yy4=ye
x=xx1+xx2+xx3+xx4
y=yy1+yy2+yy3+yy4
return x,y
编程思路也比较简单,ze对应的是x坐标,ye对应的是y坐标,xx1,xx2,xx3,,x4分别对应矩形的四条边的横坐标。yy1,yy2,yy3,yy4分别对应矩形四条边的纵坐标。用列表进行存储,最后将四条边的坐标对应好合成在一起。
xx,yy=func1(10,5,20)
plt.subplot(131)
plt.scatter(xx,yy)
plt.title('namx=20')
xx,yy=func1(10,5,50)
plt.subplot(132)
plt.scatter(xx,yy)
plt.title('namx=50')
xx,yy=func1(10,5,100)
plt.subplot(133)
plt.scatter(xx,yy)
plt.title('namx=100&#