DataScience/Matplotlip
Matplotlip&seaborn 기본적인 차트, 파이차트
leopard4
2022. 11. 28. 11:31
PYTHON PROGRAMMING FUNDAMENTALS¶
In [ ]:
# 해당 내용은 numpy ,pandas 와 연계되므로 사전 내용을 숙지 후 보시기를 바랍니다.
In [ ]:
Tidy Data¶
- each variable is a column
- each observation is a row
- each type of observational unit is a table
In [ ]:
In [ ]:
In [ ]:
가장기본적인 Plot¶
In [1]:
import matplotlib.pyplot as plt
In [2]:
import numpy as np
In [5]:
x = np.arange(0, 9 + 1)
In [6]:
x
Out[6]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [7]:
y = x
In [8]:
y
Out[8]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [12]:
# 기본적인 차트.
plt.plot(x, y)
plt.savefig("test1.jpg") # 사진을 저장하라. (항상 show 위에)
plt.show() # 메모리상태를 지움. (보기좋게)
In [ ]:
Bar Charts¶
In [ ]:
In [13]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb
%matplotlib inline # 주피터 노트북안에서 차트를 잘나오게 한다?
In [14]:
pd.read_csv("../data/pokemon.csv")
Out[14]:
id | species | generation_id | height | weight | base_experience | type_1 | type_2 | hp | attack | defense | speed | special-attack | special-defense | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | bulbasaur | 1 | 0.7 | 6.9 | 64 | grass | poison | 45 | 49 | 49 | 45 | 65 | 65 |
1 | 2 | ivysaur | 1 | 1.0 | 13.0 | 142 | grass | poison | 60 | 62 | 63 | 60 | 80 | 80 |
2 | 3 | venusaur | 1 | 2.0 | 100.0 | 236 | grass | poison | 80 | 82 | 83 | 80 | 100 | 100 |
3 | 4 | charmander | 1 | 0.6 | 8.5 | 62 | fire | NaN | 39 | 52 | 43 | 65 | 60 | 50 |
4 | 5 | charmeleon | 1 | 1.1 | 19.0 | 142 | fire | NaN | 58 | 64 | 58 | 80 | 80 | 65 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
802 | 803 | poipole | 7 | 0.6 | 1.8 | 189 | poison | NaN | 67 | 73 | 67 | 73 | 73 | 67 |
803 | 804 | naganadel | 7 | 3.6 | 150.0 | 243 | poison | dragon | 73 | 73 | 73 | 121 | 127 | 73 |
804 | 805 | stakataka | 7 | 5.5 | 820.0 | 257 | rock | steel | 61 | 131 | 211 | 13 | 53 | 101 |
805 | 806 | blacephalon | 7 | 1.8 | 13.0 | 257 | fire | ghost | 53 | 127 | 53 | 107 | 151 | 79 |
806 | 807 | zeraora | 7 | 1.5 | 44.5 | 270 | electric | NaN | 88 | 112 | 75 | 143 | 102 | 80 |
807 rows × 14 columns
In [157]:
df = pd.read_csv("../data/pokemon.csv")
In [17]:
df.shape
Out[17]:
(807, 14)
In [18]:
df.describe()
Out[18]:
id | generation_id | height | weight | base_experience | hp | attack | defense | speed | special-attack | special-defense | |
---|---|---|---|---|---|---|---|---|---|---|---|
count | 807.000000 | 807.000000 | 807.000000 | 807.000000 | 807.000000 | 807.000000 | 807.000000 | 807.000000 | 807.000000 | 807.000000 | 807.000000 |
mean | 404.000000 | 3.714994 | 1.162454 | 61.771128 | 144.848823 | 68.748451 | 76.086741 | 71.726146 | 65.830235 | 69.486989 | 70.013631 |
std | 233.105126 | 1.944148 | 1.081030 | 111.519355 | 74.953116 | 26.032808 | 29.544598 | 29.730228 | 27.736838 | 29.439715 | 27.292344 |
min | 1.000000 | 1.000000 | 0.100000 | 0.100000 | 36.000000 | 1.000000 | 5.000000 | 5.000000 | 5.000000 | 10.000000 | 20.000000 |
25% | 202.500000 | 2.000000 | 0.600000 | 9.000000 | 66.000000 | 50.000000 | 55.000000 | 50.000000 | 45.000000 | 45.000000 | 50.000000 |
50% | 404.000000 | 4.000000 | 1.000000 | 27.000000 | 151.000000 | 65.000000 | 75.000000 | 67.000000 | 65.000000 | 65.000000 | 65.000000 |
75% | 605.500000 | 5.000000 | 1.500000 | 63.000000 | 179.500000 | 80.000000 | 95.000000 | 89.000000 | 85.000000 | 90.000000 | 85.000000 |
max | 807.000000 | 7.000000 | 14.500000 | 999.900000 | 608.000000 | 255.000000 | 181.000000 | 230.000000 | 160.000000 | 173.000000 | 230.000000 |
In [19]:
# 제너레이션 아이디는 카테고리컬 데이터이므로, 확인
In [20]:
df["generation_id"].nunique()
Out[20]:
7
In [21]:
df["generation_id"].unique()
Out[21]:
array([1, 2, 3, 4, 5, 6, 7], dtype=int64)
In [25]:
# 제너레이션 아이디별로 각각 몇개씩 있는지 확인
Out[25]:
7
In [46]:
df["generation_id"].value_counts()
Out[46]:
5 156 1 151 3 135 4 107 2 100 7 86 6 72 Name: generation_id, dtype: int64
In [30]:
# 위의 결과를 차트로 그리는 방법
In [ ]:
# 특정 컬럼이 카테고리컬 데이터일때,
# 각 value 별로 몇개씩 있는지를 차트로 나타내고 싶을때
# seaborn 의 countplot 함수 사용
In [40]:
sb.color_palette() # 색을 바꾸자 , 칼라의 리스트
Out[40]:
In [44]:
base_color = sb.color_palette()[3]
In [37]:
sb.countplot(data = df, x = 'generation_id' )
plt.savefig("generation.png")
plt.show()
In [47]:
sb.countplot(data = df, x = 'generation_id' , color = base_color )
plt.show()
In [49]:
df["generation_id"].value_counts().index
Out[49]:
Int64Index([5, 1, 3, 4, 2, 7, 6], dtype='int64')
In [50]:
base_order = df["generation_id"].value_counts().index
In [51]:
base_order
Out[51]:
Int64Index([5, 1, 3, 4, 2, 7, 6], dtype='int64')
In [ ]:
In [55]:
sb.countplot(data = df, x = 'generation_id' , color = base_color , order = base_order )
plt.show()
In [62]:
base_order = base_order[ : : -1]
In [63]:
sb.countplot(data = df, x = 'generation_id' , color = base_color , order = base_order )
plt.show()
In [ ]:
# type_1 컬럼이 있습니다. 이컬럼도 카테고리컬 데이터인가요 ?
# 카테고리컬 데이터이면, 각 value별로 몇개씩 데이터가 있는지 차트로 표시
In [64]:
df
Out[64]:
id | species | generation_id | height | weight | base_experience | type_1 | type_2 | hp | attack | defense | speed | special-attack | special-defense | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | bulbasaur | 1 | 0.7 | 6.9 | 64 | grass | poison | 45 | 49 | 49 | 45 | 65 | 65 |
1 | 2 | ivysaur | 1 | 1.0 | 13.0 | 142 | grass | poison | 60 | 62 | 63 | 60 | 80 | 80 |
2 | 3 | venusaur | 1 | 2.0 | 100.0 | 236 | grass | poison | 80 | 82 | 83 | 80 | 100 | 100 |
3 | 4 | charmander | 1 | 0.6 | 8.5 | 62 | fire | NaN | 39 | 52 | 43 | 65 | 60 | 50 |
4 | 5 | charmeleon | 1 | 1.1 | 19.0 | 142 | fire | NaN | 58 | 64 | 58 | 80 | 80 | 65 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
802 | 803 | poipole | 7 | 0.6 | 1.8 | 189 | poison | NaN | 67 | 73 | 67 | 73 | 73 | 67 |
803 | 804 | naganadel | 7 | 3.6 | 150.0 | 243 | poison | dragon | 73 | 73 | 73 | 121 | 127 | 73 |
804 | 805 | stakataka | 7 | 5.5 | 820.0 | 257 | rock | steel | 61 | 131 | 211 | 13 | 53 | 101 |
805 | 806 | blacephalon | 7 | 1.8 | 13.0 | 257 | fire | ghost | 53 | 127 | 53 | 107 | 151 | 79 |
806 | 807 | zeraora | 7 | 1.5 | 44.5 | 270 | electric | NaN | 88 | 112 | 75 | 143 | 102 | 80 |
807 rows × 14 columns
In [66]:
df["type_1"].nunique()
Out[66]:
18
In [67]:
df["type_1"].value_counts()
Out[67]:
water 114 normal 105 grass 78 bug 72 fire 53 psychic 53 rock 46 electric 40 poison 34 ground 32 dark 29 fighting 29 ghost 27 dragon 27 steel 24 ice 23 fairy 18 flying 3 Name: type_1, dtype: int64
In [ ]:
In [78]:
sb.countplot(data = df, x = "type_1" , color= base_color)
plt.show()
In [104]:
sb.color_palette()
Out[104]:
In [105]:
base_color = sb.color_palette()[1]
In [92]:
df["type_1"].value_counts().index
Out[92]:
Index(['water', 'normal', 'grass', 'bug', 'fire', 'psychic', 'rock', 'electric', 'poison', 'ground', 'dark', 'fighting', 'ghost', 'dragon', 'steel', 'ice', 'fairy', 'flying'], dtype='object')
In [98]:
base_order = df["type_1"].value_counts().index
In [100]:
base_order[ : : -1]
Out[100]:
Index(['flying', 'fairy', 'ice', 'steel', 'dragon', 'ghost', 'fighting', 'dark', 'ground', 'poison', 'electric', 'rock', 'psychic', 'fire', 'bug', 'grass', 'normal', 'water'], dtype='object')
In [101]:
base_order = base_order[ : : -1]
In [107]:
# 첫번째 글자가 겹치는 문제 해결
sb.countplot(data = df, x = "type_1" , color= base_color , order = base_order)
plt.xticks(rotation = 60) # 각도 회전
plt.yticks(rotation = 60)
plt.show()
In [114]:
# 두번째 차트의 y축을 활용하는 방법
sb.countplot(data = df, y = "type_1" , color= base_color , order = base_order)
# plt.yticks(rotation = 45)
plt.show()
In [142]:
df['type_1'].nunique()
Out[142]:
18
In [158]:
base_order2 =df['type_1'].value_counts().index[:5+1]
In [143]:
# type_1 의 데이터를 보니까 18개인데
# 데이터수가 많은, 상위 6개만 카운트플롯으로 나타내주세요.
In [159]:
sb.countplot(data = df, y = "type_1", order = base_order2)
plt.show()
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
Pie Charts¶
In [115]:
# 데이터를, 퍼센테이지로 비교해서 보고 싶을때! 사용한다.
In [ ]:
In [116]:
# generation_id 별로 데이터의 갯수를
# 퍼센테이지로 비교할 수 있도록, 파이차트로 나타내라.
In [117]:
df
Out[117]:
id | species | generation_id | height | weight | base_experience | type_1 | type_2 | hp | attack | defense | speed | special-attack | special-defense | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | bulbasaur | 1 | 0.7 | 6.9 | 64 | grass | poison | 45 | 49 | 49 | 45 | 65 | 65 |
1 | 2 | ivysaur | 1 | 1.0 | 13.0 | 142 | grass | poison | 60 | 62 | 63 | 60 | 80 | 80 |
2 | 3 | venusaur | 1 | 2.0 | 100.0 | 236 | grass | poison | 80 | 82 | 83 | 80 | 100 | 100 |
3 | 4 | charmander | 1 | 0.6 | 8.5 | 62 | fire | NaN | 39 | 52 | 43 | 65 | 60 | 50 |
4 | 5 | charmeleon | 1 | 1.1 | 19.0 | 142 | fire | NaN | 58 | 64 | 58 | 80 | 80 | 65 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
802 | 803 | poipole | 7 | 0.6 | 1.8 | 189 | poison | NaN | 67 | 73 | 67 | 73 | 73 | 67 |
803 | 804 | naganadel | 7 | 3.6 | 150.0 | 243 | poison | dragon | 73 | 73 | 73 | 121 | 127 | 73 |
804 | 805 | stakataka | 7 | 5.5 | 820.0 | 257 | rock | steel | 61 | 131 | 211 | 13 | 53 | 101 |
805 | 806 | blacephalon | 7 | 1.8 | 13.0 | 257 | fire | ghost | 53 | 127 | 53 | 107 | 151 | 79 |
806 | 807 | zeraora | 7 | 1.5 | 44.5 | 270 | electric | NaN | 88 | 112 | 75 | 143 | 102 | 80 |
807 rows × 14 columns
In [118]:
df["generation_id"].value_counts()
Out[118]:
5 156 1 151 3 135 4 107 2 100 7 86 6 72 Name: generation_id, dtype: int64
In [120]:
# df로는 파이차트를 바로 그릴수가 없다.
# 따라서, 제너레이션 아이디별로 데이터가 몇개인지 우리가 먼저 구해놓는다.
df2 = df["generation_id"].value_counts()
In [121]:
df2
Out[121]:
5 156 1 151 3 135 4 107 2 100 7 86 6 72 Name: generation_id, dtype: int64
In [124]:
plt.pie(df2)
plt.show()
In [128]:
plt.pie(df2, labels= df2.index)
plt.show()
In [129]:
plt.pie(df2, labels= df2.index , autopct= '%.1f') # %.1f = 소숫점 1자리, %.2f = 소숫점 2자리
plt.show()
In [130]:
plt.pie(df2, labels= df2.index , autopct= '%.1f', startangle= 90) # startangle = 시작지점
plt.show()
In [132]:
plt.pie(df2, labels= df2.index , autopct= '%.1f', startangle= 90,
wedgeprops = {'width' : 0.7}) # 구멍
plt.show()
In [135]:
plt.pie(df2, labels= df2.index , autopct= '%.1f', startangle= 90,
wedgeprops = {'width' : 0.7})
plt.title("Generation id pie chart") # 한글사용은 추후 설명
plt.show()
In [140]:
plt.pie(df2, labels= df2.index , autopct= '%.1f', startangle= 90,
wedgeprops = {'width' : 0.7})
plt.title("Generation id pie chart")
plt.legend()
plt.show()
In [141]:
plt.pie(df2, labels= df2.index , autopct= '%.1f', startangle= 90,
wedgeprops = {'width' : 0.7})
plt.title("Generation id pie chart")
plt.legend(['id 5', 'id 1','id 3', 'id 4', 'id 2', 'id 6', 'id 7'])
plt.show()
In [ ]:
In [144]:
# 제너레이션 아이디값이 총 7 개인데,
# 데이터가 많은, 상위 4개만 파이차트로 나타내시오.
In [162]:
df["generation_id"].nunique()
Out[162]:
7
In [165]:
df3 = df["generation_id"].value_counts()
In [192]:
df3.head(4)
Out[192]:
5 156 1 151 3 135 4 107 Name: generation_id, dtype: int64
In [195]:
df3_head4 = df3.head(4)
In [213]:
plt.pie(df3_head4 , labels= df3_head4.index , autopct= "%.2f" , startangle= 90,
wedgeprops= {"width" : 0.8})
plt.legend(["id 1", "id 2", 'id 3', 'id 4'])
plt.show()