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

[파이썬 그래프] color pallet 또는 colormap 얻는 방법

by 만다린망고 2024. 8. 6.
반응형

아래와 같이 get_cmap 메소드를 사용합니다. 입력값 첫번째 자리에는 colormap 이름, 두번째 자리에는 생성하고 싶은 개수를 입력합니다. 

 

import matplotlib.cm as cm

colors = cm.get_cmap('Set1', 5)

 

colormap 들은 아래 링크에서 확인할 수 있습니다. 

 

https://matplotlib.org/stable/users/explain/colors/colormaps.html

 

Choosing Colormaps in Matplotlib — Matplotlib 3.9.1 documentation

Choosing Colormaps in Matplotlib Matplotlib has a number of built-in colormaps accessible via matplotlib.colormaps. There are also external libraries that have many extra colormaps, which can be viewed in the Third-party colormaps section of the Matplotlib

matplotlib.org

 

colormap 을 사용한 예시를 하나 보여드리겠습니다. 

 

import matplotlib.pyplot as plt
import matplotlib.cm as cm

fig, ax = plt.subplots(figsize=(10, 6))

my_colors = cm.get_cmap('Set1', 3)

#선 1 생성
ax.plot([10,10],[2,5],linewidth=6,color=my_colors(0))
#선 2 생성
ax.plot([15,15],[2,5],linewidth=6,color=my_colors(1))
#선 3 생성
ax.plot([20,20],[2,5],linewidth=6,color=my_colors(2))

ax.set_xlim(0, 30)
ax.set_ylim(0, 10)

plt.show()

 

반응형

댓글