『科学计算』科学绘图库matplotlib练习

思想:万物皆对象

 作业

第一题:

import numpy as np
import matplotlib.pyplot as plt

x = [1, 2, 3, 1]
y = [1, 3, 0, 1]

def plot_picture(x, y):
    plt.plot(x, y, color='r', linewidth='2', linestyle='--', marker='D', label='one')
    plt.xticks(list(range(-5,5,1)))
    plt.yticks(list(range(-5,5,1)))
    plt.grid(True)
    

def rotate(x0, y0, point=[0, 0], angle=np.pi/2):
    rx = []
    ry = []
    for i, pos in enumerate(zip(x0,y0)):
        rx.append((x0[i]-point[0])*np.cos(angle) - (y0[i]-point[1])*np.sin(angle) + point[0])
        ry.append((y0[i]-point[1])*np.cos(angle) + (x0[i]-point[0])*np.sin(angle) + point[1])
    return rx, ry
print(rotate([3,1],[0,1],angle=-np.pi/2))

plot_picture(x, y)
plot_picture(rotate(x, y, angle=-np.pi / 2)[0], rotate(x, y, angle=-np.pi / 2)[1])

 

第二题:

% matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
fig.add_subplot(321)
fig.add_subplot(322)
fig.add_subplot(312)
fig.add_subplot(325)
fig.add_subplot(326)
fig.subplots_adjust(hspace=0.3)

 

第三题:

def rotate(x0, y0, point=[0, 0], angle=np.pi/2):
    rx = []
    ry = []
    for i, pos in enumerate(zip(x0,y0)):
        rx.append((x0[i]-point[0])*np.cos(angle) - (y0[i]-point[1])*np.sin(angle) + point[0])
        ry.append((y0[i]-point[1])*np.cos(angle) + (x0[i]-point[0])*np.sin(angle) + point[1])
    return rx, ry

def draw_flower(step=4, start_point=[2,0]):
    step = 2**step
    start_x = [start_point[0]]
    start_y = [start_point[1]]
    x = []
    y = []
    b_x = []
    b_y = []
    for i in range(step):
        angle = (step/4)*2*np.pi/(step-1)
        start_x, start_y = rotate(start_x, start_y, angle=angle)
        x += start_x
        y += start_y
        if i != 0:
            b_x.append((x[-2]+start_x)/2)
            b_y.append((y[-2]+start_y)/2)
    plt.plot(x, y, 'r')
    plt.plot(b_x, b_y, 'b.')
   plt.axis('equal')
    plt.grid(True)

draw_flower(6)

 

要求的菊花图,洞有点大,调整angle可以调整大小,

 第三题需要一点解析几何计算,难度不大,主要是回忆几何知识比较痛苦,明明刚刚考过试的,哎。

posted @ 2017-06-26 18:32  叠加态的猫  阅读(915)  评论(0编辑  收藏  举报