A.I
파이썬으로 그래프 그리기 본문
1. 파이썬으로 그래프 그리기¶
$ pip install matplotlib
$ pip install seaborn
In [10]:
import matplotlib.pyplot as plt
%matplotlib inline
#그래프 데이터
subject = ['English', 'Math', 'Korean', 'Science', 'Computer']
points = [40, 90, 50, 60, 100]
# 축 그리기
fig = plt.figure() # 도화지 펼치기
ax1 = fig.add_subplot(1,1,1) # 1행 1열로 1개 그릴수 있다는 뜻
# 그래프 그리기
ax1.bar(subject, points)
# 라벨, 타이틀 달기
plt.xlabel('Subject')
plt.ylabel('Points')
plt.title("Yuna's Test Result")
# 보여주기
plt.savefig('./barplot.png') # 그래프를 이미지로 출력
plt.show() # 그래프를 화면으로 출력
2. 주석 기법을 통한 선 그래프 그리기¶
In [11]:
from datetime import datetime
import pandas as pd
import os
#그래프 데이터
csv_path = os.getenv("HOME") + "/aiffel/data_represent/data/AMZN.csv"
data = pd.read_csv(csv_path ,index_col=0, parse_dates=True)
price = data['Close']
# 축 그리기 및 좌표축 설정
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
price.plot(ax=ax, style='black') # subplot을 사용한다
plt.ylim([1600,2200])
plt.xlim(['2019-05-01','2020-03-01'])
# 주석달기
important_data = [(datetime(2019, 6, 3), "Low Price"),(datetime(2020, 2, 19), "Peak Price")]
for d, label in important_data:
ax.annotate(label, xy=(d, price.asof(d)+10),
xytext=(d,price.asof(d)+100),
arrowprops=dict(facecolor='red'))
# 그리드, 타이틀 달기
plt.grid() # 격자눈금 추가
ax.set_title('StockPrice')
# 보여주기
plt.show()
3. plt.plot()로 그래프 그리기¶
In [12]:
import numpy as np
x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x),'o')
plt.plot(x, np.cos(x),'--', color='black')
plt.show()
In [13]:
x = np.linspace(0, 10, 100)
plt.subplot(2,1,1)
plt.plot(x, np.sin(x),'orange','o')
plt.subplot(2,1,2)
plt.plot(x, np.cos(x), 'orange')
plt.show()
In [14]:
# linestyle, marker옵션
x = np.linspace(0, 10, 100)
plt.plot(x, x + 0, linestyle='solid')
plt.plot(x, x + 1, linestyle='dashed')
plt.plot(x, x + 2, linestyle='dashdot')
plt.plot(x, x + 3, linestyle='dotted')
plt.plot(x, x + 0, '-g') # solid green
plt.plot(x, x + 1, '--c') # dashed cyan
plt.plot(x, x + 2, '-.k') # dashdot black
plt.plot(x, x + 3, ':r'); # dotted red
plt.plot(x, x + 4, linestyle='-') # solid
plt.plot(x, x + 5, linestyle='--') # dashed
plt.plot(x, x + 6, linestyle='-.') # dashdot
plt.plot(x, x + 7, linestyle=':'); # dotted
4. Pandas로 그래프 그리기¶
pandas.plot메서드 인자
- label: 그래프의 범례이름.
- ax: 그래프를 그릴 matplotlib의 서브플롯 객체.
- style: matplotlib에 전달할 'ko--'같은 스타일의 문자열
- alpha: 투명도 (0 ~1)
- kind: 그래프의 종류: line, bar, barh, kde
- logy: Y축에 대한 로그스케일
- use_index: 객체의 색인을 눈금 이름으로 사용할지의 여부
- rot: 눈금 이름을 로테이션(0 ~ 360)
- xticks, yticks: x축, y축으로 사용할 값
- xlim, ylim: x축, y축 한계
- grid: 축의 그리드 표시할 지 여부
pandas의 data가 DataFrame일때 plot 메서드 인자
- subplots: 각 DataFrame의 칼럼을 독립된 서브플롯에 그린다.
- sharex: subplots=True면 같은 X축을 공유하고 눈금과 한계를 연결한다.
- sharey: subplots=True면 같은 Y축을 공유한다.
- figsize: 그래프의 크기, 튜플로 지정
- title: 그래프의 제목을 문자열로 지정
- sort_columns: 칼럼을 알파벳 순서로 그린다.
In [15]:
fig, axes = plt.subplots(2, 1)
data = pd.Series(np.random.rand(5), index=list('abcde'))
data.plot(kind='bar', ax=axes[0], color='blue', alpha=1)
data.plot(kind='barh', ax=axes[1], color='red', alpha=0.3)
Out[15]:
<AxesSubplot:>
In [16]:
df = pd.DataFrame(np.random.rand(6,4), columns=pd.Index(['A','B','C','D']))
df.plot(kind='line')
Out[16]:
<AxesSubplot:>
각 요소 별 명칭¶
https://github.com/mwaskom/seaborn-data -> csv파일형식의 데이터셋들이라 연습하기 좋음
5. Seaborn 데이터로 그래프 그리기¶
5-1. 범주형 데이터 그래프¶
In [20]:
import pandas as pd
import seaborn as sns
tips = sns.load_dataset("tips")
In [21]:
df = pd.DataFrame(tips)
df.head()
Out[21]:
total_bill | tip | sex | smoker | day | time | size | |
---|---|---|---|---|---|---|---|
0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
In [22]:
df.shape
Out[22]:
(244, 7)
In [23]:
df.describe()
Out[23]:
total_bill | tip | size | |
---|---|---|---|
count | 244.000000 | 244.000000 | 244.000000 |
mean | 19.785943 | 2.998279 | 2.569672 |
std | 8.902412 | 1.383638 | 0.951100 |
min | 3.070000 | 1.000000 | 1.000000 |
25% | 13.347500 | 2.000000 | 2.000000 |
50% | 17.795000 | 2.900000 | 2.000000 |
75% | 24.127500 | 3.562500 | 3.000000 |
max | 50.810000 | 10.000000 | 6.000000 |
In [24]:
df.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 244 entries, 0 to 243 Data columns (total 7 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 total_bill 244 non-null float64 1 tip 244 non-null float64 2 sex 244 non-null category 3 smoker 244 non-null category 4 day 244 non-null category 5 time 244 non-null category 6 size 244 non-null int64 dtypes: category(4), float64(2), int64(1) memory usage: 7.3 KB
In [25]:
print(df['sex'].value_counts())
print("===========================")
print(df['time'].value_counts())
print("===========================")
print(df['smoker'].value_counts())
print("===========================")
print(df['day'].value_counts())
print("===========================")
print(df['size'].value_counts())
print("===========================")
Male 157 Female 87 Name: sex, dtype: int64 =========================== Dinner 176 Lunch 68 Name: time, dtype: int64 =========================== No 151 Yes 93 Name: smoker, dtype: int64 =========================== Sat 87 Sun 76 Thur 62 Fri 19 Name: day, dtype: int64 =========================== 2 156 3 38 4 37 5 5 6 4 1 4 Name: size, dtype: int64 ===========================
In [29]:
# matplotlib에 데이터를 인자로 넣기 위해선 pandas 데이터를 바로 이용할 수는 없으므로 데이터를 x, y에 series, list 형태로 각각 나눠주어야함.
grouped = df['tip'].groupby(df['sex'])
In [27]:
grouped.mean() # 성별에 따른 팁의 평균
Out[27]:
sex Male 3.089618 Female 2.833448 Name: tip, dtype: float64
In [28]:
grouped.size() # 성별에 따른 데이터 량(팁 횟수)
Out[28]:
sex Male 157 Female 87 Name: tip, dtype: int64
In [54]:
import numpy as np
sex = dict(grouped.mean())
x = list(sex.keys())
y = list(sex.values())
plt.bar(x, y)
plt.ylabel('tip[$]')
plt.title('Tip by Sex')
#plt.legend()
Out[54]:
Text(0.5, 1.0, 'Tip by Sex')
In [34]:
sns.barplot(data=df, x='sex', y='tip')
Out[34]:
<AxesSubplot:xlabel='sex', ylabel='tip'>
In [35]:
plt.figure(figsize=(10,6))
sns.barplot(data=df, x='sex', y='tip')
plt.ylim(0, 4)
plt.title('Tip by sex')
Out[35]:
Text(0.5, 1.0, 'Tip by sex')
In [36]:
plt.figure(figsize=(10,6))
sns.barplot(data=df, x='day', y='tip')
plt.ylim(0, 4)
plt.title('Tip by day')
Out[36]:
Text(0.5, 1.0, 'Tip by day')
In [37]:
fig = plt.figure(figsize=(13,10))
ax1 = fig.add_subplot(2,2,1)
sns.barplot(data=df, x='day', y='tip',palette="ch:.25")
ax2 = fig.add_subplot(2,2,2)
sns.barplot(data=df, x='sex', y='tip')
ax3 = fig.add_subplot(2,2,4)
sns.violinplot(data=df, x='sex', y='tip')
ax3 = fig.add_subplot(2,2,3)
sns.violinplot(data=df, x='day', y='tip',palette="ch:.25")
Out[37]:
<AxesSubplot:xlabel='day', ylabel='tip'>
In [38]:
sns.catplot(x="day", y="tip", jitter=False, data=tips)
Out[38]:
<seaborn.axisgrid.FacetGrid at 0x7f7bda1f0b50>
5-2. 수치형 그래프¶
In [39]:
sns.scatterplot(data=df , x='total_bill', y='tip', palette="ch:r=-.2,d=.3_r")
Out[39]:
<AxesSubplot:xlabel='total_bill', ylabel='tip'>
In [40]:
sns.scatterplot(data=df , x='total_bill', y='tip', hue='day')
Out[40]:
<AxesSubplot:xlabel='total_bill', ylabel='tip'>
In [42]:
x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x), 'o')
plt.plot(x, np.cos(x))
plt.show()
In [43]:
sns.lineplot(x, np.sin(x))
sns.lineplot(x, np.cos(x))
/home/ssac24/anaconda3/envs/aiffel/lib/python3.7/site-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation. FutureWarning /home/ssac24/anaconda3/envs/aiffel/lib/python3.7/site-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation. FutureWarning
Out[43]:
<AxesSubplot:>
5-3. 히스토그램¶
In [44]:
#그래프 데이터
mu1, mu2, sigma = 100, 130, 15 # x1,x2 의 평균은 100, 130
x1 = mu1 + sigma*np.random.randn(10000)
x2 = mu2 + sigma*np.random.randn(10000)
# 축 그리기
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
# 그래프 그리기 / 도수를 50개의 구간으로 표시하며, 확률 밀도가 아닌 빈도로 표기
patches = ax1.hist(x1, bins=50, density=False)
patches = ax1.hist(x2, bins=50, density=False, alpha=0.5)
ax1.xaxis.set_ticks_position('bottom')
ax1.yaxis.set_ticks_position('left')
# 라벨, 타이틀 달기
plt.xlabel('Bins')
plt.ylabel('Number of Values in Bin')
ax1.set_title('Two Frequency Distributions')
# 보여주기
plt.show()
In [45]:
sns.distplot(df['total_bill'])
sns.distplot(df['tip'])
/home/ssac24/anaconda3/envs/aiffel/lib/python3.7/site-packages/seaborn/distributions.py:2557: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms). warnings.warn(msg, FutureWarning) /home/ssac24/anaconda3/envs/aiffel/lib/python3.7/site-packages/seaborn/distributions.py:2557: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms). warnings.warn(msg, FutureWarning)
Out[45]:
<AxesSubplot:xlabel='tip', ylabel='Density'>
In [46]:
# 전체 결제 금액 대비 팁의 비율
df['tip_pct'] = df['tip'] / df['total_bill']
df['tip_pct'].hist(bins=50)
Out[46]:
<AxesSubplot:>
In [47]:
# KDE(Kernel Density Estimate) 커널 밀도 추정 그래프로 표현
df['tip_pct'].plot(kind='kde')
Out[47]:
<AxesSubplot:ylabel='Density'>
6. 시계열 데이터 그래프화¶
mkdir -p ~/aiffel/data_represent/data
wget https://aiffelstaticprd.blob.core.windows.net/media/documents/flights.csv
mv flights.csv ~/aiffel/data_represent/data
In [48]:
csv_path = os.getenv("HOME") + "/aiffel/data_represent/data/flights.csv"
data = pd.read_csv(csv_path)
flights = pd.DataFrame(data)
flights
Out[48]:
year | month | passengers | |
---|---|---|---|
0 | 1949 | January | 112 |
1 | 1949 | February | 118 |
2 | 1949 | March | 132 |
3 | 1949 | April | 129 |
4 | 1949 | May | 121 |
... | ... | ... | ... |
139 | 1960 | August | 606 |
140 | 1960 | September | 508 |
141 | 1960 | October | 461 |
142 | 1960 | November | 390 |
143 | 1960 | December | 432 |
144 rows × 3 columns
In [49]:
sns.barplot(data=flights, x='year', y='passengers')
Out[49]:
<AxesSubplot:xlabel='year', ylabel='passengers'>
In [50]:
sns.pointplot(data=flights, x='year', y='passengers')
Out[50]:
<AxesSubplot:xlabel='year', ylabel='passengers'>
In [51]:
sns.lineplot(data=flights, x='year', y='passengers')
Out[51]:
<AxesSubplot:xlabel='year', ylabel='passengers'>
In [56]:
# month 별로 나누기 위해 hue 값을 줌
sns.lineplot(data=flights, x='year', y='passengers', hue='month', palette='ch:.50')
plt.legend(bbox_to_anchor=(1.03, 1), loc=2) #legend 그래프 밖에 추가하기
Out[56]:
<matplotlib.legend.Legend at 0x7f7bd0878550>
In [57]:
# 히스토그램
sns.distplot(flights['passengers'])
/home/ssac24/anaconda3/envs/aiffel/lib/python3.7/site-packages/seaborn/distributions.py:2557: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms). warnings.warn(msg, FutureWarning)
Out[57]:
<AxesSubplot:xlabel='passengers', ylabel='Density'>
7. Heatmap¶
- 방대한 양의 데이터를 수치별로 색상을 다르게 표현한 그래프
In [58]:
# 데이터의 재배치 pivot
pivot = flights.pivot(index='year', columns='month', values='passengers')
pivot
Out[58]:
month | April | August | December | February | January | July | June | March | May | November | October | September |
---|---|---|---|---|---|---|---|---|---|---|---|---|
year | ||||||||||||
1949 | 129 | 148 | 118 | 118 | 112 | 148 | 135 | 132 | 121 | 104 | 119 | 136 |
1950 | 135 | 170 | 140 | 126 | 115 | 170 | 149 | 141 | 125 | 114 | 133 | 158 |
1951 | 163 | 199 | 166 | 150 | 145 | 199 | 178 | 178 | 172 | 146 | 162 | 184 |
1952 | 181 | 242 | 194 | 180 | 171 | 230 | 218 | 193 | 183 | 172 | 191 | 209 |
1953 | 235 | 272 | 201 | 196 | 196 | 264 | 243 | 236 | 229 | 180 | 211 | 237 |
1954 | 227 | 293 | 229 | 188 | 204 | 302 | 264 | 235 | 234 | 203 | 229 | 259 |
1955 | 269 | 347 | 278 | 233 | 242 | 364 | 315 | 267 | 270 | 237 | 274 | 312 |
1956 | 313 | 405 | 306 | 277 | 284 | 413 | 374 | 317 | 318 | 271 | 306 | 355 |
1957 | 348 | 467 | 336 | 301 | 315 | 465 | 422 | 356 | 355 | 305 | 347 | 404 |
1958 | 348 | 505 | 337 | 318 | 340 | 491 | 435 | 362 | 363 | 310 | 359 | 404 |
1959 | 396 | 559 | 405 | 342 | 360 | 548 | 472 | 406 | 420 | 362 | 407 | 463 |
1960 | 461 | 606 | 432 | 391 | 417 | 622 | 535 | 419 | 472 | 390 | 461 | 508 |
In [59]:
sns.heatmap(pivot)
Out[59]:
<AxesSubplot:xlabel='month', ylabel='year'>
In [60]:
sns.heatmap(pivot, linewidths=.2, annot=True, fmt="d")
Out[60]:
<AxesSubplot:xlabel='month', ylabel='year'>
In [61]:
sns.heatmap(pivot, cmap="YlGnBu")
Out[61]:
<AxesSubplot:xlabel='month', ylabel='year'>
'파이썬 & AI 학습' 카테고리의 다른 글
주사위 만들기 (0) | 2021.01.25 |
---|---|
sklearn의 이해 (0) | 2021.01.22 |
fibonacci함수 (0) | 2021.01.08 |
1주차 leetcode😢 (0) | 2021.01.08 |
파이썬 명령어 모음 (0) | 2020.12.31 |