파이썬으로 풀어보는 수학

재미있어 보여서 샀는데, 64쪽의 예제가 동작을 안합니다. 문제가 된 환경은 맥OS X.

x_numbers = [1,2,3]
y_numbers = [2,4,6]
from pylab import plot, show

plot(x_numbers, y_numbers)
show()

를 실행하면 화면에 그래프가 보일꺼라고 쓰여있지만, ImportError: No module named 'pylab' 라는 오류가 보입니다.

pip install pylab 을 한 다음 실행해보면 ImportError: cannot import name 'plot' 이라고 합니다.

뒤져보니 from matplotlib.pyplot import plot, show, imshow 가 올바른 임포트 방법이라고 하네요.

그랬더니 이번에는

RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are Working with Matplotlib in a virtual enviroment see 'Working with Matplotlib in Virtual environments' in the Matplotlib FAQ

라고 하구요.

한동안 검색을 해보니, 렌더링 백엔드 관련 설정이 필요하다고 합니다. 제가 성공한 방법은 다음과 같습니다.

import matplotlib
matplotlib.use("TkAgg")

from matplotlib.pyplot import plot, show, imshow

x_numbers =[1, 2, 3]
y_numbers =[2, 4, 6]
plot(x_numbers, y_numbers)
show()

하지만, 커맨드라인이나 idle 과 달리, ipython notebook 에서는 use("TkAgg")를 안해줘도, 아래 구문만 넣으면 이쁘게 나오네요.

%matplotlib inline

ipython notebook을 쓰는 것이 좋을 듯 합니다. 다음은 ipython notebook 설치에서 실행까지입니다.

virtualenv -p /usr/local/bin/python3 --no-site-packages pycalc
pip install matplotlib
pip install ipython
pip install "ipython[notebook]"
pip install pyzmq
pip install jinja2
pip install tornado
ipython notebook