LoginSignup
0
1

More than 3 years have passed since last update.

jupyterlab & matplotlibで作成したグラフのデータ点にリンクを貼る

Last updated at Posted at 2020-04-07

この記事について

記事タイトルの通りのことがやりたかった。
また使うことがあるかも知れないので、その方法を記録しておく。

手順

  • matplotlib で リンク付きのグラフを作成
  • svg 形式で保存
  • Chrome などのブラウザでファイルを開く

環境

自分は jupyter-lab での作業でした。
バージョンはこんな感じ(pip freeze の結果より)

jupyterlab-server==1.0.7
matplotlib==3.2.1

ファイル作成

# 各種設定
import matplotlib.pyplot as plt
from IPython.display import set_matplotlib_formats

set_matplotlib_formats("svg")

# テストデータ準備
data_list = [[0, 1], [1,0], [1,1]]
label_list = ['google', 'yahoo', 'lightcafe']
url_list = ['https://www.google.com/', 'https://www.yahoo.co.jp/', 'https://www.lightcafe.co.jp/']

# グラフ作成
fig, ax = plt.subplots(1, 1, tight_layout=True)
for d, l, u in zip(data_list, label_list, url_list):
    x, y = d
    ax.scatter(x, y)  #点プロット
    ax.annotate(l, xy=(x, y), size=10,  #文字プロット
                url=u,
                bbox=dict(color='w', alpha=1e-6, url=u),
               )
# 保存
fig.savefig('test.svg')

確認

test.svg を ブラウザで開き、グラフ内のデータ点 or ラベルをクリックすれば
設定したリンクに飛ぶ。

ちなみに、Google Colaboratory で同じものを実行すると svg 保存しなくても、
セルの Output から飛ぶことが出来ました。

IMG_20200408_091005.jpg

以上

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1