Matplotlib에서 원을 그리는 방법(예제 포함)
다음 구문을 사용하는 Circle() 함수를 사용하여 Matplotlib의 플롯에 원을 빠르게 추가할 수 있습니다.
matplotlib.patches.Circle(xy, 반경=5)
금:
- xy : 원의 좌표 (x, y)
- 반경: 원의 반경. 기본값은 5입니다.
이 튜토리얼에서는 이 기능의 실제 사용에 대한 몇 가지 예를 보여줍니다.
예 1: 단일 원 만들기
다음 코드는 좌표 (x,y)(10,10)에 위치한 Matplotlib 플롯에 단일 원을 만드는 방법을 보여줍니다.
import matplotlib. pyplot as plt #set axis limits of plot (x=0 to 20, y=0 to 20) plt. axis ([0, 20, 0, 20]) #create circle with (x, y) coordinates at (10, 10) c=plt. Circle ((10, 10)) #add circle to plot (gca means "get current axis") plt. gca (). add_artist (c)
기본적으로 Matplotlib 플롯의 축은 일반적으로 데이터 단위당 더 많은 픽셀을 표시합니다. 원을 타원 대신 원으로 표시하려면 다음과 같이 plt.axis(“equal”) 인수를 사용해야 합니다.
import matplotlib. pyplot as plt #set axis limits of plot (x=0 to 20, y=0 to 20) plt. axis ([0, 20, 0, 20]) plt. axis (" equal ") #create circle with (x, y) coordinates at (10, 10) c=plt. Circle ((10, 10)) #add circle to plot (gca means "get current axis") plt. gca (). add_artist (c)
예 2: 여러 개의 원 만들기
다음 코드는 Matplotlib 플롯에 여러 원을 만드는 방법을 보여줍니다.
import matplotlib. pyplot as plt #set axis limits of plot (x=0 to 20, y=0 to 20) plt. axis ([0, 20, 0, 20]) plt. axis (" equal ") #define circles c1=plt. Circle ((5, 5), radius= 1 ) c2=plt. Circle ((10, 10), radius= 2 ) c3=plt. Circle ((15, 13), radius= 3 ) #add circles to plot plt. gca (). add_artist (c1) plt. gca (). add_artist (c2) plt. gca (). add_artist (c3)
예 3: 원 모양 변경
Matplotlib에서 원의 모양을 변경하려면 다음 인수를 사용할 수 있습니다.
- radius: 원의 반지름을 지정합니다.
- 색상: 원의 색상을 지정합니다.
- 알파: 원의 투명도를 지정합니다.
다음 코드는 이러한 여러 인수를 한 번에 사용하는 방법의 예를 보여줍니다.
import matplotlib. pyplot as plt #set axis limits of plot (x=0 to 20, y=0 to 20) plt. axis ([0, 20, 0, 20]) plt. axis (" equal ") #create circle with (x, y) coordinates at (10, 10) c=plt. Circle ((10, 10), radius= 2 , color=' red ', alpha= .3 ) #add circle to plot (gca means "get current axis") plt. gca (). add_artist (c)
사용자 정의 16진수 색상 코드를 사용하여 원의 색상을 지정할 수도 있습니다.