0. はじめに
plotlyの本を読んで写経している時に、vscodeのjupyterにてshow()
コマンドで何も表示されなくて苦戦したので簡単だがメモを残しておく。
※以下のgithubコードの以下部分を動かしてましたが、以下のように「No renderer could be found for mimetype ・・(略)」と出てグラフが表示されなかった。
※サンプルは「plotly.expresss」だが、通常のplotly.graph_objects(import plotly.graph_objects as go)も同様です
- 動作環境
- OS : Windows10 pro (Ver.2004)
- python 3.9.6(pyenv)
- plotry 5.10.0
- jupyter notebook(vscode-jupyter) ※vscodeではなく通常のjupyterだと問題ないかもです
1. 解決方法
2つ紹介する。1-2のほうがオススメ
1-1. 解決方法①plotを使用する
vscodeではなくブラウザ表示させる方法。
'show()'の代わりにfrom plotly.offline import plot
を使用する
import plotly.express as px
from plotly.offline import plot
gapminder = px.data.gapminder()
#.show()は取り除く
plot(
px.scatter(
gapminder, x="gdpPercap", y="lifeExp", log_x=True, hover_name="country"
)
)
1-2. 解決方法②デフォルトのレンダラーを設定する方法
レンダラーとは「データを所定の形式に従って処理し、描画するシステム」こと。
これをjupyterでの表示形式に設定させればOK。pio.renderers.default = 'notebook'
import plotly.express as px
import plotly.io as pio
pio.renderers.default = 'notebook' #1回設定しておけばいい!
gapminder = px.data.gapminder()
px.scatter(
gapminder, x="gdpPercap", y="lifeExp", log_x=True, hover_name="country"
).show()
2. おわりに
stackoverflow等でも色々調べたが、解決策に至らなかった為に調べた。
もしこれでもうまくいかない場合は、設定可能なレンダラーをとりあえず全部試してみてもいいかもしれない(以下[]の中)。
Renderers configuration
-----------------------
Default renderer: 'notebook_connected'
Available renderers:
['plotly_mimetype', 'jupyterlab', 'nteract', 'vscode',
'notebook', 'notebook_connected', 'kaggle', 'azure', 'colab',
'cocalc', 'databricks', 'json', 'png', 'jpeg', 'jpg', 'svg',
'pdf', 'browser', 'firefox', 'chrome', 'chromium', 'iframe',
'iframe_connected', 'sphinx_gallery', 'sphinx_gallery_png']
3. 2023/1/1追記
久々にこの方法でやってみたらまた動かなくなってました。
そこでまた迷走していたら、以下URLのvscodeの拡張機能「Jupyter Notebook Renderers」にて解決しました。
この拡張機能を有効にし、カーネルを再起動するだけで他の設定は不要でした(以下は有効にした後の例)
参考