teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2020/01/10 07:09

投稿

quickquip
quickquip

スコア11314

answer CHANGED
@@ -8,4 +8,34 @@
8
8
  y = np.sin(x)
9
9
  plt.plot(x, y)
10
10
  ```
11
- を実行してみてください。グラフがでないはずですよ。
11
+ を実行してみてください。グラフがでないはずですよ。
12
+
13
+ ----
14
+
15
+ `%matplotlib inline` は、
16
+
17
+ [https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-matplotlib](https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-matplotlib) に
18
+
19
+ > This function lets you activate matplotlib interactive support at any point during an IPython session. It does not import anything into the interactive namespace.
20
+
21
+ とへ明記されている通り、matplotlib のインタラクティブサポートを有効化するためのコマンドです。
22
+
23
+ ----
24
+
25
+ 例えば`jupyter console`コマンドなどでJupyterを動かした場合、コンソールで動きますから`%matplotlib inline`をしてもコンソールに画像が出ないので意味がありません。
26
+
27
+ macOS上では、そこでコード`plt.plot(...)`を実行すると画像がテンポラリに保存されてプレビューアプリケーションが起動されます。
28
+
29
+ `%matplotlib osx`してからコード`plt.plot(...)`を実行すると**Pythonが**ウィンドウを出して画像を表示してくれます。
30
+
31
+ ----
32
+
33
+ 例えば`jupyter qtconsole`コマンドでJupyterを動かした場合、画像が表示できるコンソールで動きますから`%matplotlib inline`をすると`plt.plot(...)`の結果を画像として表示するようですね。
34
+ [https://stackoverflow.com/questions/38128640/use-both-matplotlib-inline-and-qt-in-jupyter-notebook#answer-38129646](https://stackoverflow.com/questions/38128640/use-both-matplotlib-inline-and-qt-in-jupyter-notebook#answer-38129646)
35
+
36
+ 他のGUIライブラリのqtやtkが使えるようにビルドされたPythonを使っていれば、`%matplotlib qt`とか`%matplotlib tk`というような選択肢もあると思います。
37
+
38
+ ----
39
+
40
+ 実際やってみるとこのスイッチがどこかに保管されていて、今何が有効なのか? がつかみづらかったです。
41
+ 確かに、1度目はインライン表示されないが2度実行すると表示される、みたいなことも、特にinline指定しなくても表示される、みたいなことも起こりました。