第1关:手动创建子图
任务:绘制一个包含两个样式不一致的子图。
import matplotlib
matplotlib.use("Agg")
import numpy as np
import matplotlib.pyplot as plt
def student(x):
'''
根据输入数据绘制不同的两个子图
:param x: 输入数据,类型为array
:return: None
'''
# ********* Begin *********#
plt.figure(figsize=(12,12))
plt.subplot(1, 2, 1)
plt.plot(x,x**2,'--r',linewidth=1,alpha=0.3)
plt.subplot(1, 2, 2)
plt.plot(x,np.log(x))
plt.savefig("Task1/img/T1.png")
# ********* End *********#
第2关:网格子图
任务:编写一个能绘制一个两行四列的网格子图的程序。
import matplotlib
matplotlib.use("Agg")
import numpy as np
import matplotlib.pyplot as plt
def student():
'''
绘制不同一个两行四列的网格子图,并设置图形高度与宽度为0.4
:param: None
:return: None
'''
# ********* Begin *********#
fig=plt.figure(figsize=(10,10))
fig.subplots_adjust(hspace=0.4, wspace=0.4)
for i in range(1, 9):
ax = fig.add_subplot(2, 4, i)
plt.savefig("Task2/img/T1.png")
# ********* End *********#
第3关:更复杂的排列方式
任务:编写一个绘制不规则子图的函数。
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")
def student():
'''
绘制一个2行4列的不规则子图宽高间隔分别为0.4、0.3。
第0行设置2个子图,第一个子图占3,第二个子图占1。第二行相反
:param: None
:return: None
'''
# ********* Begin *********#
plt.figure(figsize=(10,10))
grid = plt.GridSpec(2, 4, wspace=0.4, hspace=0.3)
plt.subplot(grid[0, :3])
plt.subplot(grid[0, 3:]) #[0,-1]
plt.subplot(grid[1, 0]) #[-1,0]
plt.subplot(grid[1, 1:]) #[-1,1:]
plt.savefig("Task3/img/T1.png")
# ********* End *********#