Matplotlib 에서 플롯 배경색을 설정하는 방법

  1. 특정 플롯의 배경색 설정
  2. 여러 플롯에 대한 기본 플롯 배경색 설정
Matplotlib 에서 플롯 배경색을 설정하는 방법

axes 객체의 set_facecolor(color)는 해당 플롯의 배경색, 즉 얼굴 색을 설정합니다.

Matplotlib 설정 플롯 배경색

특정 플롯의 배경색 설정

set_facecolor()메소드를 호출하기 전에 axes 객체를 가져와야합니다.

1. Matlab 과 같은 상태 저장 API

plt.plot(x, y)
ax = plt.gca()

완전한 예제 코드:

import matplotlib.pyplot as plt

plt.plot(range(5), range(5, 10))

ax = plt.gca()
ax.set_facecolor("m")
plt.show()

2. 객체 지향 방식으로 도형과 축 생성

figureaxes 객체는 함께 만들 수 있습니다.

fig, ax = plt.subplots()

또는 먼저 ‘그림’을 만든 다음 나중에 ‘축’을 시작하십시오.

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

완전한 예제 코드:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(1)

ax.plot(range(5), range(5, 10))

ax.set_facecolor("m")
plt.show()

또는,

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

ax.plot(range(5), range(5, 10))

ax.set_facecolor("m")
plt.show()

여러 플롯에 대한 기본 플롯 배경색 설정

다중 플롯에 기본 배경색을 설정해야하는 경우,rcParams 객체에서 axes.facecolor 속성을 설정할 수 있습니다.

plt.rcParams["axes.facecolor"] = color

완전한 예제 코드:

import matplotlib.pyplot as plt

plt.rcParams["axes.facecolor"] = "m"

plt.subplot(1, 2, 1)
plt.plot(range(5), range(5, 10))

plt.subplot(1, 2, 2)
plt.plot(range(5), range(10, 5, -1))

plt.show()

Matplotlib 세트 플롯 배경 Color_rcParams

보시다시피, 두 플롯의 배경색은 동일합니다.

튜토리얼이 마음에 드시나요? DelftStack을 구독하세요 YouTube에서 저희가 더 많은 고품질 비디오 가이드를 제작할 수 있도록 지원해주세요. 구독하다
작가: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

관련 문장 - Matplotlib Color