matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, *, edgecolors=None, plotnonfinite=False, data=None, **kwargs)
▪Parameters
‣ [x, y] : float or array-like, 데이터 위치
‣ [s] : float or array-like, (옵션 사항)
기본 값 : rcParams['lines.markersize']** 2 (typographic points are 1/72 in.)
▪ marker size는 linewidth와 edgecolor에 따라 상호작용하여 시각적으로 표현 가능
▪ marker edge를 제거하기 위해서는 linewidth=0 or edgecolor='none'으로 설정
‣ [c] : array-like or list of color or color, (옵션 사항)
▪ 단일 숫자 RGB 또는 RGBA일 수 없음
▫ 동일한 RGB 또는 RGBA 값을 지정하려면 단일 행이 있는 2D 배열 사용
▪ 기본 값은 'None'
‣ [marker] : MarkerStyle, 기본 값 : 0, 마커 스타일 지정
‣ [cmap] : str or Colormap, 기본 값 : 'viridis',
▪ Colormap instance 또는 registered colormap
▪ [c]가 RGB(A)일 경우 해당 parameter는 무시
‣ [norm] : str or Normalize, cmap을 사용하여 색상으로 매핑하기 전, scalar data를 [0, 1] range로 스케일링하는 데 사용되는 정규화 방법
▪ 기본적으로 linear scaling 사용, 가장 낮은 값을 0, 가장 높은 값을 1로 매핑
▪ [c]가 RGB(A)일 경우 해당 parameter는 무시
‣ [vmin, vmax] : float, (옵션 사항)
▪ 명시적인 [norm]가 없고 scalar 데이터를 사용하는 경우 vmin, vmax 정의
▪ [c]가 RGB(A)일 경우 해당 parameter는 무시
‣ [alpha] : float, 기본 값 : None, Alpha blending 값, 투명 0에서 불투명 1사이의 값
‣ [linewidth] : float or array-like, 기본 값 : 1.5, 선 폭 지정 값
‣ [edgecolor]: {'face', 'none', None} or color or list of color, default: rcParams["scatter.edgecolors"] (default: 'face') ,
▪ 'face' : edge color와 face color 같음
▪ 'none' : patch boundary 그리지 않음
‣ [plotnonfinite] : bool, 기본 값 : False,
▪ 무한이 아닌 c로 점을 표시할지 여부 (i.e. inf,-inf or nan)
▪ 참일 경우, bad colormap으로 색상 표현 (Colormap.set_bad)
‣ Returns : PathCollection
<Example 01>
import matplotlib.pyplot as plt
import numpy as np
# 데이터 생성
np.random.seed(0) # 랜덤 시드 설정으로 동일한 결과 재현
x = np.random.rand(50)
y = np.random.rand(50)
# 기본 산점도 생성
plt.scatter(x, y)
plt.title("Basic Scatter Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
Result
<Example 02>
import matplotlib.pyplot as plt
import numpy as np
# 데이터 생성
x = np.random.rand(50)
y = np.random.rand(50)
colors = np.random.rand(50) # 색상을 위한 값
sizes = 1000 * np.random.rand(50) # 크기를 위한 값
# 색상과 크기를 지정한 산점도 생성
plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='viridis')
plt.title("Scatter Plot with Color and Size")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.colorbar() # 색상 바 추가
plt.show()
Result
<Example 03>
import matplotlib.pyplot as plt
import numpy as np
# 데이터 생성
x = np.random.rand(50)
y = np.random.rand(50)
colors = np.random.rand(50)
sizes = 1000 * np.random.rand(50)
# 산점도 생성
plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='plasma')
plt.title("Advanced Scatter Plot with Annotations")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.colorbar()
# 특정 데이터 포인트에 주석 추가
for i in range(len(x)):
if sizes[i] > 700: # 특정 크기 이상인 점들만 주석 추가
plt.annotate(f'({x[i]:.2f}, {y[i]:.2f})', (x[i], y[i]), textcoords="offset points", xytext=(5,5), ha='center')
# 축 범위 설정
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.show()
Result
import matplotlib.pyplot as plt
import numpy as np
cmap_names = [
'viridis', 'plasma', 'inferno', 'magma', 'cividis', # Perceptually Uniform Sequential
'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds', # Sequential
'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu', # Sequential continued
'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn', # Sequential continued
'PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu', # Diverging
'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic', # Diverging continued
'twilight', 'twilight_shifted', 'hsv', # Cyclic
'Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2', # Qualitative
'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b', 'tab20c', # Qualitative continued
'flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern', # Miscellaneous
'gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg', # Miscellaneous continued
'gist_rainbow', 'rainbow', 'jet', 'nipy_spectral', 'gist_ncar' # Miscellaneous continued
]
def plot_color_gradients(cmap_list):
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))
fig, axes = plt.subplots(nrows=len(cmap_list), figsize=(8, 0.3 * len(cmap_list)))
fig.subplots_adjust(top=0.99, bottom=0.01, left=0.2, right=0.99)
for ax, name in zip(axes, cmap_list):
ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
ax.text(-0.01, 0.5, name, va='center', ha='right', fontsize=10, transform=ax.transAxes)
ax.set_axis_off()
plt.show()
plot_color_gradients(cmap_names)
Result
※ 이 글이 도움이 되었다면 "🤍공감" 버튼을 클릭해주세요. 클릭 한번이 글 쓰는데 큰 힘이 됩니다.
내 블로그 - 관리자 홈 전환 |
Q
Q
|
---|---|
새 글 쓰기 |
W
W
|
글 수정 (권한 있는 경우) |
E
E
|
---|---|
댓글 영역으로 이동 |
C
C
|
이 페이지의 URL 복사 |
S
S
|
---|---|
맨 위로 이동 |
T
T
|
티스토리 홈 이동 |
H
H
|
단축키 안내 |
Shift + /
⇧ + /
|
* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.