😅

Plotlyの図が表示されない問題の解決メモ(数式Latexが表示されない追加)

2024/05/15に公開

Jupyter labの場合とvscode(Jupyter)における図が表示されない場合における私がした対処方法をメモとして残しておきます。

Jupyter lab

インストール

  1. conda install -c plotly plotly=5.22.0
  2. conda install "jupyterlab>=3" "ipywidgets>=7.6"
  3. conda install -c conda-forge -c plotly jupyter-dash
  4. Jupyter labで起動していろいろコードを書いていく

    JavaScript Error: Cannot read properties pf undefined(reading 'output')
    上記エラーが生じてプロットできない場合は下記のような形で対処しました。
import plotly.io as pio
import plotly.graph_objects as go
pio.renderers.default = 'iframe' # or 'colab' or 'iframe' or 'iframe_connected' or 'sphinx_gallery'

Vscode(Jupyter)

インストール

  1. 拡張機能からJupyterをインストール
  2. VscodeでJupyter labを編集

Plotly No renderer could be found for mimetype "application/vnd.plotly.v1+json"
上記エラーが生じてプロットできない。
下記のソフトをインストールしたら治りました。
Jupyter Notebook Renderers

Vscode(Jupyter)において数式(Latex)が表示されない

インストール

  1. mathjaxをインストール:https://www.mathjax.org/ npm install mathjax
  2. 下記コードを追加
import plotly
from IPython.display import display, HTML

plotly.offline.init_notebook_mode()
display(HTML(
    '<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_SVG"></script>'
))

参考

https://github.com/microsoft/vscode-jupyter/issues/11658
https://plotly.com/python/getting-started/
https://happy-analysis.com/python/python-topic-plotly-env.html
https://stackoverflow.com/questions/74638652/render-latex-symbols-in-plotly-graphs-in-vscode-interactive-window
https://github.com/microsoft/vscode-jupyter/issues/8131

Discussion