Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python Matplotlib에서 곡선 아래에 무지개 색을 채우는 방법은 무엇입니까?

<시간/>

Python Matplotlib의 곡선 아래에 무지개 색을 채우기 위해 다음 단계를 수행할 수 있습니다. -

  • 그림 크기를 설정하고 서브플롯 사이 및 주변 여백을 조정합니다.
  • 사용자 정의 메소드, plot_rainbow_under_curve() 생성 , 7개의 무지개 색상 목록이 있고 데이터 포인트 세트 "x"를 생성할 수 있습니다. numpy를 사용합니다.
  • 0에서 7 사이의 범위에서 반복하고 곡선을 그리고 곡선 사이의 영역을 채웁니다.
  • 그림을 표시하려면 show()를 사용하세요. 방법.

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
def plot_rainbow_under_curve():
   rainbow_colors = ['violet', 'indigo', 'blue', 'green', 'yellow', 'orange', 'red']
   x = np.linspace(-5, 5, 100)
   for i in range(0, 7):
      plt.plot(x, x ** 2, lw=0)
      plt.fill_between(x, x ** 2 + len(rainbow_colors)-i,color=rainbow_colors[i])

plot_rainbow_under_curve()

plt.show()

출력

다음 출력을 생성합니다.

Python Matplotlib에서 곡선 아래에 무지개 색을 채우는 방법은 무엇입니까? Python Matplotlib에서 곡선 아래에 무지개 색을 채우는 방법은 무엇입니까?