本文实例为大家分享了matplotlib实现区域颜色填充的具体代码,供大家参考,具体内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace( 0 , 5 * np.pi, 1000 )
y1 = np.sin(x)
y2 = np.sin( 2 * x)
#plt.plot(x,y1)
#plt.plot(x,y2)
plt.fill(x,y1, 'b' ,alpha = 0.5 )
plt.fill(x,y2, 'r' ,alpha = 0.3 )
plt.fill_between(x,y1,y2,facecolor = 'green' )
plt.grid(true)
plt.show()
#########################################################
plt.plot(x,y1, 'b' ,alpha = 0.5 )
plt.plot(x,y2, 'r' ,alpha = 0.3 )
#添加条件
#如果数据点比较少的情况下,会有缝隙出现,使用interpolate可以填充缝隙
plt.fill_between(x,y1,y2,where = y1> = y2,facecolor = 'green' ,interpolate = true)
plt.fill_between(x,y1,y2,where = y2>y1,facecolor = 'yellow' ,interpolate = true)
plt.grid(true)
plt.show()
###########################################################
n = 256
x = np.linspace( - np.pi, np.pi, n, endpoint = true)
y = np.sin( 2 * x)
plt.plot(x, y + 1 , color = 'blue' , alpha = 1.00 )
plt.fill_between(x, 1 , y + 1 , color = 'blue' , alpha = . 25 )
plt.plot(x, y - 1 , color = 'blue' , alpha = 1.00 )
plt.fill_between(x, - 1 , y - 1 , (y - 1 ) > - 1 , color = 'blue' , alpha = . 25 )
plt.fill_between(x, - 1 , y - 1 , (y - 1 ) < - 1 , color = 'red' , alpha = . 25 )
plt.xlim( - np.pi, np.pi)
plt.xticks(())
plt.ylim( - 2.5 , 2.5 )
plt.yticks(())
|
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/szfhy/article/details/82710742