JupyterLab インストール -> MacOSX + Anaconda で matplotlib のインライン表示ができない!解決方法 / How to use matplotlib inline with JupyterLab in MacOSX and Anaconda

JupyterLab MacOSX + Anaconda で matplotlib のインライン表示ができない!解決方法 / How to use matplotlib inline with JupyterLab in MacOSX and Anaconda

** Sorry, this note is Japanese only, but please take a look at some code snippets. Hope it helps you a bit!

なにやら Jupyter Notebook をベースにした新しい データ サイエンティスト 環境 JupyterLab なるものが発表されたそうです。
まだまだ発展途上みたいなんだけど、上の画像みたいに色々できるようで心が小躍りしながら試してみたら・・・matplotlib の plot で失敗しました・・・

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

なんで??とか思ってたら、このエラー、IPython notebook 時代からのあるあるらしいです。
わたしみたいに最近 Ipython notebook とか Jupyter とか触り始めた人が引っかからないように、解決方法をメモっておきます。

JupyterLab ってなに??

そもそもこのテの分野のトーシロなわたしは、恥ずかしながら JupyterLab ってのがなんなんだ・・・と思ってました・・・
公式ブログを見てみると、

blog.jupyter.org

・・・

・・・

英語ばっかでよくわかんないですが、要は IPython notebook をベースにもっとリッチなデータ分析環境作ったよ、的なアレってことでしょうか。
JupyterLab を発表している動画もありました。

なんか色々ドラッグ&ドロップで操作できるとことか、一画面内に IPython notebook とかイロイロ一度に開いたりとか、後、データをキレイに表で表示するデモとかしてます。
なんか Spyder とか RStudio に似てる感じ・・・
ちょっと前の部分で、ユーザーインターフェースとかデザインとか、今後ガンガンupdateしていくよ!とかおっしゃってるようです。

JupyterLab インストール ~ 起動まで

JupyterLab のインストールは普通に pip でできます。

pip install jupyterlab

で、そのあとは、

jupyter lab

で起動。
起動後は http://localhost:8888/lab にブラウザでアクセスすると、JupyterLab の画面が表示されます。
起動画面はこんなカンジ。

JupyterLab インストール 起動画面

JupyterLab で matplotlib を使おうとするとエラーが・・・!

冒頭のような華やかなスクショを表示するようなプログラムはとても書けないのですが、せめて簡単なグラフだけでも、というのが Python を使う女心じゃないでしょうか(違
MacOSX と Anaconda 環境で以下のような簡単なコードで試したのですが、RuntimeError って!?

gist.github.com

んで、そのエラーの内容が冒頭のやつです(再掲)。

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

一番初めの %matplotlib inline のところでコケてるし・・・

RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to の解決方法

いろいろググったら、なんだ、結構あるあるなんですね、コレ。IPython notebook でも同じことが起こってたみたいです。IPython ベースだから当たり前か><
matplotlib の公式サイトでもちゃんと解決方法が解決されてます。

Working with Matplotlib in Virtual environments — Matplotlib 1.5.1 documentation(英語)

今回は matplotlibrc を作成する方法でやりました。
matplotlib をインスコすると、ホームディレクトリあたりに、.matplotlib というディレクトリが(たぶん)できてるので、そこに

backend: MacOSX

とだけ、テキストファイルに書いて、matplotlibrc という名前で保存します。
で、も一度 jupyter lab で JupyterLab を起動して、上のプログラムを走らせると・・・できた!!

JupyterLab エラー:RuntimeError: Python is not installed as a framework. の解決方法

JupyterLab を紹介してるブログ

日本語のはあんまり見つけられなかったのですが、こちらのブログが、ポイントをうまくまとめてくれてて参考になりました。

nonbiri-tereka.hatenablog.com