파이선 스크립트로 간단한 막대그래프 그리기를 만들어봤습니다. 정말 간단합니다. ^^;;
--------------------------
#-*- coding: utf-8 -*-
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams
X = []
Y = []
while True:
try:
x = input("input X: ")
except:
break
if x == '':
break
try:
y = input("input Y: ")
except:
break
if y == '':
break
if type(x) is not str:
if int(x) == float(x):
x = int(x)
else:
x = float(x)
if int(y) == float(y):
y = int(y)
else:
y = float(y)
X += [x]
Y += [y]
if len(X) == 0:
sys.exit()
ind = np.arange( len(X) )
width = 0.35
plt.plot()
plt.bar( ind, Y, 0.3 )
plt.xticks( ind+width/2., X, family = ['Noto Sans Korean'] )
plt.ylim( 0, max(Y) + int( max(Y)/10.0 ) )
plt.show()
--------------------
numpy가 필요없을 것 같은데, 기왕 집어넣은 거 냅두기로 했습니다.
- python버전은 3.x입니다.
- 우분투(리눅스) 환경에서 구동해봤습니다.
2015/07/23
파이선 3 (python 3)에서 matplotlib으로 그래프만들때 한글 집어넣기
파이션 공부하다보니 그래프 그릴 일이 있더군요. 그래서 matplotlib( http://matplotlib.org/1.3.1/index.html )을 사용하기로 했습니다. 조금만 공부하면 금방 도표그릴 수 있습니다.
그런데 좌표에 한글을 넣을 일이 있는데, 기본값으로 하면 그냥 깨지더군요. 이것저것 뒤져보다 알게된 걸 올립니다.
X축에 글자를 넣으려고 하면 xticks( http://matplotlib.org/1.3.1/api/pyplot_api.html?highlight=xticks#matplotlib.pyplot.xticks )를 쓰게 됩니다. 그리고 xticks는 args로 Text( http://matplotlib.org/api/text_api.html#matplotlib.text.Text.set_fontproperties )를 쓸 수 있습니다. 이 text의 Property에 보면 family( http://matplotlib.org/api/text_api.html#matplotlib.text.Text.set_family )가 있습니다. 이 값을 컴에 설치되어 있는 한글폰트값으로 설정해주면 한글이 떡 나타나게 됩니다.
예) plt.xticks( ind+width/2., X, family = ['Noto Sans Korean'] )
아주~~ 편합니다.
그런데 좌표에 한글을 넣을 일이 있는데, 기본값으로 하면 그냥 깨지더군요. 이것저것 뒤져보다 알게된 걸 올립니다.
X축에 글자를 넣으려고 하면 xticks( http://matplotlib.org/1.3.1/api/pyplot_api.html?highlight=xticks#matplotlib.pyplot.xticks )를 쓰게 됩니다. 그리고 xticks는 args로 Text( http://matplotlib.org/api/text_api.html#matplotlib.text.Text.set_fontproperties )를 쓸 수 있습니다. 이 text의 Property에 보면 family( http://matplotlib.org/api/text_api.html#matplotlib.text.Text.set_family )가 있습니다. 이 값을 컴에 설치되어 있는 한글폰트값으로 설정해주면 한글이 떡 나타나게 됩니다.
예) plt.xticks( ind+width/2., X, family = ['Noto Sans Korean'] )
아주~~ 편합니다.
피드 구독하기:
글 (Atom)