matplotlib fill和fill_between

 1 import numpy as np
 2 import matplotlib.pyplot as plt
 3 
 4 x = np.linspace(0, 5 * np.pi, 1000)
 5 
 6 y1 = np.sin(x)
 7 y2 = np.sin(2 * x)
 8 
 9 plt.plot(x, y1, label = "$ y = sin(x) $")
10 plt.plot(x, y2, label = "$ y = sin(2 * x) $")
11 plt.legend(loc = 3)
12 
13 plt.show()

对函数与坐标轴之间的区域进行填充,使用fill函数

 1 import numpy as np
 2 import matplotlib.pyplot as plt
 3 
 4 x = np.linspace(0, 5 * np.pi, 1000)
 5 
 6 y1 = np.sin(x)
 7 y2 = np.sin(2 * x)
 8 
 9 plt.fill(x, y1, color = "g", alpha = 0.3)
10 plt.fill(x, y2, color = "b", alpha = 0.3)
11 
12 plt.show()

填充两个函数之间的区域用fill_between

 1 import numpy as np
 2 import matplotlib.pyplot as plt
 3 
 4 x = np.linspace(0, 5 * np.pi, 1000)
 5 
 6 y1 = np.sin(x)
 7 y2 = np.sin(2 * x)
 8 
 9 plt.plot(x, y1, c = "g")
10 plt.plot(x, y2, c = 'r')
11 
12 # fill_between 填充两个函数之间的区域
13 # 两个函数之间的区域用黄色填充
14 plt.fill_between(x, y1, y2, facecolor = "yellow")
15 
16 plt.show()
原文地址:https://www.cnblogs.com/zenan/p/8417315.html