Matplotlib에서 줄 사이의 영역을 채우는 방법
다음 함수를 사용하여 Matplotlib 플롯의 값 사이의 영역을 쉽게 채울 수 있습니다.
- fill_between() : 두 수평 곡선 사이의 영역을 채웁니다.
- fill_betweenx() : 두 수직 곡선 사이의 영역을 채웁니다.
이 튜토리얼에서는 이러한 각 기능을 실제로 사용하는 방법에 대한 예를 제공합니다.
예 1: 두 수평선 사이의 영역 채우기
다음 코드는 두 개의 수평선 사이의 영역을 채우는 방법을 보여줍니다.
import matplotlib. pyplot as plt import numpy as np #define x and y values x = np. arange (0,10,0.1) y = np. arange (10,20,0.1) #create plot of values plt. plot (x,y) #fill in area between the two lines plt. fill_between (x,y,color=' red ')
어떤 값이 채워졌는지 더 쉽게 확인하기 위해 plt.grid() 함수를 사용하여 플롯에 그리드를 추가할 수도 있습니다.
import matplotlib. pyplot as plt import numpy as np #define x and y values x = np. arange (0,10,0.1) y = np. arange (10,20,0.1) #create plot of values plt. plot (x,y) #fill in area between the two lines plt. fill_between (x, y, color=' red ', alpha= .5 ) #add gridlines plt. grid ()
예 2: 곡선 아래 영역 채우기
다음 코드는 곡선 아래 영역을 채우는 방법을 보여줍니다.
import matplotlib. pyplot as plt import numpy as np #define x and y values x = np. arange (0,10,0.1) y = x**4 #create plot of values plt. plot (x,y) #fill in area between the two lines plt. fill_between (x, y, color=' red ', alpha= .5 )
예 3: 곡선 위 영역 채우기
다음 코드는 곡선 위의 영역을 채우는 방법을 보여줍니다.
import matplotlib. pyplot as plt import numpy as np #define x and y values x = np. arange (0,10,0.1) y = x**4 #create plot of values plt. plot (x,y) #fill in area between the two lines plt. fill_between (x, y, np. max (y), color=' red ', alpha= .5 )
예 4: 두 수직선 사이의 영역 채우기
다음 코드는 fill_betweenx() 함수를 사용하여 두 수직선 사이의 영역을 채우는 방법을 보여줍니다.
import matplotlib. pyplot as plt import numpy as np #define x and y values x = np. arange (0,10,0.1) y = np. arange (10,20,0.1) #create plot of values plt. plot (x,y) #fill in area between the two lines plt. fill_betweenx (y, 2, 4, color=' red ', alpha= .5 )
관련 항목: Matplotlib에서 부드러운 곡선을 그리는 방법