Python
그래프 범례
Haribo-
2022. 11. 22. 10:39
그래프 범례
하나의 그래프에 여러 데이터를 한 번에 그리는 경우 각 데이터의 정보를 그래프에 함께 띄워 주어야 합니다.
이 때 사용하는 함수가 ax.legend()함수입니다.
속성 값을 변경하여 범례(legend)의 모양을 다양하게 변경할 수 있습니다.
이번 실습에서는 그래프의 범례(legend) 속성 중 위치를 변경하여 봅시다.
자주 사용되는 위치 옵션은 아래와 같습니다.
loc
문자형code문자형code‘best’ | 0 | ‘center left’ | 6 |
‘upper right’ | 1 | ‘center right’ | 7 |
‘upper left’ | 2 | ‘lower center’ | 8 |
‘lower left’ | 3 | ‘upper center’ | 9 |
‘lower right’ | 4 | ‘center’ | 10 |
‘right’ | 5 | - | - |
이렇게 해보세요!
위의 위치 옵션 표를 참고하여 문자형이나 code를 통해 범례의 위치를 왼쪽 중간으로 변경해보세요.
from elice_utils import EliceUtils
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
elice_utils = EliceUtils()
x = np.arange(10)
fig, ax = plt.subplots()
ax.plot(
x, x, label='y=x',
linestyle='-',
marker='.',
color='blue'
)
ax.plot(
x, x**2, label='y=x^2',
linestyle='-.',
marker=',',
color='red'
)
ax.set_xlabel("x")
ax.set_ylabel("y")
#이미 입력되어 있는 코드의 다양한 속성값들을 변경해 봅시다.
ax.legend(
loc='center left', # center left 로 변경해보세요.
shadow=True,
fancybox=True,
borderpad=2
)
# elice에서 그래프를 확인
fig.savefig("plot.png")
elice_utils.send_image("plot.png")