本次作业主要参考了哈桑的雨天的作业(https://blog.csdn.net/weixin_48626051/article/details/111264498
并在代码方面增加了自己的尝试
感谢这位同学
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import numpy as np
x = np.linspace(0, 1, 10)
fig = plt.figure()
plt.title(r'Task 02')
for i in range(1, 7):
exec("ax_%s=fig.add_subplot(23%s)"%(i,i))
plot = Line2D(x, x**i)
exec("ax_%s.add_line(plot)"%i)
plt.show()
# 以下为原作者方法
# ax_1 = fig.add_subplot(231)
# plot = Line2D(x, x)
# ax_1.add_line(plot)
# plt.xlabel(r'X')
# plt.ylabel(r'X')
# ax_2 = fig.add_subplot(232)
# line = Line2D(x, x**2)
# ax_2.add_line(line)
# plt.xlabel(r'$X$')
# plt.ylabel(r'$X^2$')
# ax_3 = fig.add_subplot(233)
# plot = Line2D(x, x**3)
# ax_3.add_line(plot)
# plt.xlabel(r'$X$')
# plt.ylabel(r'$X^3$')
# ax_4 = fig.add_subplot(234)
# plot = Line2D(x, x**4)
# ax_4.add_line(plot)
# plt.xlabel(r'$X$')
# plt.ylabel(r'$X^4$')
# ax_5 = fig.add_subplot(235)
# plot = Line2D(x, x**5)
# ax_5.add_line(plot)
# plt.xlabel(r'$X$')
# plt.ylabel(r'$X^5$')
# ax_6 = fig.add_subplot(236)
# plot = Line2D(x, x**6)
# ax_6.add_line(plot)
# plt.xlabel(r'$X$')
# plt.ylabel(r'$X^6$')
# plt.show()
# 以下主要为原作者代码
import re
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0,10,0.1)
y = -1*(x-2)*(x-8)+10
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.plot(x, y, color='red')
for i in range(20,90):
rect = plt.Rectangle((x[i],0), width=(x[i+1]-x[i]-0.05), height=y[i], color='red', alpha=0.5)
ax1.add_patch(rect)
plt.ylim(0.0,20.0)
ax2 = fig.add_subplot(212)
ax2.plot(x, y, color='g')
x1 = np.arange(2,9,0.1)
y1 = -1*(x1-2)*(x1-8)+10
ax2.fill_between(x1, y1,y2=0,color="g", alpha=0.7)
plt.ylim(0.0,20.0)
plt.show()