Matplotlib 作业

1. 绘制如下动画

import numpy as np
import matplotlib.animation as animation 
import matplotlib.pyplot as plt
 
fig, ax = plt.subplots(constrained_layout=True) 

x = np.arange(0, 2*np.pi, 0.01) 
y = np.cos(x) 
line, = ax.plot(x, y) 
dot, = ax.plot(x[0],y[0],marker='o',color='r') 

def init():  # only required for blitting to give a clean slate. 
    dot.set_data(np.nan,np.nan) 
    return dot, 
 
def animate(i): 
    # 你的代码
    return dot, 

ani = animation.FuncAnimation(# 你的代码 ) 

ani.save("my_fancy_animation.gif", dpi=80)                  

2. 绘制如下图形

import matplotlib.pyplot as plt
import matplotlib as mpl

# 请换成你的电脑上的字体文件
myfont = mpl.font_manager.FontProperties(fname='/System/Library/Fonts/STHeiti Light.ttc')

def format_axes(fig):
    for i, ax in enumerate(fig.axes):
        ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
        ax.tick_params(labelbottom=False, labelleft=False)
        
fig = plt.figure()
gs = fig.add_gridspec(2, 3)
# 你的代码

format_axes(fig)
plt.savefig('layout.png',dpi=100,bbox_inches='tight')