matplotlib 이어서 진행
우선 오류메시지 지우고 시작
# warning메시지 무시
import warnings
warnings.filterwarnings('ignore')
# 한글폰트 설치하기 위해 필요한 모듈
import matplotlib.font_manager as fm
!apt install fonts-nanum
fm.fontManager.addfont('/usr/share/fonts/truetype/nanum/NanumBarunGothic.ttf')
plt.rcParams['font.family'] = "NanumBarunGothic"
3-6.color
import matplotlib.pyplot as plt
x = np.arange(-np.pi, np.pi, 0.02)
y1 = np.sin(x)
y2 = np.cos(x)
fig, ax = plt.subplots()
ax.plot(x, y1, label = 'sin', color= (0.1, 0.3, 0.5)) # RGB
ax.plot(x, y2, label = 'cos', color='c') # one of {'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'} or blue, green이렇게도 가능함
ax.legend(loc='upper right')
plt.show()
3-7. facecolor
#ax.set_facecolor()
x = np.arange(-np.pi, np.pi, 0.02)
y1 = np.sin(x)
y2 = np.cos(x)
fig,axs = plt.subplots(1,2)
axs[0].plot(x, y1)
axs[1].plot(x, y2)
axs[0].set_title('sin')
axs[1].set_title('cos')
fig.suptitle('삼각함수')
axs[0].set_xlabel('x값', ha='left', va = 'top')
axs[1].set_xlabel('x값')
axs[0].set_ylabel('sin값')
axs[1].set_ylabel('cos값')
axs[0].set_facecolor('pink')
axs[1].set_facecolor('skyblue')
fig.set_facecolor("g")
plt.show()
3-8. grid
격자점 생성
x = np.arange(-np.pi, np.pi, 0.02)
y1 = np.sin(x)
y2 = np.cos(x)
fig, ax = plt.subplots()
ax.plot(x, y1, label = 'sin')
ax.plot(x, y2, label = 'cos')
ax.legend(loc='upper right')
ax.grid(color='r', linestyle='--', linewidth=0.5)
plt.show()
5.여러가지 그래프
5-1.Line Plot
Line Plot (선 그래프): 데이터 포인트를 선으로 연결하여 시간에 따른 변화나 추세를 시각화
x = np.arange(-5, 5, 0.5)
y1 = x
y2 = x+2
y3 = x+4
y4 = x+6
fig, ax = plt.subplots()
ax.plot(x, y1)
ax.plot(x, y2, marker='s',color='g',linestyle='dotted')
ax.plot(x, y3, color='k')
ax.plot(x, y4, linestyle='dotted')
plt.show()
5-2.Bar Plot
Bar Plot: 각 카테고리의 값을 막대의 길이로 표현하여 비교
fruits = {'멸치': 25, '고등어': 17, '갈치': 5, '새우': 20}
names = list(fruits.keys())
values = list(fruits.values())
fig, ax = plt.subplots()
ax.bar(names, values)
plt.show()
fruits = {'멸치': 25, '고등어': 17, '갈치': 5, '새우': 20}
names = list(fruits.keys())
values = list(fruits.values())
fig, ax = plt.subplots()
ax.barh(names, values)
plt.show()
labels = ['픽미업', '화산귀환', '전독시', '로스트아크']
user = [8.2, 9.4, 6.6, 9.16]
critic = [5.4, 8, 5.5, 7.17]
fig, ax = plt.subplots()
ax.bar(labels, user, color='g')
ax.bar(labels, critic, color='r')
plt.show()
labels = ['픽미업', '화산귀환', '전독시', '로스트아크']
user = [8.2, 9.4, 6.6, 9.16]
critic = [5.4, 8, 5.5, 7.17]
fig, ax = plt.subplots()
x = np.arange(len(labels)) # the label locations
width=0.35
ax1=ax.bar(x-width/2, user, width, color='skyblue')
ax2=ax.bar(x+width/2, critic, width, color='k')
plt.show()
labels = ['픽미업', '화산귀환', '전독시', '로스트아크']
user = [9.2, 9.4, 8.6, 9.16]
critic = [5.4, 8, 5.5, 7.17]
fig, ax = plt.subplots()
x = np.arange(len(labels)) # the label locations
width=0.35
ax.bar(x-width/2, user, width, color='skyblue')
ax.bar(x+width/2, critic, width, color='g')
ax.legend(['관객평점', '전문가평점'], loc='upper right')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.set_ylim([0, 12])
ax.set_title('오래된 영화 평점', fontsize=20, color = 'b')
plt.show()
5-3.Histogram
- Histogram: 데이터의 분포를 시각화하기 위해 값을 여러 구간(bin)으로 나누어 각 구간의 빈도를 막대로 표시함
- 구간별 크기를 가지고 차트를 그려서, 데이터 분류별로 데이터 값이 어떻게 되느냐를 알 수 있다.
# [0, 1) 범위에서 균일한 분포를 갖는 난수 10000개 생성
data = np.random.rand(10000)
fig, ax = plt.subplots()
ax.hist(data, bins = 10, facecolor='g') # 1만개의 데이터를 10개로 나눠서 그림
ax.grid(True)
plt.show()
mu, sigma = 100, 5
x = mu + sigma * np.random.randn(1000)
fig, ax = plt.subplots()
ax.hist(x, 10, density=True, facecolor='skyblue', histtype='barstacked')
plt.show()
5-4.Scatter Plot
- Scatter Plot: 데이터 점들을 x축과 y축에 따라 흩어지게 배치하여 두 변수 간의 관계를 시각화
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
area = (20 * np.random.rand(N))**2
fig, ax = plt.subplots()
ax.scatter(x, y, s=area, marker='o', c=area) # 넓이 크기만큼 색상이 바뀌게함
plt.show()
5-5.heatmap
- 히트맵(heatmap)은 데이터를 2차원 행렬로 표현하여 각 셀의 색깔을 통해 값의 크기를 시각적으로 나타내는 그래프
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# 데이터 설정
vegetables = ["cucumber", "tomato", "lettuce", "asparagus", "potato", "wheat", "barley"]
farmers = ["Farmer Joe", "Upland Bros.", "Smith Gardening", "Agrifun", "Organiculture", "BioGoods Ltd.", "Cornylee Corp."]
harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
# 플롯 생성
fig, ax = plt.subplots()
im = ax.imshow(harvest)
# 축 설정
ax.set_xticks(np.arange(len(farmers)))
ax.set_yticks(np.arange(len(vegetables)))
ax.set_xticklabels(farmers)
ax.set_yticklabels(vegetables)
# 틱 레이블 회전 및 정렬
plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")
# 데이터 값 주석 추가
for i in range(len(vegetables)):
for j in range(len(farmers)):
text = ax.text(j, i, harvest[i, j], ha="center", va="center", color="w")
# 제목 및 레이아웃 설정
ax.set_title("Harvest of local farmers (in tons/year)")
fig.tight_layout()
plt.show()
harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
fig, ax = plt.subplots()
ax.imshow(harvest)
plt.show()
6.저장 (savefig)
!pwd로 현재 위치 확인
savefile_path = './scatter.jpg'
print(savefile_path)
fig.savefig(savefile_path)
./scatter.jpg
mpimg 모듈 이용해서 저장 및 복원
imread로 읽으면 색상, 점 같은것들을 데이터로 바꿔서 저장함( numpy.ndarray로 저장됨)
import matplotlib.image as mpimg
img = mpimg.imread(savefile_path)
print(type(img))
print(img)
plt.imshow(img)
<class 'numpy.ndarray'>
데이터들 나옴
<matplotlib.image.AxesImage at 0x7c945c73d6f0>
pilimg로 받아오면 이미지 자체를 읽어옴
import PIL.Image as pilimg
img = pilimg.open(savefile_path)
print(type(img))
print(img)
img
<class 'PIL.JpegImagePlugin.JpegImageFile'>
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=600x600 at 0x7C0EFCEDECE0>
- csv 파일을 읽어서 그 데이터를 그래프로 그려보자
csv데이터는 세종시 cctv데이터 이용함
# cctv = pd.read_csv('./CCTV_20190920.csv', encoding='cp949')
cctv = pd.read_csv('세종cctv 20190920 csv 데이터', encoding='cp949')
cctv.head()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 965 entries, 0 to 964
Data columns (total 13 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 관리기관명 965 non-null object
1 소재지도로명주소 389 non-null object
2 소재지지번주소 791 non-null object
3 설치목적구분 965 non-null object
4 카메라대수 965 non-null int64
5 카메라화소수 965 non-null int64
6 촬영방면정보 882 non-null object
7 보관일수 965 non-null int64
8 설치년월 0 non-null float64
9 관리기관전화번호 965 non-null object
10 위도 965 non-null float64
11 경도 965 non-null float64
12 데이터기준일자 965 non-null object
dtypes: float64(3), int64(3), object(7)
memory usage: 98.1+ KB
기본정보 확인
cctv.describe()
# x축은 카메라 화소수, y축은 카메라 대수
# 카메라화소수가 얼마얼마 있는지 unique한 값을 찾아서 오름차순으로 정렬 ==> x
array([200, 41, 130, 40, 300])
cctv['카메라화소수'].value_counts()
200 707
130 133
41 122
40 2
300 1
Name: 카메라화소수, dtype: int64
data = cctv['카메라화소수'].value_counts().sort_index()
data
40 2
41 122
130 133
200 707
300 1
Name: 카메라화소수, dtype: int64
그래프 그리기 위해 x, y로 나눔
Int64Index([40, 41, 130, 200, 300], dtype='int64')
array([ 2, 122, 133, 707, 1])
fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()
- x는 0부터 400까지, y는 0부터 1000까지로 그래프 범위를 지정
fig, axs = plt.subplots()
axs.plot(data.index, data.values)
axs.set_xlim([0, 400])
axs.set_ylim([0, 1000])
plt.show()
화소 수를 100만 단위로 Tick을 지정
fig, axs = plt.subplots()
axs.plot(data.index, data.values)
axs.set_xlim([0,400])
axs.set_ylim([0,1000])
axs.set_xticks([0,100,200,300,400])
axs.set_xticklabels(['0화소','100만화소','200만화소','300만화소','400만화소'])
plt.show()
fig, axs = plt.subplots()
axs.plot(data.index, data.values, label='카메라 화소')
# title
fig.suptitle('화소')
axs.set_title('카메라화소')
# xlim, ylim
axs.set_xlim([0, 400])
axs.set_ylim([0, 1000])
# xticks, xticklabels
axs.set_xticks([0,100,200,300,400])
axs.set_xticklabels(['0화소','1백만화소','2백만화소','3백만화소','4백만화소'])
# legend
axs.legend(loc='upper right')
# xlabel, ylabel
axs.set_xlabel('CCTV 화소수')
axs.set_ylabel('CCTV 대수')
plt.show()
fig, ax = plt.subplots()
ax.plot(data.index, data.values, label='카메라 화소')
ax.set_xlim([0, 400])
ax.set_ylim([0, 1000])
ax.set_xticks([0,100,200,300,400])
ax.set_xticklabels(['0화소','1백만화소','2백만화소','3백만화소','4백만화소'])
ax.legend(loc='upper right')
ax.set_xlabel('CCTV 화소수')
ax.set_ylabel('CCTV 대수')
for x_, y_ in zip(data.index, data.values):
ax.text(x_+1, y_, '%d대'%(int(y_)))
plt.show()
for x_, y_ in zip(data.index, data.values):
axs.text(x_+1, y_,'(%d, %d)' % (int(x_), int(y_)))
이렇게 넣어주면 좌표값이 나오게 됨
그래프에 가장 많은 부분에 피크를 달아줌(annotation이 있는지 찾아보고 추가로 달기)
# 707대 위치에 '가장 많음'이라고 달기
fig, ax = plt.subplots()
ax.plot(data.index, data.values, label='카메라 화소')
ax.set_xlim([0, 400])
ax.set_ylim([0, 1000])
ax.set_xticks([0,100,200,300,400])
ax.set_xticklabels(['0화소','1백만화소','2백만화소','3백만화소','4백만화소'])
ax.legend(loc='upper right')
ax.set_xlabel('CCTV 화소수')
ax.set_ylabel('CCTV 대수')
for x_, y_ in zip(data.index, data.values):
ax.text(x_+1, y_, '%d대'%(int(y_)))
ax.annotate('가장 많음', xy=(200,707), xytext=(100,700),arrowprops=dict(facecolor='red'))
plt.show()
그래프 색 바꾸기
ax.plot(data.index, data.values, label='카메라 화소', color='r')
ax.annotate('가장 많음', xy=(200,707), xytext=(100,700),arrowprops=dict(facecolor='blue'))
이 두개 바꿔서 색 변경 가능
그래프의 배경색상 바꾸기
ax.set_facecolor('c')
fig.set_facecolor('y')
를 추가해서 출력
그래프에 그리드(grid)를 넣기
ax.set_facecolor('cyan')
fig.set_facecolor('pink')
ax.grid(color='r', linestyle='--', linewidth=1)
plt.show()
이렇게 넣어서 그리드 + 배경 색 변경
7. 지도에 표시해보기
mymap = folium.Map(location=[36.6208541,127.2849716], zoom_start=13) # 위도, 경도, 축척
mymap
홍대 위, 경도 를 ㄹ기준으로 한것임
folium.Marker([36.6208541,127.2849716], popup="Hongik Univ").add_to(mymap)
mymap
CCTV 위도 경도 리스트 작성
그 리스트의 벨류만 가져와서 loc안에 다 넣은다음 마커 지정( 튜플로 다 들어왔을 것임)
for loc in cctv[['위도','경도']].values:
folium.Marker(loc).add_to(mymap)
다른 데이터 : 대전 신호등 설치 데이터
cp949는 ms 윈도우에서 많이 사용되는 데이터
light = pd.read_csv(' 대전 신호등 설치 데이터.csv', encoding='cp949')
light.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 21526 entries, 0 to 21525
Data columns (total 10 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 관리번호 21526 non-null int64
1 주소 21526 non-null object
2 교차로명 21388 non-null object
3 교차로번호 21388 non-null float64
4 설치일자 21345 non-null object
5 신호등 종류 21526 non-null object
6 신호등 재질 21526 non-null object
7 신호등 전구 21526 non-null object
8 경도 21526 non-null float64
9 위도 21526 non-null float64
dtypes: float64(3), int64(1), object(6)
memory usage: 1.6+ MB
mymap = folium.Map(location=[36.353617,127.3690643], zoom_start=20)
for loc in light[['위도','경도','교차로명']].values:
if loc[2] == '갈마네거리':
folium.Marker(loc[0:2]).add_to(mymap)
mymap