Python/Matplotlib

[Matplotlib] 그래프 스타일

쇼미요미 2022. 5. 4. 01:27
728x90

 

안녕하세요. 쇼미요미입니다.

 

나도코딩님을 통해 학습한 데이터 시각화 내용입니다.

오늘은 그래프 스타일에 대해서 알아보도록 하겠습니다.

 

 

1. line 설정

 

1-1) linewidth

x = [1,2,3]
y = [2,4,8]
plt.plot(x,y,linewidth=5)
plt.show()

 

1-2) linestyle

- 'solid'   # Same as '-'
- 'dotted'   # Same as ':'
- 'dashed'   # Same as '--'
- 'dashdot'  # Same as '-.'

plt.plot(x,y,marker='o', linestyle='None')   #라인을 없앰

출처 :https://matplotlib.org/3.5.0/gallery/lines_bars_and_markers/linestyles.html

 

Linestyles — Matplotlib 3.5.0 documentation

Linestyles Simple linestyles can be defined using the strings "solid", "dotted", "dashed" or "dashdot". More refined control can be achieved by providing a dash tuple (offset, (on_off_seq)). For example, (0, (3, 10, 1, 15)) means (3pt line, 10pt space, 1pt

matplotlib.org

 

1-3) line color

plt.plot(x,y,color='red')

출처 :https://matplotlib.org/2.1.2/api/_as_gen/matplotlib.pyplot.plot.html

 

matplotlib.pyplot.plot — Matplotlib 2.1.2 documentation

Plot lines and/or markers to the Axes. args is a variable length argument, allowing for multiple x, y pairs with an optional format string. For example, each of the following is legal: If x and/or y is 2-dimensional, then the corresponding columns will be

matplotlib.org

 

 

List of named colors — Matplotlib 3.5.0 documentation

Note Click here to download the full example code

matplotlib.org

 

2. Marker 설정

 

2-1) Marker 모양

plt.plot(x,y,marker='o')   #마커를 'o' 모양으로 찍음

출처 : https://matplotlib.org/stable/api/markers_api.html

 

2-2) Marker Size, Marker edgecolor, Marker facecolor

plt.plot(x,y,marker='o', markersize=10, markeredgecolor='red', markerfacecolor='yellow')

 

3. 포맷

plt.plot(x,y,'ro--')   #x축, y축, 'color, marker, linestyle'

 

4. 축약어

plt.plot(x,y,marker='o', mfc='red', ms='10', mec='blue',ls=':')

출처 :https://matplotlib.org/3.5.0/api/_as_gen/matplotlib.pyplot.plot.html

 

matplotlib.pyplot.plot — Matplotlib 3.5.0 documentation

An object with labelled data. If given, provide the label names to plot in x and y. Note Technically there's a slight ambiguity in calls where the second label is a valid fmt. plot('n', 'o', data=obj) could be plt(x, y) or plt(y, fmt). In such cases, the f

matplotlib.org

 

5. 투명도

plt.plot(x,y,marker='o', mfc='red', ms='10', alpha=0.2) #0~1

 

6. 그래프 크기

plt.figure(figsize=(10,5), dpi=200)   #dpi는 그래프 크기를 비율에 맞춰 노출함 (200은 크기를 2배로 노출)
plt.plot(x,y)

 

7. 배경색

plt.figure(facecolor='yellow')
plt.plot(x,y)

 

8. 텍스트

plt.plot(x,y, marker='o')

for idx,txt in enumerate(y):
    plt.text(x[idx],y[idx] + 0.3,txt,ha='center', color='r')
    #x좌표, y좌표, 텍스트, 텍스트 위치, 텍스트 컬러

 

9. 격자

- axis = both, x, y로 구분

plt.grid(axis='both', color='r', alpha=0.3, linestyle='--', linewidth=2)

 

 

 

728x90