SDK location not foundのエラー
エラー内容:
SDK location not found. Define location with an ANDROID_SDK_ROOT environment variable or by setting the sdk.dir path in your project's local properties file at ~~~~~~
発生条件: Macで開発していたAndroidプロジェクトをWindowsマシンで開いたとき
解決策: ルートディレクトリに含まれるlocal.propertiesの内容を空にしてプロジェクトを開き直す。
原因: 上記のファイルに書かれているファイルのパスが、MacとWindowsで異なることがエラーの原因。 パスを消して、開き直せば、適切なパスに書き換えられて、エラーは解消される。
Takeaways
Takeaways - 英語のプレゼンで最後にまとめとして,重要な事項を振り返るときのスライドタイトルに
Sankey diagram(サンキーダイアグラム)をPythonで描いてみる
Sankeyダイアグラムの練習ノート
Plotlyのページ:https://plotly.com/python/sankey-diagram/
! pip install plotly
まずは,単純に2分岐するパスをつくってみる.A-->Bに10,A-->Cに20の流量があるとする.
ノードとパスはすべて整数で指定することになるので,A:0, B:1, C:2としておく.
# A:0-->B:1,A:0-->B:2 src = [0, 0] dst = [1, 2]
# それぞれの流量も順に指定する val = [10, 20]
import plotly.graph_objects as go fig = go.Figure( data=[ go.Sankey( # ノードをlinkに辞書型で指定 link = dict( source = src, target = dst, value = val ) ) ] ) # 表示 fig.show()
# わかりやすいようにノードの名前を文字列で指定する # この配列の順番どおりに割り当てられる.つまり,A:0, B:1, C:2に対応する. lbl = ["A", "B", "C"]
fig = go.Figure( data=[ go.Sankey( # ノードも辞書型で指定 node = dict( label = lbl ), link = dict( source = src, target = dst, value = val ) ) ] ) fig.show()
装飾
https://plotly.com/python/reference/sankey/#sankey-node
fig = go.Figure( data=[ go.Sankey( node = dict( pad = 100, # ノード間の縦方向の間隔 thickness = 50, # ノードの横幅 color = "gray", # ノードの色 line = dict(color = "blue", width = 5.0), # ノードの枠の色と太さ label = lbl ), link = dict( source = src, target = dst, value = val ) ) ] ) fig.show()
複数のパスの描画
複数の群を同時に可視化してみる.
A-->B:10. A-->C:20のパスに加えて,A-->B:100, A--C:200のパスを追加して,4本のパスを可視化してみる.
# ここでは,予め辞書型でノードとパスの情報を作っておくことにする # ノードは,変わらずにA, B, Cの3つを指定 dict_node = dict( label = ["A", "B", "C"] ) # パスは,4つ指定.後半の2要素を追加. dict_path = dict( {'source':[0, 0, 0, 0], 'target':[1, 2, 1, 2], 'value':[10, 20, 100, 200], } ) print(dict_node) print(dict_path)
fig = go.Figure( data=[ go.Sankey( node = dict_node, link = dict_path ) ] ) fig.show()
マウスのカーソルをパスに重ねるとたしかに4本のパスが描かれていることがわかる.しかし,これだとわかりにくいので,それぞれのパスにラベルと色の情報を追加する.
dict_node = dict( label = ["A", "B", "C"] ) dict_path = dict( {'source':[0, 0, 0, 0], 'target':[1, 2, 1, 2], 'value':[10, 20, 100, 200], 'label':["alpha", "alpha", "beta", "beta"], # 追加 'color':["red", "red", "blue", "blue"] # 追加 } ) print(dict_node) print(dict_path)
fig = go.Figure( data=[ go.Sankey( node = dict_node, link = dict_path ) ] ) fig.show()
センスはないが,それぞれのパスを色分けすることができたことがわかる.
ノードの位置
次に,これまでは,ノードの位置が自由に設定されていたが,ここでは明示的に指定してみる. ノードに'x', 'y'を追加することで,ノードの位置を指定できる.
ポイントは,xとyの両方を指定することと,はみ出ないようにpadを指定すること.
# ノードは,変わらずにA, B, Cの3つを指定 dict_node = dict( label = ["A", "B", "C"], x = [0.1, 0.5, 0.9], # 追加,正規化された位置 y = [0.1, 0.9, 0.1], # 追加.正規化された位置 pad = 100 # 追加, 100pxという意味 ) # パスは,2つ指定. dict_path = dict( {'source':[0, 0], 'target':[1, 2], 'value':[10, 20], } ) print(dict_node) print(dict_path)
fig = go.Figure( data=[ go.Sankey( node = dict_node, link = dict_path ) ] ) fig.show()
実務上のTips
あらかじめノードのリストがある場合は問題ないが,多くの場合は,ソースとターゲットのノード名とそれらの間の流量を示したテーブルを用意しておき,それを入力として可視化することが多いと思う. その場合は,テーブルに含まれるユニークなノードの一覧とからリストを作って,連番を振るほうが効率的である.
import pandas as pd table = pd.DataFrame( { 'source_name':["A", "A"], 'target_name':["B", "C"], 'value':[10, 20] } )
display(table)
# label encoderを使って連番を付与するモデルを作成する # sourceの列とtargetの列を連結してもれなく入力する from sklearn import preprocessing encoder = preprocessing.LabelEncoder() encoder.fit(pd.concat([table['source_name'], table['target_name']]).values)
# 試しにsource_nameとtarget_nameを数値に変換する print(encoder.transform(table['source_name'].values)) print(encoder.transform(table['target_name'].values))
# 文字列と数値の対応関係は,encoder.classes_で取得できる # これは,このままノードのラベルに使える print(encoder.classes_)
# もともとのテーブルに可視化用の数値データを table['source'] = encoder.transform(table['source_name']) table['target'] = encoder.transform(table['target_name'])
# 描画用の辞書は dict_node = dict( label = encoder.classes_ # エンコーダから ) # パスは,2つ指定. dict_path = dict( {'source':table['source'], 'target':table['target'], 'value':table['value'], } )
fig = go.Figure( data=[ go.Sankey( node = dict_node, link = dict_path ) ] ) fig.show()
x軸まわりに回転
rotate about x-axis
参考
11.4: Rotation about an axis versus rotation about a point . - Physics LibreTexts
Vue.jsを試そうとしてエラー
これを試してみようと思ったけど、
> npm run dev
すると、エラーになって困っていたが、
> npm init > npm install
で初期化していなかったことが原因だったっぽい。