본문 바로가기

Programming

[Python][Library] 3. matplotlib - 그래프 패키지

반응형

목차

0. 참조 사이트

1. matplotlib API

- 기본

- 시스템 폰트 찾기 

- 한글 폰트 설정

- 한글 폰트 설정 확인

1.1 Figure 와 subplot

1) 기본

2) 여러개의 그림판 생성

3) 여러개의 그림판 배열로 생성

1.2 Line Chart

1.3 Bar Chart

1.4 Pie Chart

1.5 Scatter Chart

1.6 색상, 마커, 선 스타일

1) 기본 문법

2) Matplotlib 스타일

- color

- line

- 조합

- 마커 참조 사이트

1.7 눈금, 라벨, 범례

1) 제목 축 이름, 눈금 이름

2) 범례 추가

1.8 주석, 그림 추가

1) 주석

2) 그림

1.9 파일로 저장

2. Pandas 에서 그래프 그리기

2.1 선 그래프

1) Series.plot() 인자

2) DataFrame.plot() 인자

2.2 막대 그래프

[참고] seaborn 그리기

2.3. 히스토그램, 밀도 그래프

2.4 산포도


0. 참조 사이트

- 한글폰트 다운로드
[구글 폰트 무료 배포 사이트] (https://www.google.co.kr/get/noto/#/)
 

Google Fonts

Making the web more beautiful, fast, and open through great typography

fonts.google.com

Korean 으로 검색
예: 'Noto Sans Korean'

 

- matplotlib documenation: https://matplotlib.org/stable/contents.html

 

https://matplotlib.org/stable/contents.html

 

matplotlib.org

 

1. Matplotlib API

import numpy as np
#np.set_printoptions(precision=4, suppress=True)

import pandas as pd
#pd.options.display.max_rows = 20

import matplotlib.pyplot as plt
%matplotlib inline

plt.rc('figure', figsize=(10, 6))

from matplotlib import rcParams
rcParams['font.family'] = 'NanumGothic'
rcParams['font.size'] = 10
rcParams['axes.unicode_minus'] = False
 
 

시스템 폰트 찾기

import matplotlib.font_manager as fm

for f in fm.fontManager.ttflist:
    if f.name.find('Nanum') != -1:
        print(f.name)
 

한글 폰트 설정

from matplotlib import rcParams

rcParams['font.family'] = 'NanumGothic'
rcParams['font.size'] = 10

 

한글 폰트 설정 확인

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(range(10))
ax.set_title('한글 제목')
plt.show()
 

1.1 Figure 와 subplot

기본

# %%
data = np.arange(10)
data
# %%
plt.plot(data)
# %%
plt.plot(np.random.randn(50).cumsum(), 'k--')
# %%
plt.hist(np.random.randn(100), bins=20, color='k', alpha=0.3)
plt.show()
 

여러개의 그림판 생성

fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
ax1.hist(np.random.randn(100), bins=20, color='k', alpha=0.3)
ax2.scatter(np.arange(30), np.arange(30) + 3 * np.random.randn(30))

Subplot 옵션

subplot(nrows, ncols, sharex, sharey)
 
인자 | 설명
:---|:---
nrows | 서브플롯의 로우 수
ncols | 서브플롯의 컬럼 수
sharex | 모든 서브플롯이 x축 눈금을 공유
sharey | 모든 서블플롯이 y축 눈금을 공유

여러 개의 그림판 배열로 생성

fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)
for i in range(2):
    for j in range(2):
        axes[i, j].hist(np.random.randn(500), bins=50, color='k', alpha=0.5)

# 간격조정        
plt.subplots_adjust(wspace=0, hspace=0)
 

1.2 Line Chart

from dateutil import parser

kl = pd.read_csv('data/k_league_fw_goals.csv', index_col=0)
lp = pd.read_csv('data/liverpool.csv', index_col=0, date_parser=parser.parse)
cf = pd.read_csv('data/seac_coffee.csv')

coffee = cf[['A', 'C', 'E', 'S']].sum().values
index = np.arange(len(coffee))
 
kl.head()
cf.head()
lp.head()
plt.plot(lp['CumWin'])
plt.plot(lp[['CumWin', 'CumLost']])
plt.plot(cf['A'])
plt.plot(cf[['A', 'C', 'E', 'S']])
 

1.3 Bar Chart

# %%
plt.bar(np.arange(4), cf[['A', 'C', 'E', 'S']].sum().values)
 
# %%
bar_width = 0.5
plt.bar(index, coffee, width=bar_width, color='r')
plt.title('# Stores by Coffee Shop')
plt.xlabel('Coffee Shop')
plt.ylabel('# Store')
plt.xticks(index + bar_width - 0.25, ('A', 'C', 'E', 'S'))
plt.show()
 
# %%
bar_height = 0.5
plt.barh(index, coffee, height=bar_height, color='g')
plt.title('# Stores by Coffee Shop')
plt.ylabel('Coffee Shop')
plt.xlabel('# Store')
plt.yticks(index + bar_width - 0.25, ('A', 'C', 'E', 'S'))
plt.show()
 
 

1.4 Pie Chart

# %%
labels = ('Angelinus', 'Cafe Bene', 'Ediya', 'Starbucks')
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']

plt.pie(coffee, labels=labels, colors=colors,
        autopct='%1.1f%%', shadow=True,
        startangle=135, explode=(0, 0, 0, 0.3))

plt.show()
 

1.5 Scatter Chart

# %%
fig, ax = plt.subplots()
ax.scatter(kl['st'], kl['goal'], alpha=0.5, color='r', marker='*')
ax.set_xlabel('st')
ax.set_ylabel('goal')
ax.set_title('Shoot vs Goal')
ax.grid(True)
 
# %%
with plt.xkcd():
    bar_width = 0.5
    bar_height = 0.5
    plt.barh(index, coffee, height=bar_height, color='g')
    plt.title('# Stores by Coffee Shop')
    plt.ylabel('Coffee Shop')
    plt.xlabel('# Store')
    plt.yticks(index + bar_width - 0.25, ('A', 'C', 'E', 'S'))
   

1.6 색상, 마커, 선 스타일

# %%
plt.plot(np.random.randn(30).cumsum(), 'g--')
#plt.plot(np.random.randn(30).cumsum(), 'ko--')
 
# %%
plt.plot(np.random.randn(30).cumsum(), color='g', linestyle='--')
#plt.plot(np.random.randn(30).cumsum(), color='k', linestyle='dashed', marker='o')
 
# %%
data = np.random.randn(30).cumsum()
plt.plot(data, 'k--', label='Default')
plt.plot(data, 'k-', drawstyle='steps-post', label='steps-post')
plt.legend(loc='best')

Matplotlib 스타일

1) 컬러 

컬러 기호| 컬러
:---|:---
b| blue
g| green
r| red
c| cyan
m| magenta
y| yellow
k| black
w| white
 
* Grayscale (0 - 1)
```python
color = '0.75'
```
 
* HTML Hex String
```python
color = '#eeefff'
```

2) 라인

선 기호 | 선 모양
:---|:---
'-' | solid
'--' | dashed
'-.' | dash_dot
':' | dotted
'' | draw nothing
 

3) 조합

- 'k--'
- 'g-.'
- 'r:'
- 'ko--'

4) 마커 참조 사이트

 
 

matplotlib.markers — Matplotlib 3.5.1 documentation

matplotlib.markers Functions to handle markers; used by the marker functionality of plot, scatter, and errorbar. All possible markers are defined here: marker symbol description "." point "," pixel "o" circle "v" triangle_down "^" triangle_up "<" triangle_

matplotlib.org

 
 

1.7 색상, 마커, 선 스타일

1) 제목 축 이름, 눈금 이름

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(np.random.randn(1000).cumsum())

 

ax.set_xticks([0, 250, 500, 750, 1000])
ax.set_xticklabels(['one', 'two', 'three', 'four', 'five'],
                            rotation=30, fontsize='small')

 

ax.set_title('My first matplotlib plot')
ax.set_xlabel('Stages')
 
 

2) 범레 추가

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

ax.plot(np.random.randn(1000).cumsum(), 'k', label='one')
ax.plot(np.random.randn(1000).cumsum(), 'k--', label='two')
ax.plot(np.random.randn(1000).cumsum(), 'k.', label='three')

ax.legend(loc='best')
 

1.8 주석, 그림 추가

1) 주석

from datetime import datetime
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
data = pd.read_csv('Lecture_python/python02/data/spx.csv', index_col=0, parse_dates=True)
spx = data['SPX']
spx.plot(ax=ax, style='k-')
crisis_data = [
    (datetime(2007, 10, 11), 'Peak of bull market'),
    (datetime(2008, 3, 12), 'Bear Stearns Fails'),
    (datetime(2008, 9, 15), 'Lehman Bankruptcy')
]
for date, label in crisis_data:
    ax.annotate(label, xy=(date, spx.asof(date) + 75),
                xytext=(date, spx.asof(date) + 225),
                arrowprops=dict(facecolor='black', headwidth=4, width=2,
                                headlength=4),
                horizontalalignment='left', verticalalignment='top')
# X축 범위 지정
ax.set_xlim(['1/1/2007', '1/1/2011'])
# Y축 범위 지정
ax.set_ylim([600, 1800])
# 제목 지정
ax.set_title('Important dates in the 2008-2009 financial crisis')

2) 그림

fig = plt.figure(figsize=(12, 6)); ax = fig.add_subplot(1, 1, 1)
rect = plt.Rectangle((0.2, 0.75), 0.4, 0.15, color='k', alpha=0.3)
circ = plt.Circle((0.7, 0.2), 0.15, color='b', alpha=0.3)
pgon = plt.Polygon([[0.15, 0.15], [0.35, 0.4], [0.2, 0.6]], color='g', alpha=0.5)
ax.add_patch(rect)
ax.add_patch(circ)
ax.add_patch(pgon)
 
 
1.9 파일로 저장
 
plt.plot(np.random.randn(50).cumsum(), 'k--')
plt.savefig('data/1.png')
plt.savefig('data/2.png', dpi=400, bbox_inches='tight')
 

2. Pandas 에서 그래프 그리기

2.1 선 그래프

# %% Series
sr = pd.Series(np.random.randn(10).cumsum(), index=np.arange(0, 100, 10))
sr.plot()
 
 
# %% Dataframe
df = pd.DataFrame(np.random.randn(10, 4).cumsum(0),
                  columns=['A', 'B', 'C', 'D'],
                  index=np.arange(0, 100, 10))
df.plot()
 
#### Series.plot() 인자

인자 | 설명
:---|:---
lable | 그래프의 범례 이름
style | matplotlib에 전달할 'ko--'같은 스타일의 문자열
alpha | 그래프 투명도 ( 0 ~ 1 )
kind | 그래프 종류. 'line', 'bar', 'barh', 'kde'
use_index | 객체의 색인을 눈금 이름으로 사용할지의 여부
xticks | X축으로 사용할 값
yticks | Y축으로 사용할 값
xlim | X 축 한계
ylim | Y 축 한계
 
#### DataFrame에서만 사용하는 plot() 인자

인자 | 설명
:---|:---
subplots | 각 DataFram의 컬럼을 독립된 서브플롯에 그린다.
sharex | X축을 공유한다.
sharey | Y축을 공유한다.
figsize | 그래프의 크기
title | 그래프의 제목
 

2.2 막대 그래프

# %%
fig, axes = plt.subplots(2, 1)
data = pd.Series(np.random.rand(16), index=list('abcdefghijklmnop'))
data.plot.bar(ax=axes[0],  color='g', alpha=0.7)
data.plot.barh(ax=axes[1], color='b', alpha=0.7)
 
# %%
df = pd.DataFrame(np.random.rand(6, 4),
                  index=['one', 'two', 'three', 'four', 'five', 'six'],
                  columns=pd.Index(['A', 'B', 'C', 'D'], name='Genus'))
df
df.plot.bar()
 
# %%
df.plot.barh(stacked=True, alpha=0.5)
 
# %%
tips = pd.read_csv('Lecture_python/python02/data/tips.csv')
party_counts = pd.crosstab(tips['day'], tips['size'])
party_counts

# Not many 1- and 6-person parties
party_counts = party_counts.loc[:, 2:5]
 
# %%
# Normalize: item / sum(item)
party_pcts = party_counts.div(party_counts.sum(axis=1), axis=0)
party_pcts
party_pcts.plot.bar()

[참고] seaborn 그리기

# %%
# !pip install seaborn
import seaborn as sns

 

# %%
tips['tip_pct'] = tips['tip'] / (tips['total_bill'] - tips['tip'])
tips.head()
 
# %%
sns.barplot('tip_pct','day', data=tips, orient='h')
#sns.barplot(x='tip_pct', y='day', data=tips, orient='h')
 
# %%
sns.barplot(x='tip_pct', y='day', hue='time', data=tips, orient='h')

 

# %%
sns.set(style="whitegrid")
 

2.3 히스토그램, 밀도 그래프

# %%
tips['tip_pct'].plot.hist(bins=50)
 
# %%
tips['tip_pct'].plot.density()
 
# %%
# seaborn
comp1 = np.random.normal(0, 1, size=200)
comp2 = np.random.normal(10, 2, size=200)
values = pd.Series(np.concatenate([comp1, comp2]))
sns.distplot(values, bins=100, color='k')
 
 

2.4 산포도

# %%
macro = pd.read_csv('Lecture_python/python02/data/macrodata.csv')
data = macro[['cpi', 'm1', 'tbilrate', 'unemp']]
trans_data = np.log(data).diff().dropna()
trans_data[-5:]
 
# %%
plt.scatter(trans_data['m1'], trans_data['unemp'])
plt.title('Change in log %s vs. log %s' % ('m1', 'unemp'))

# %%
pd.plotting.scatter_matrix(trans_data, diagonal='kde', color='k', alpha=0.3)
plt.show()
 
# %%
# seaborn
sns.regplot('m1', 'unemp', data=trans_data)
plt.title('Changes in log %s versus log %s' % ('m1', 'unemp'))
 
# %%
# seaborn
sns.pairplot(trans_data, diag_kind='kde', plot_kws={'alpha': 0.2})
 

 

import matplotlib.pyplot as plt
 

1) 라인 플롯

 

plt.title('test'# 그래프 제목
plt.plot([1,2,3,4],[2,4,8,6]) # X값과 Y값을 정의
plt.show()
 
 

레이블 삽입

 
plt.title('test')
plt.plot([1,2,3,4],[2,4,8,6])
plt.xlabel('hours'# x축 레이블
plt.ylabel('score'# y축 레이블
plt.show()
 
 

범례 삽입

 

plt.title('test')
plt.plot([1,2,3,4],[2,4,8,6]) # 라인1
plt.plot([1.5,2.5,3.5,4.5],[3,5,8,10]) # 라인2
plt.xlabel('hours')
plt.ylabel('score')
plt.legend(['A''B']) # 라인1, 라인2의 범례 삽입
plt.show()
 
 
 

2) 카테고리컬 밸류

 

names = ['group_a''group_b''group_c']
values = [110100]

 

plt.figure(figsize=(9,3)) #figure 객체 생성하기 

 

plt.subplot(131)           # 1 x 3 행렬의 첫번째 그림
plt.bar(names, values)

 

plt.subplot(132)
plt.scatter(names, values)

 

plt.subplot(133)
plt.plot(names, values)

 

plt.suptitle('Categorical plotting')
plt.show()
 
 
 
#실습
#아래의 데이터를 이용하여 라인과 스캐터가 함께 표시되는 차트를 생성하시오. 
#데이터: smelt_length = [9.8, 10.5, 10.6, 11.0, 11.2 11.3, 11.8, 11.8, 12.0, 12.2, 12.4, 13.0, 14.3, 15.0]
#        smelt_weight = [6.7, 7.5, 7.0, 9.7, 9.8, 8.7, 10.0, 9.9, 9.8, 12.2, 13.4, 12.2, 19.7, 19.9]   
#label : x축 길이, y축 무게 
 
 
 
#실습
#아래의 데이터를 이용하여 라인과 스캐터가 각각 표시되는 차트를 생성하시오. 
#데이터: smelt_length = [9.8, 10.5, 10.6, 11.0, 11.2 11.3, 11.8, 11.8, 12.0, 12.2, 12.4, 13.0, 14.3, 15.0]
#        smelt_weight = [6.7, 7.5, 7.0, 9.7, 9.8, 8.7, 10.0, 9.9, 9.8, 12.2, 13.4, 12.2, 19.7, 19.9]   
#label : x축 길이, y축 무게 
 
 
 
 

 

 
반응형
LIST