[Python] Matplotlib을 이용한 histogram 그리기

코딩/Python 2014. 8. 4. 11:07

[Matplotlib 설치]

1. Numpy 설치

- 설치된 Python 버전에 맞는 numpy 설치

2. Matplotlib 설치

- 구글링해서 1.3.1 버전을 받아서 설치했더니 'matplotlib requires dateutil'이라는 에러 발생

- 그냥 아래 링크 설명대로 github에서 다시 받아서 설치했더니 문제 없었음
(http://matplotlib.org/users/installing.html)

 

[Histogram 그리기]

1. 예제 코드

- TP_list와 FP_list에 들어있는 값들에 대한 histogram을 겹쳐서 그림

- 'alpha=0.5'를 통해 투명도 조절

import matplotlib.pyplot as plt
...
plt.hist(TP_list, bins=100, color='b', label='TP')
plt.hist(FP_list, bins=100, color='r', alpha=0.5, label='FP')
plt.title('Score Histogram')
plt.xlabel('Scores')
plt.ylabel('Frequency')
plt.legend()
plt.show()

 

2. 그 외

- 자세한 설명은 생략한다 아래 링크 참조

(http://bespokeblog.wordpress.com/2011/07/11/basic-data-plotting-with-matplotlib-part-3-histograms/)

 

'코딩 > Python' 카테고리의 다른 글

[Python] 파일 라인 수정  (0) 2014.04.28

[Python] 파일 라인 수정

코딩/Python 2014. 4. 28. 09:39

예제> 'test.txt'에서 'old_term: ... '으로 시작하는 라인을 'new_term: ...'으로 수정


 

with open('test.txt', 'r+') as f:              # file을 열고 알아서 닫아 줌
    lines = []
    new_line = 'new_term: this is test\n'
    for line in f:
        if(line.startswith('old_term:')):      # 'old_term:'으로 시작하는 line을 찾음
            lines = lines + [new_line]
        else:
            lines = lines + [line]
f.seek(0)                                      # file pointer 위치를 처음으로 돌림
f.writelines(lines)                            # 수정한 lines를 파일에 다시 씀
f.truncate()                                   # 현재 file pointer 위치까지만 남기고 나머지는 정리 

'코딩 > Python' 카테고리의 다른 글

[Python] Matplotlib을 이용한 histogram 그리기  (0) 2014.08.04