본문 바로가기
파이썬/시각화 matplot

이중 y축 표시하기

by Merware 2023. 5. 17.

[학습목표]
2중 y축을 표시하여 2가지 유형의 데이터를 한번에 표현할 수 있다.

import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.family']='NanumGothic'
plt.rcParams['axes.unicode_minus']=False

plt.rcParams['font.size'] = 12

샘플데이터

  • 어떤 학생의 나이에 따른 키와 몸무게의 변화
age = [13,14,15,16,17]
height = [160,165,170,173,177]
weight = [80,85,83,78,73]

두가지 정보를 하나의 그래프에 그리기

  • 축을 분리하기 위해 객체지향으로 그린다.
  • fig, ax = plt.subplots()
fig, ax = plt.subplots()
ax.bar(age, height, color='skyblue', width=0.5, ec='lightgray', label='height')
ax.plot(age, weight, color='darkred', marker='o', ls='-.', label='weight')
plt.legend()
plt.show()

 

2중 y축 만들기

  • x축을 공유하는 새로운 axes객체를 만든다.
    axes객체.twinx()
fig, ax1 = plt.subplots()
ax1.bar(age, height, color='skyblue', width=0.5, ec='lightgray', label='height')

ax2 = ax1.twinx()
ax2.plot(age, weight, color='darkred', marker='o', ls='-.', label='weight')
plt.show()

 

축 레이블 표시하기

axes객체.set_xlabel(x레이블)
axes객체.set_ylabel(y레이블)

fig, ax1 = plt.subplots()
ax1.bar(age, height, color='skyblue', width=0.5, ec='lightgray', label='height')

ax2 = ax1.twinx()
ax2.plot(age, weight, color='darkred', marker='o', ls='-.', label='weight')

ax1.set_xlabel('나이')
ax1.set_ylabel('키(cm)')
ax2.set_ylabel('몸무게(kg)')

plt.show()

 

y축 범위 지정

  • axes객체.set_ylim(y축눈금범위)
fig, ax1 = plt.subplots()
ax1.bar(age, height, color='skyblue', width=0.5, ec='lightgray', label='height')

ax2 = ax1.twinx()
ax2.plot(age, weight, color='darkred', marker='o', ls='-.', label='weight')

ax1.set_xlabel('나이')
ax1.set_ylabel('키(cm)')
ax2.set_ylabel('몸무게(kg)')

ax1.set_ylim(150,180)
ax2.set_ylim(60,90)
plt.show()

 

y축 눈금

  • axes객체.set_yticks(y축눈금)
  • axes객체.tick_params(...)
fig, ax1 = plt.subplots()
ax1.bar(age, height, color='skyblue', width=0.5, ec='lightgray', label='height')

ax2 = ax1.twinx()
ax2.plot(age, weight, color='darkred', marker='o', ls='-.', label='weight')

ax1.set_xlabel('나이')
ax1.set_ylabel('키(cm)')
ax2.set_ylabel('몸무게(kg)')

ax1.set_ylim(150,180)
ax2.set_ylim(60,90)

ax1.set_yticks(height)
ax2.set_yticks(weight)

ax1.tick_params(axis='y', colors='skyblue')
ax2.tick_params(axis='y', colors='darkred')

plt.show()

 

범례 표시

  • axes객체별로 legend메소드 호출
fig, ax1 = plt.subplots()
ax1.bar(age, height, color='skyblue', width=0.5, ec='lightgray', label='height')

ax2 = ax1.twinx()
ax2.plot(age, weight, color='darkred', marker='o', ls='-.', label='weight')

ax1.set_xlabel('나이')
ax1.set_ylabel('키(cm)')
ax2.set_ylabel('몸무게(kg)')

ax1.set_ylim(150,180)
ax2.set_ylim(60,90)

ax1.set_yticks(height)
ax2.set_yticks(weight)

ax1.tick_params(axis='y', colors='skyblue')
ax2.tick_params(axis='y', colors='darkred')

ax1.legend()
ax2.legend()

plt.show()

 

그리드 표시

  • axes객체.grid()
fig, ax1 = plt.subplots()
ax1.bar(age, height, color='skyblue', width=0.5, ec='lightgray', label='height')

ax2 = ax1.twinx()
ax2.plot(age, weight, color='darkred', marker='o', ls='-.', label='weight')

ax1.set_xlabel('나이')
ax1.set_ylabel('키(cm)')
ax2.set_ylabel('몸무게(kg)')

ax1.set_ylim(150,180)
ax2.set_ylim(60,90)

ax1.set_yticks(height)
ax2.set_yticks(weight)

ax1.tick_params(axis='y', colors='skyblue')
ax2.tick_params(axis='y', colors='darkred')

ax1.legend()
ax2.legend()

ax1.grid(axis='y', ls='--', color='skyblue')
ax2.grid(axis='y', ls='--', color='pink')

plt.show()