プログラミング/julia/グラフの書き方 のバックアップ(No.4)

更新


プログラミング/julia

Plots を使う

関数グラフの描画には Plots というライブラリを使う。

これは様々なグラフ描画ライブラリを julia から同一の API で使えるようにするためのラッパーライブラリ。

実際の描画を行う Backend の描画ライブラリとして何を選ぶかによって、描画速度や仕上がりなどが結構変わってくる。

インストール

バックエンドのライブラリがないと描画できないので、とりあえず GR, Plotly, PyPlot を入れておく。

LANG:julia
Pkg.add("Plots")
Pkg.add("GR")
Pkg.add("Plotly")
Pkg.add("PyPlot")

# 最新版を使うには
Pkg.checkout("Plots")

バックエンドライブラリ

実際の描画を行うバックエンドライブラリはいろいろある。

http://docs.juliaplots.org/latest/backends/

例えばバックエンドライブラリとして GR を使うには、

LANG:julia
using Plots
Plots.gr() // Plots のバックエンドとして GR を使う
gr() // こう書いても同じ
// 引数としてオプションを渡せる

などとする。

2018春時点では、たぶん、有名なのは GR, Plotly, PyPlot の3つ。

  • GR : Plots.gr()
    • 出力が svg になるので、印刷してもキレイ
    • 点がたくさんあるとブラウザが固まる
    • 普段使いにはこれがベスト?
  • Plotly : Plots.plotly()
    • マウスでグラフの表示範囲などを調整可能
    • 出力が svg になるので、印刷してもキレイ
    • 点がたくさんあるとブラウザが固まる
    • Gists 上などでは javascript が許可されないため表示できない
    • jupyter notebook のリロードでエラーになり画像が表示されない場合がある
    • 試行錯誤中は良いかもしれないけれど最終版としてはつらい
  • PyPlot : Plots.pyplot()
    • 出力が png になるため、データ点の多いグラフでも Web ブラウザに負担がかからない
    • ただ印刷などすると dpi 不足で画像が粗くなる
    • PyCall の設定が必要(下記参照)
    • データ点の多いグラフ専用?

それぞれの表示はこんな感じ。

  • Plotly の円グラフの表示がおかしい
  • PyPlot の円グラフが楕円になってしまっている

test-julia-plots.png

GR のグラフは拡大してもキレイ

test-julia-plots-gr.png

PyPlot のグラフはぼやける

test-julia-plots-pyplot.png

Plotly のグラフはインタラクティブに編集できる。

test-julia-plots-plotly.gif

でもブラウザ上でリロードするだけで、Plotly のグラフは

Javascript error adding output!
ReferenceError: Plotly is not defined
See your browser Javascript console for more details.

などというエラーが出て表示されなくなってしまう。

Gist 上でも無理。

このあたりを見る?

Plots/GR: グラフ package のおすすめ
http://www.cas.cmc.osaka-u.ac.jp/~paoon/misc/julia/post/trouble-plots-package/

JupyterでJuliaを動かして回帰分析をやってみる。
https://qiita.com/kenmatsu4/items/b77ecec75418cbaa2212

jupyter notebookで触れるプロットを描く
http://ksknw.hatenablog.com/entry/2016/08/04/233103

Julia言語と Plots + GR で複素関数のgifアニメーションを作る
http://optie.hatenablog.com/entry/2018/03/29/210619

GR - examples
http://docs.juliaplots.org/latest/examples/gr/

PlotThemes
https://github.com/JuliaPlots/PlotThemes.jl

Plotly を使う?

https://qiita.com/deaikei/items/b71c1ca2c4c2a2c37526

$ python3 -m pip install numpy plotly

これとてもいいのだけれど、これを使った notebook のファイルサイズが非常に大きくなるみたい。

簡単なグラフでも5メガバイトとか。

で、gist へ保存ができなくなってしまった。

どうしたらいいか、考え中。

PyPlot を使う

matplotlib を使うので、

LANG:console
$ sudo python3 -m pip install matplotlib

とすれば、python から使うのは簡単だった。

julia から使うには、単に Pkg.add("PyPlot") しただけだと、

InitError: Failed to import required Python module matplotlib.

For automated matplotlib installation, try configuring PyCall to use the Conda.jl package's Python "Miniconda" distribution within Julia. Relaunch Julia and run:
    ENV["PYTHON"]=""
    Pkg.build("PyCall")
before trying again.

The pyimport exception was: PyError (ccall(@pysym(:PyImport_ImportModule), PyPtr, (Cstring,), name)

The Python package matplotlib could not be found by pyimport. Usually this means
that you did not install matplotlib in the Python version being used by PyCall.

PyCall is currently configured to use the Python version at:

python

and you should use whatever mechanism you usually use (apt-get, pip, conda,
etcetera) to install the Python package containing the matplotlib module.

One alternative is to re-configure PyCall to use a different Python
version on your system: set ENV["PYTHON"] to the path/name of the python
executable you want to use, run Pkg.build("PyCall"), and re-launch Julia.

Another alternative is to configure PyCall to use a Julia-specific Python
distribution via the Conda.jl package (which installs a private Anaconda
Python distribution), which has the advantage that packages can be installed
and kept up-to-date via Julia.  As explained in the PyCall documentation,
set ENV["PYTHON"]="", run Pkg.build("PyCall"), and re-launch Julia. Then,
To install the matplotlib module, you can use `pyimport_conda("matplotlib", PKG)`,
where PKG is the Anaconda package the contains the module matplotlib,
or alternatively you can use the Conda package directly (via
`using Conda` followed by `Conda.add` etcetera).

) <type 'exceptions.ImportError'>
ImportError('No module named matplotlib',)


during initialization of module PyPlot

Stacktrace:
 [1] pyimport_conda(::String, ::String, ::String) at /home/osamu/.julia/v0.6/PyCall/src/PyCall.jl:609
 [2] __init__() at /home/osamu/.julia/v0.6/PyPlot/src/init.jl:175
 [3] _include_from_serialized(::String) at ./loading.jl:157
 [4] _require_from_serialized(::Int64, ::Symbol, ::String, ::Bool) at ./loading.jl:200
 [5] _require_search_from_serialized(::Int64, ::Symbol, ::String, ::Bool) at ./loading.jl:236
 [6] _require(::Symbol) at ./loading.jl:441
 [7] require(::Symbol) at ./loading.jl:405
 [8] include_string(::String, ::String) at ./loading.jl:522

と言われ、PyCall を build する必要があった。

LANG:console
julia> Pkg.add("PyPlot")
julia> ENV["PYTHON"]=""
julia> Pkg.build("PyCall")

ENV["PYTHON"]="" はシステムの python を使うためのおまじないっぽい?

GR を使う

グラフは svg で書かれるようで、クオリティーは高いけれど、 あまり複雑なグラフを表示するとブラウザが固まる。

コメント・質問





Counter: 39933 (from 2010/06/03), today: 8, yesterday: 0