サイトアイコン てくてくぷれいす

CentOSでPlotlyとorcaをインストールしてファイル書き出しする

こんにちは,しまさん(@shimasan0x00)です.

最近はCentOS 7上でJupyter Labを動かしてプログラム動かしてます.

そんなこんなでLab上で分析しているとデータを可視化したくなります.

いつもmatplotlibを使っていたのですがPlotlyがよさそうだと聞きました.

Plotly's Python graphing library makes interactive, publication-quality graphs. Examples of how to make line plots, scatter plots, area charts, bar charts, error bars, box plots, histograms, heatmaps, subplots, multiple-axes, polar charts, and bubble charts.
Plotly Python Graphing Library – 

ということで色々触ってみたのですが可視化したものを保存しようとしたら詰みました.

pio.write_image(fig,"xx.eps")

で保存しようとしても全然うまくいきませんでした.

色々調べて実行してもうまくいかず時間を溶かす日々.

I’ve used plotly with great success, and I’ve been able save a chart as an image file with success. Now I’m learning to use plotly offline, both with iPython Notebook and with the generic offline version. But I can’t find any informaton on how to save my offline (iPython Notebook version or otherwise) chart as an image file. Any help would be appreciated.
Use plotly offline to save chart as image file – Plotly Community Forum

調べていくとorcaをうまくインストールできていなかったようです.

なので今回はorcaをインストールする方法を紹介したいと思います.

Githubで紹介されているMethod 4: Standalone binariesで解決できました.

環境

CentOS 7

Pyenv : Python 3.6.8

スポンサーリンク

Plotlyその他のインストール

まずはpythonパッケージをいくつかインストールします.

pip install plotly psutil
pip install orca(いらないかも)
sudo yum -y install xorg-x11-server-Xvfb

スポンサーリンク

Orcaのインストール

ではここから問題のOrcaをインストールしていきます.

公式サイトではcondaやnpm+pipでの方法が紹介されています.

Plotly allows you to save static images of your plots. Save the image to your local computer, or embed it inside your Jupyter notebooks as a static image.
Static Image Export – 

ですがnpmでインストールしても動かなかったです.

なので素直にGithubを見ることにしました.

Command line application for generating static images of interactive plotly charts – plotly/orca
plotly/orca – GitHub

condaやDockerは使いたくないし,npmはうまくいかなかったのでどうしたものかと思ったら第4の方法がありました.

スタンドアローンバイナリでインストールする方法です.

まずはリリースページにいきます.

Command line application for generating static images of interactive plotly charts – plotly/orca
Releases · plotly/orca – GitHub

AppImageファイルをダウンロードしてください.

次に権限付与します.

chmod +x orca-1.2.1-x86_64.AppImage

その後,AppImageを抽出します.

./orca-1.2.1-x86_64.AppImage --appimage-extract

シンボリックリンク貼ります.

sudo ln -sf /home/User/保存したPath/squashfs-root/app/orca /usr/local/bin/orca

リンク先をシェルに書き換えてしまいます.

sudo rm /usr/local/bin/orca
sudo vim /usr/local/bin/orca

内容(/usr/local/bin/orca)

#!/bin/bash
xvfb-run -a /home/User/保存したPath/squashfs-root/app/orca "$@"

権限周りを変更します.

sudo chown user:user /usr/local/bin/orca
chmod 777 /usr/local/bin/orca

すると…

$ orca
Plotly's image-exporting utilities

  Usage: orca [--version] [--help] <command> [<args>]

  Available commands:
  - graph [or plotly-graph, plotly_graph]
    Generates an image of plotly graph from inputted plotly.js JSON attributes.
    For more info, run `orca graph --help`.
  - serve [or server]
    Boots up a server with one route per available export component
    For more info, run `orca serve --help`.

コマンド通った!!!!ありがてぇ.

保存してみる(Jupyterlab)

PlotlyをLabで使うなら以下の拡張機能を入れとくと便利です.

@jupyterlab/plotly-extension

では円グラフを作成するサンプルを以下に貼っておきます.

一応offlineで使用できるように設定しています.

import plotly
import plotly.offline as offline
import plotly.graph_objs as go
import plotly.io as pio
offline.init_notebook_mode()#connected=False

plotly.io.orca.config.executable = '/usr/local/bin/orca'
plotly.io.orca.config.save()

layout = go.Layout(
    autosize=False,
    width=600,
    height=600,
)

data = [
    go.Pie(labels=label, values=number,
              hoverinfo='label+percent', 
              textinfo='percent',
              textfont=dict(size=10),
              opacity = 0.9,
              sort = True,
              direction =  'clockwise',
              marker=dict(line=dict(color='#000000', width=1)))
]

fig = go.Figure(data=data, layout=layout)
# offline.iplot(fig, filename='sample_pie')
pio.write_image(fig,"img/sample.eps")

ようやく保存できた…

スポンサーリンク

さいごに

今回はPlotlyで画像を保存するためにOrcaをインストールしていきました.

plotly使いやすいので徐々にmatplotlibから移行していければいいなと思います.

参考文献

こんにちは、mabuiです。 今回は備忘録としてグラフ作成ライブラリ、Plotlyで使用される、画像出力のためのライブラリ、plotly-orcaをCentOS7でインストール・実行するまでの作業ログになります。
CentOS7でplotly-orcaをインストール・実行する | – mabui.org
この記事ではPythonを使ってPlotlyのグラフを画像として保存する方法と、自分が資料用のグラフを作るときによく使うレイアウト調整の方法を紹介します。
Plotlyでレポート・論文に使えるグラフを描こう – かみのメモ – かみのメモ
モバイルバージョンを終了