220914
In [ ]:
#제곱근 구하는 첫 x는 input (x)는 아웃풋
import math
makesqr= lambda x: math.sqrt(x)
num = int(input('수 입력>>>'))
print(f'{num}의 제곱근은 {makesqr(num)}이다')
수 입력>>>5
5의 제곱근은 2.23606797749979이다
filter()함수 처리된 각각의 요로에 대하여 boolian값 반환 트루를 반환하면 그요소는 남게 되고 펄스를 반환하면 그요소는 제거 결국 주어진 조건을 만족하는 요소를 구히는 함수
In [ ]:
import random
import numpy as np
In [ ]:
np.random.seed(0) # seed (0) 은 랜덤함수를 썻지만 같은수만 출력할때 사용
a= np.random.randint(50,100,10)
filtereven= lambda x: x % 2==0
even =list(filter(filtereven, a))
even
Out[ ]:
[94, 50, 86]
넘파이랑 리스트의연산속도 차이 비교
In [ ]:
st1 = ['20201234','홍길동',22,[4.3,4.0,3.9],'융합전공']
In [ ]:
arr= np.array([2,4,5.0,-4,8,-7])
arr
Out[ ]:
array([ 2., 4., 5., -4., 8., -7.])
In [ ]:
numlist= [x for x in range(10000)] #리스트 함축
numarray= np.arange(10000)
In [ ]:
%time for i in range(10):numlist2=[x*2 for x in numlist]
CPU times: user 6.12 ms, sys: 1.02 ms, total: 7.14 ms
Wall time: 7.11 ms
In [ ]:
%time for i in range(10): numarray2=numarray*2
CPU times: user 128 µs, sys: 1.03 ms, total: 1.16 ms
Wall time: 1.47 ms
In [ ]:
np.random.seed(0)
score= np.random.randint(50,100,size=(5,3))
score
Out[ ]:
array([[94, 97, 50],
[53, 53, 89],
[59, 69, 71],
[86, 73, 56],
[74, 74, 62]])
In [ ]:
tot=np.sum(score)
avg=np.mean(score)
#과목별 총점 평균
totsub= np.sum(score, axis=0)
avgsub= np.mean(score, axis=0)
print(f'과목별 총점 {tot}, 과목별 평균{avg}')
maxsub= np.max(score, axis=0)
minsub= np.min(score, axis=0)
print(f'최고점수 {tot}, 최저점수{avg}')
과목별 총점 1060, 과목별 평균70.66666666666667
최고점수 1060, 최저점수70.66666666666667
넘파이 자료의 시각화
In [ ]:
#라인그래프
import numpy as np
import matplotlib.pyplot as plt
x=np.array([1,2,3,4,5])
y=np.array([53, 53, 89,74, 62])
plt.plot(x,y)
plt.show()
In [ ]:
#라인그래프
import numpy as np
import matplotlib.pyplot as plt
x=np.array([1,2,3,4,5])
y=np.array([53, 53, 89,74, 62])
plt.plot(x,y,'m:o')
#x축 눈금지정
plt.xticks(x)
plt.show()
In [ ]:
#라인그래프
import numpy as np
import matplotlib.pyplot as plt
# 한글 글꼴 지정
plt.rcParams['font.family'] = 'NanumGothic'
x=np.array([1,2,3,4,5])
y1=np.array([53, 100, 89,74, 62])
y2=np.array([53, 89, 74,53, 53])
plt.plot(x,y1,'m:o',label='세용')
plt.plot(x,y2,'m:o',label='형준')
#x축 눈금지정
plt.xticks(x)
plt.title('시험성적')
plt.xlabel('시험횟수')
plt.ylabel('grade')
plt.show()
WARNING:matplotlib.font_manager:findfont: Font family ['NanumGothic'] not found. Falling back to DejaVu Sans.
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:214: RuntimeWarning: Glyph 49884 missing from current font.
font.set_text(s, 0.0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:214: RuntimeWarning: Glyph 54744 missing from current font.
font.set_text(s, 0.0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:214: RuntimeWarning: Glyph 49457 missing from current font.
font.set_text(s, 0.0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:214: RuntimeWarning: Glyph 51201 missing from current font.
font.set_text(s, 0.0, flags=flags)
WARNING:matplotlib.font_manager:findfont: Font family ['NanumGothic'] not found. Falling back to DejaVu Sans.
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:214: RuntimeWarning: Glyph 54943 missing from current font.
font.set_text(s, 0.0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:214: RuntimeWarning: Glyph 49688 missing from current font.
font.set_text(s, 0.0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:183: RuntimeWarning: Glyph 49884 missing from current font.
font.set_text(s, 0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:183: RuntimeWarning: Glyph 54744 missing from current font.
font.set_text(s, 0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:183: RuntimeWarning: Glyph 54943 missing from current font.
font.set_text(s, 0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:183: RuntimeWarning: Glyph 49688 missing from current font.
font.set_text(s, 0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:183: RuntimeWarning: Glyph 49457 missing from current font.
font.set_text(s, 0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:183: RuntimeWarning: Glyph 51201 missing from current font.
font.set_text(s, 0, flags=flags)
텍스트 폰트 설치 라이브러리
In [ ]:
#구글 코렙 서버에 한글 폰트 설치
!sudo apt-get install -y fonts-name
!sudo fc-cache -fv
!rm -/.cache/matplotlib -rf
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package fonts-name
/usr/share/fonts: caching, new cache contents: 0 fonts, 1 dirs
/usr/share/fonts/truetype: caching, new cache contents: 0 fonts, 2 dirs
/usr/share/fonts/truetype/humor-sans: caching, new cache contents: 1 fonts, 0 dirs
/usr/share/fonts/truetype/liberation: caching, new cache contents: 16 fonts, 0 dirs
/usr/local/share/fonts: caching, new cache contents: 0 fonts, 0 dirs
/root/.local/share/fonts: skipping, no such directory
/root/.fonts: skipping, no such directory
/var/cache/fontconfig: cleaning cache directory
/root/.cache/fontconfig: not cleaning non-existent cache directory
/root/.fontconfig: not cleaning non-existent cache directory
fc-cache: succeeded
rm: invalid option -- '/'
Try 'rm --help' for more information.
[런타임] ->[런타임 다시 시작]
In [ ]:
#설치된 한글 폰트 확인방법
import matplotlib.font_manager as fm
#구글 코렙 서버에 설치된 모든 글꼴확인
sysFonts=fm.findSystemFonts()
#글꼴 중에서 나눔 폰트만 찾기
nanumFonts =[f for f in sysFonts if 'Nanum' in f]
nanumFonts
Out[ ]:
[]
In [ ]:
nanumFonts =[f for f in sysFonts ]
nanumFonts
Out[ ]:
['/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf',
'/usr/share/fonts/truetype/liberation/LiberationMono-BoldItalic.ttf',
'/usr/share/fonts/truetype/liberation/LiberationSansNarrow-Regular.ttf',
'/usr/share/fonts/truetype/liberation/LiberationMono-Regular.ttf',
'/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf',
'/usr/share/fonts/truetype/liberation/LiberationSans-BoldItalic.ttf',
'/usr/share/fonts/truetype/liberation/LiberationSerif-BoldItalic.ttf',
'/usr/share/fonts/truetype/liberation/LiberationSerif-Italic.ttf',
'/usr/share/fonts/truetype/liberation/LiberationSansNarrow-Italic.ttf',
'/usr/share/fonts/truetype/liberation/LiberationSerif-Bold.ttf',
'/usr/share/fonts/truetype/humor-sans/Humor-Sans.ttf',
'/usr/share/fonts/truetype/liberation/LiberationSansNarrow-BoldItalic.ttf',
'/usr/share/fonts/truetype/liberation/LiberationMono-Italic.ttf',
'/usr/share/fonts/truetype/liberation/LiberationSans-Italic.ttf',
'/usr/share/fonts/truetype/liberation/LiberationSerif-Regular.ttf',
'/usr/share/fonts/truetype/liberation/LiberationMono-Bold.ttf',
'/usr/share/fonts/truetype/liberation/LiberationSansNarrow-Bold.ttf']
In [66]:
import numpy as nnp
import numpy.random as rdn
import matplotlib.pyplot as plt
st= np.arange(10)
stname=['kim','kang','hwang','sim','joo','son','park','win','jim','tee']
score1= rdn.randint(50,101,10)
score2= rdn.randint(50,101,10)
plt.xticks(st,stname)
plt.bar(st,score1)
for i in st:
plt.text(i-0.2, score1[i]-5,score1[i])
plt.show()
In [68]:
import numpy as nnp
import numpy.random as rdn
import matplotlib.pyplot as plt
st= np.arange(10)
stname=['kim','kang','hwang','sim','joo','son','park','win','jim','tee']
score1= rdn.randint(50,101,10)
score2= rdn.randint(50,101,10)
plt.xticks(st,stname)
plt.bar(st,score1)
plt.bar(st,score2)
'''
for i in st:
plt.text(i-0.2, score1[i]-5,score1[i])
'''
plt.show()
In [98]:
import numpy as nnp
import numpy.random as rdn
import matplotlib.pyplot as plt
st= np.arange(10)
stname=['kim','kang','hwang','sim','joo','son','park','win','jim','tee']
score1= rdn.randint(50,101,10)
score2= rdn.randint(50,101,10)
plt.figure(figsize=(10,5))
plt.title('중간기말 시험결과')
plt.xlabel('학생')
plt.xlabel('성적')
plt.xticks(st,stname)
plt.bar(st-0.2,score1, width=0.4, edgecolor='yellowgreen',alpha=0.5)
plt.bar(st+0.2,score2,width=0.4,edgecolor='k',alpha=0.5)
#범례 표시
plt.legend()
for i in st:
plt.text(i-0.3, score1[i]-5,score1[i])
plt.text(i+0.1, score2[i]-5,score2[i])
plt.show()
plt.savefig('0914_grade.png')
WARNING:matplotlib.legend:No handles with labels found to put in legend.
<Figure size 432x288 with 0 Axes>
In [91]:
#구글 드라이브랑 코렙 연동
from google.colab import drive
drive.mount('/gdrive',force_remount= True)
Mounted at /gdrive
In [108]:
import matplotlib.pyplot as plt
plt.rc('font', family='NanumGothic')
'''
plt.rcParams['font.family'] = 'NanumGothic'
plt.rcParams['font.size'] = 11'''
지출항목 =['통신비','교통비','식비','문화비','의류비',' 보험비']
지출금액=[56000,100000,200000,100000,5000,100000]
plt.pie(지출금액,labels=지출항목,startangle=90,autopct='%.1f%%',counterclock=False,explode=(0,0,0.1,0,0,0),shadow=True)
plt.show()
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:214: RuntimeWarning: Glyph 53685 missing from current font.
font.set_text(s, 0.0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:214: RuntimeWarning: Glyph 49888 missing from current font.
font.set_text(s, 0.0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:214: RuntimeWarning: Glyph 48708 missing from current font.
font.set_text(s, 0.0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:214: RuntimeWarning: Glyph 44368 missing from current font.
font.set_text(s, 0.0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:214: RuntimeWarning: Glyph 49885 missing from current font.
font.set_text(s, 0.0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:214: RuntimeWarning: Glyph 47928 missing from current font.
font.set_text(s, 0.0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:214: RuntimeWarning: Glyph 54868 missing from current font.
font.set_text(s, 0.0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:214: RuntimeWarning: Glyph 51032 missing from current font.
font.set_text(s, 0.0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:214: RuntimeWarning: Glyph 47448 missing from current font.
font.set_text(s, 0.0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:214: RuntimeWarning: Glyph 48372 missing from current font.
font.set_text(s, 0.0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:214: RuntimeWarning: Glyph 54744 missing from current font.
font.set_text(s, 0.0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:183: RuntimeWarning: Glyph 53685 missing from current font.
font.set_text(s, 0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:183: RuntimeWarning: Glyph 49888 missing from current font.
font.set_text(s, 0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:183: RuntimeWarning: Glyph 48708 missing from current font.
font.set_text(s, 0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:183: RuntimeWarning: Glyph 44368 missing from current font.
font.set_text(s, 0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:183: RuntimeWarning: Glyph 49885 missing from current font.
font.set_text(s, 0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:183: RuntimeWarning: Glyph 47928 missing from current font.
font.set_text(s, 0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:183: RuntimeWarning: Glyph 54868 missing from current font.
font.set_text(s, 0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:183: RuntimeWarning: Glyph 51032 missing from current font.
font.set_text(s, 0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:183: RuntimeWarning: Glyph 47448 missing from current font.
font.set_text(s, 0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:183: RuntimeWarning: Glyph 48372 missing from current font.
font.set_text(s, 0, flags=flags)
/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py:183: RuntimeWarning: Glyph 54744 missing from current font.
font.set_text(s, 0, flags=flags)
In [104]:
plt.rc('font', family='NanumGothic') # For Windows
print(plt.rcParams['font.family'])
['NanumGothic']
In [ ]:
In [ ]:
'파이썬 기본 > 그래프 기초' 카테고리의 다른 글
기본 그래프 그려보기 (0) | 2022.10.25 |
---|