30분 요약 강좌 시즌2 : Python 활용편-섹션3. Visualization
2021. 9. 5. 17:57ㆍ빅데이터 스터디
3.Matplotlib¶
In [2]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [4]:
%matplotlib inline #inline에 표시(최신버전은 기본)
In [7]:
x = [100,200,300]
y = [1,2,3]
plt.plot(x,y)
Out[7]:
[<matplotlib.lines.Line2D at 0x128c72be4f0>]
In [11]:
#Series로 구현
value = pd.Series([1,2,3],[100,200,300])
plt.plot(value)
Out[11]:
[<matplotlib.lines.Line2D at 0x128c74a17f0>]
In [18]:
x = [100,200,300]
y = [1,2,3]
plt.plot(x,y,':r',linewidth =10) #빨간색 점선 굵기 10
Out[18]:
[<matplotlib.lines.Line2D at 0x128c89d9e50>]
In [25]:
x = [100,200,300]
y = [1,2,3]
plt.plot(x,y, color= '#ff0000',linewidth = 10)
plt.title('hello world',fontsize = 20) #제목
plt.xlabel('hello',fontsize = 10) #x라벨
plt.ylabel(' world',fontsize =10) #y라벨
Out[25]:
Text(0, 0.5, ' world')
In [27]:
x = [100,200,300]
y = [1,2,3]
plt.plot(x,y, color= '#ff0000',linewidth = 10)
plt.title('hello world',fontsize = 20) #제목
plt.xlabel('hello',fontsize = 10) #x라벨
plt.ylabel(' world',fontsize =10) #y라벨
plt.savefig('sample.png') #이미지로 저장
In [36]:
x = np.linspace(0,10,100)
y = np.sin(x)
y_ =np.cos(x)
plt.plot(x,y,label = 'sin')
plt.plot(x,y_,'-o',label = 'cos')
plt.legend(loc = 3) #라벨표시
Out[36]:
<matplotlib.legend.Legend at 0x128c8fdca90>
scatter¶
In [40]:
x = np.linspace(0,10,20)
y = x**2
plt.scatter(x,y,c = 'r', alpha = 0.5)
plt.show() #그래프 표시
histogram¶
In [51]:
x = [np.random.randint(1,7) for _ in range(100000)]
In [57]:
plt.hist(x, bins = 6)
plt.show()
Pie chart¶
In [60]:
labels = ['one','two','three']
size = [100,20,10]
plt.pie(size,labels = labels)
Out[60]:
([<matplotlib.patches.Wedge at 0x128ca1b5370>, <matplotlib.patches.Wedge at 0x128cb5b5d00>, <matplotlib.patches.Wedge at 0x128ca1b5c40>], [Text(-0.8233618545088073, 0.7294348884854754, 'one'), Text(0.6248713159427525, -0.905282187227813, 'two'), Text(1.0680360337692223, -0.2632470903362633, 'three')])
In [63]:
labels = ['one','two','three']
size = [100,20,10]
plt.pie(size,labels = labels, shadow = True) #그림자 생성
Out[63]:
([<matplotlib.patches.Wedge at 0x128cb575be0>, <matplotlib.patches.Wedge at 0x128cb5758e0>, <matplotlib.patches.Wedge at 0x128cb549460>], [Text(-0.8233618545088073, 0.7294348884854754, 'one'), Text(0.6248713159427525, -0.905282187227813, 'two'), Text(1.0680360337692223, -0.2632470903362633, 'three')])
In [65]:
labels = ['one','two','three']
size = [100,20,10]
plt.pie(size,labels = labels, shadow = True,autopct = '%1.2f%%') #퍼센트생성
Out[65]:
([<matplotlib.patches.Wedge at 0x128cb344130>, <matplotlib.patches.Wedge at 0x128cb2e28e0>, <matplotlib.patches.Wedge at 0x128cb2e2af0>], [Text(-0.8233618545088073, 0.7294348884854754, 'one'), Text(0.6248713159427525, -0.905282187227813, 'two'), Text(1.0680360337692223, -0.2632470903362633, 'three')], [Text(-0.44910646609571303, 0.397873575537532, '76.92%'), Text(0.34083889960513764, -0.4937902839424434, '15.38%'), Text(0.5825651093286667, -0.14358932200159816, '7.69%')])
Bar Chart¶
In [70]:
plt.bar(['one','two','three'],[10,20,30])
plt.show()
In [71]:
plt.barh(['one','two','three'],[10,20,30]) #가로 바 그래프
plt.show()
4.Plotly¶
In [80]:
import plotly.express as px #확장성이 더 좋다
x_ = np.array([1,2,3,4,5])
y_ = x**2
fig = px.line(x = x_,y = y_)
fig.show()
In [83]:
import plotly.graph_objects as go
korea_life = px.data.gapminder().query("country == 'Korea, Rep.'")
fig = px.line(korea_life, x = 'year',y = 'lifeExp',title = 'Life expectancy in Korea')
fig.show()
In [87]:
korea_life['year']
korea_life['lifeExp']
Out[87]:
840 47.453 841 52.681 842 55.292 843 57.716 844 62.612 845 64.766 846 67.123 847 69.810 848 72.244 849 74.647 850 77.045 851 78.623 Name: lifeExp, dtype: float64
In [91]:
korea_gdp = px.data.gapminder().query("country == 'Korea, Rep.'")
fig = px.bar(korea_gdp, x= 'year',y= 'gdpPercap',title = '한국인 GDP')
fig.show()
In [94]:
korea_gdp = px.data.gapminder().query("country == 'Korea, Rep.'")
fig = px.scatter(korea_gdp, x= 'year',y= 'gdpPercap',title = '한국인 GDP')
fig.show()
#산점도
이미지 분석¶
In [97]:
from skimage import io
#from PIL import Image
#from cv2
In [102]:
jeju = io.imread('jeju.jpg')
type(jeju)
Out[102]:
numpy.ndarray
In [104]:
jeju.shape
Out[104]:
(853, 1280, 3)
In [107]:
jeju
Out[107]:
array([[[ 0, 48, 110], [ 3, 51, 113], [ 2, 50, 112], ..., [ 8, 23, 56], [ 10, 25, 58], [ 12, 27, 60]], [[ 0, 47, 109], [ 1, 49, 111], [ 0, 48, 110], ..., [ 7, 22, 55], [ 9, 24, 57], [ 11, 26, 59]], [[ 0, 47, 109], [ 0, 48, 110], [ 0, 48, 110], ..., [ 6, 21, 54], [ 8, 23, 56], [ 10, 25, 58]], ..., [[ 14, 67, 119], [ 13, 66, 118], [ 11, 64, 116], ..., [ 15, 36, 65], [ 14, 35, 64], [ 11, 32, 61]], [[ 15, 68, 120], [ 13, 66, 118], [ 11, 64, 116], ..., [ 15, 36, 65], [ 15, 36, 65], [ 13, 34, 63]], [[ 16, 69, 121], [ 13, 66, 118], [ 11, 64, 116], ..., [ 15, 36, 65], [ 16, 37, 66], [ 15, 36, 65]]], dtype=uint8)
In [109]:
np.min(jeju),np.max(jeju)
Out[109]:
(0, 255)
In [111]:
plt.imshow(jeju)
Out[111]:
<matplotlib.image.AxesImage at 0x128ecc02ca0>
In [113]:
plt.imshow(jeju[::-1]) #상하 좌우 반전
Out[113]:
<matplotlib.image.AxesImage at 0x128ed685370>
In [115]:
plt.imshow(jeju[:,::-1]) #좌우 반전
Out[115]:
<matplotlib.image.AxesImage at 0x128eddc6af0>
In [117]:
plt.imshow(jeju[::-1,:]) #상하반전
Out[117]:
<matplotlib.image.AxesImage at 0x128ee500ee0>
In [131]:
plt.imshow(jeju[100:660,100:1200])
#이미지 자르기
Out[131]:
<matplotlib.image.AxesImage at 0x128f0de9d30>
In [135]:
plt.imshow(jeju[::3,::3]) #3칸씩 건너뜀
Out[135]:
<matplotlib.image.AxesImage at 0x128f118cdc0>
In [137]:
plt.imshow(jeju[::10,::10]) #10칸씩 건너뜀 픽셀 깨짐
Out[137]:
<matplotlib.image.AxesImage at 0x128f1266dc0>
In [138]:
minijeju = jeju[::10,::10]
In [140]:
plt.hist(minijeju.ravel(),256,[0,256])
plt.show()
In [143]:
#특정값 이하 편집
minijeju_= np.where(minijeju<50,minijeju,0)
#minijeju가 50 보다 작으면 minijeju 그대로 아니면 0
plt.imshow(minijeju_)
Out[143]:
<matplotlib.image.AxesImage at 0x128f12c1280>
In [144]:
#색상변경
plt.imshow(jeju[:,:,0])
Out[144]:
<matplotlib.image.AxesImage at 0x128f151b070>
In [147]:
plt.imshow(jeju[:,:,1])
Out[147]:
<matplotlib.image.AxesImage at 0x128f17f8d00>
In [151]:
plt.imshow(jeju[:,:,2])
Out[151]:
<matplotlib.image.AxesImage at 0x128f1c9f9d0>
In [155]:
#회색 계열 변경
from skimage import color
plt.imshow(color.rgb2gray(jeju),cmap=plt.cm.gray)
Out[155]:
<matplotlib.image.AxesImage at 0x128f2e9f370>
'빅데이터 스터디' 카테고리의 다른 글
30분 요약 강좌 시즌2 : Python 활용편 섹션4-웹크롤링 연습문제-1 (0) | 2021.09.17 |
---|---|
30분 요약 강좌 시즌2 : Python 활용편 섹션4-웹크롤링 (0) | 2021.09.17 |
30분 요약 강좌 시즌2 : Python 활용편 - 섹션3. Numpy와 Pandas (0) | 2021.09.05 |
30분 요약 강좌 시즌2 : Python 활용편 - 섹션 2. Python-2 (0) | 2021.09.05 |
30분 요약 강좌 시즌2 : Python 활용편 - 섹션 2. Python-1 (0) | 2021.09.05 |