当前位置:网站首页>Use fill and fill in Matplotlib_ Between fill the blank area between functions

Use fill and fill in Matplotlib_ Between fill the blank area between functions

2022-06-26 04:45:00 I am a little monster

Catalog

 fill: Used to fill the area between the coordinate axis and the function

 fill_between: Used to fill the area between functions  

  The following is with explicit parameters fill_between An example of :


fill: Used to fill the area between the coordinate axis and the function

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

x=np.linspace(0,2*np.pi,num=500)
y=np.sin(x)

plt.fill(x,y,color='cornflowerblue',alpha=0.4)
plt.plot(x,y,color='red',alpha=0.8)
plt.plot([x[0],x[-1]],[y[0],y[-1]],color='red',alpha=0.8)

plt.xlim(0,2*np.pi)
plt.ylim(-1.1,1.1)

plt.show()

 fill_between: Used to fill the area between functions  

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

x=np.linspace(0,2,500)# Use numpy In the library linspace stay 0 To 2 Take the average of 500 A little bit 
y1=np.sin(2*np.pi*x)
y2=1.4*np.sin(3*np.pi*x)# Draw... Separately y1 and y2 Images 

fig,ax=plt.subplots(3,1,sharex='all')# This is a 3 That's ok 1 Sharing of columns x Grid layout subarea of the axis 
plt.xlim(0,2)# Set up x Axis range 

ax[0].fill_between(x,0,y1,color='red',alpha=0.5)# The first subgraph curve y1 and y=0 The area between is filled with red 
ax[0].set_ylim(-1.2,1.2)# Set up y Axis range 

ax[1].fill_between(x,y2,1.1,color='blue',alpha=0.5)#y2 and y=1.1 The area between them is filled with blue , Transparency is 0.5
ax[1].set_ylim(-1.5,1.5)

ax[2].fill_between(x,y1,y2,color='blue',alpha=0.3) #y1 and y2 The area between them is filled with blue , Transparency is 0.3
ax[2].set_ylim(-1.5,1.5)

plt.show()

  The following is with explicit parameters fill_between An example of :

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

x=np.linspace(0,2,500)
y1=np.sin(2*np.pi*x)
y2=1.4*np.sin(3*np.pi*x)

fig=plt.figure(figsize=(12,6))# Specifies that the size of the drawing board is 12:6
ax=fig.add_subplot(111)# Add canvas 

ax.plot(x,y1,color='black')# Draw images separately 
ax.plot(x,y2,color='red')
ax.set_xlim(0,2) Set up x The scope of the shaft 

#where Specified scope ,interpolate Set to True, You can assign a color to the area near the intersection of the curve , There is no change after I adjust this parameter 
ax.fill_between(x,y1,y2,where=y1<=y2,interpolate=True,facecolor='cornflowerblue',alpha=0.7)
ax.fill_between(x,y2,y1,where=y1>=y2,interpolate=True,facecolor='darkred',alpha=0.7)

plt.show()

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

fig=plt.figure()
ax=fig.add_subplot(111)

y=np.linspace(0,2,500)
x1=np.sin(2*np.pi*y)
x2=1.4*np.sin(3*np.pi*y)

ax.plot(x1,y,color='k',ls=':',lw=2)# Set the line style to dashed line , Width is 2
ax.plot(x2,y,color='k')

ax.fill_betweenx(y,x2,x1,where=x1<=x2,interpolate=True,facecolor='cornflowerblue',alpha=0.7)
ax.fill_betweenx(y,x2,x1,where=x1>=x2,interpolate=True,facecolor='darkred',alpha=0.7)
ax.set_xlim(-1.5,1.5)
ax.set_ylim(0,2)
ax.grid(ls=':',lw=1,color='gray',alpha=0.5)
plt.show()

原网站

版权声明
本文为[I am a little monster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180510154062.html