[Matplotlib] 축, 범례
안녕하세요. 쇼미요미입니다.
나도코딩님을 통해 학습한 데이터 시각화 내용입니다.
오늘은 그래프의 축, 범례에 대해서 알아보도록 하겠습니다.
1. 축
1-1) label 지정
- label은 label명, color, 위치 입력 가능
- color : 'r' 또는 'red' 또는 '#FFFFFF 입력 가능
- 위치는 x축의 경우 left, center, right 중 선택, y축의 경우 top, center, bottom 중 선택
x = [1,2,3]
y = [2,4,8]
plt.plot(x,y)
plt.title('꺾은선 그래프', fontdict={'family':'Gulim','size':13}) #타이틀명, 타이틀 폰트, 사이즈 설정
plt.xlabel('X축',color='red', loc='right')
plt.ylabel('Y축',color='#00aa00', loc='top')
plt.show()
matplotlib.pyplot.plot — Matplotlib 2.1.2 documentation
Plot lines and/or markers to the Axes. args is a variable length argument, allowing for multiple x, y pairs with an optional format string. For example, each of the following is legal: If x and/or y is 2-dimensional, then the corresponding columns will be
matplotlib.org
List of named colors — Matplotlib 3.5.0 documentation
Note Click here to download the full example code
matplotlib.org
1-2) 범위 지정
- 값을 직접 입력하여 지정
plt.xticks([1,2,3], rotation=90) #x축 범위 지정, x축 텍스트 회전
plt.yticks([3,6,9,12]) #y축 범위 지정
- 최소, 최대값을 입력하여 지정
plt.xticks([1,2,3], rotation=90) #x축 범위 지정
plt.ylim(0,20) #y축 최소 0~20까지 범위 지정
2. 범례
- 범례 위치를 설정하지 않았을 경우 좌상단에 범례 노출
- 범례 위치는 x축,y축을 직접 입력하거나, string 또는 code로 입력 가능
plt.plot(x,y,label='데이터') #범례 이름 설정
plt.legend() #범례 위치 설정
-----------------------------------------
plt.legend(loc=(0,0)) #튜플로 넣음. 0~1사이로 적어야 함
matplotlib.pyplot.legend — Matplotlib 3.5.0 documentation
Place a legend on the Axes. The call signatures correspond to the following different ways to use this method: 1. Automatic detection of elements to be shown in the legend The elements to be added to the legend are automatically determined, when you do not
matplotlib.org
