plotly

公式サイト

インストール

py -m pip install plotly==5.24.1 
  • バージョンを指定しなければ、最新版がインストールされる。

インポート

import plotly.express as px
  • インストールは”plotly”で行い、インポートは”plotly.express”にする。

マップの描画

軌跡(Trajectry)のマップを作成した例である。(GPSdata_plotmap/GPSdata_util)

zoom = 16.0    # Default Zoom Value
csv_file = 'currently processing csv file name'

# Read CSV File in DataFrame
df1 =pd.read_csv(csv_file)
df1.dropna(inplace=True, how='all')

fig = px.scatter_mapbox(
data_frame=df1,
lat="Lat",
lon="Lng",
hover_data=["DateTime","display_name"],
color="category",
size="StayTime",
size_max=100,
opacity=0.5,
zoom=zoom,
height=None,
width=None,
title='plotly Map of'+csv_file)
fig.update_layout(mapbox_style='open-street-map')
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
fig.update_layout(autosize=True)
fig.show()

# Output HTML File
output_html_file = csv_file.replace('.csv','')+'_plotly.html'
fig.write_html(output_html_file)
  • 実際の使用方法は様々なので、ドキュメントを参照する必要がある。
  • plotlyはDataFrameから直接書き出すことができる。

アニメーションマップの描画

plotlyでアニメーションを作成した例である。(GPSdata_MobileClip)

print('\nStart func_mapplot_plotly()')

# dataframe1.dropna(inplace=True, how='all')

fig = px.scatter_mapbox(
data_frame=dataframe3,
animation_frame="datetime",
animation_group="uid",
lat="lat",
lon="lng",
hover_name="uid",
color="uid",
size="lat",
size_max=20,
opacity=opacity,
zoom=zoom,
height=None,
width=None,
title='Plotly Animation by ' + os.path.basename(__file__) \
+ ' (Input=' + os.path.basename(input_csv_file) \
+ ', Interval=' + str(interval) + ', Zoom=' + str(zoom) + ')'
)
fig.update_layout(mapbox_style='open-street-map')
fig.update_layout(margin={"r": 0, "t": 24, "l": 0, "b": 0})
fig.update_layout(autosize=True)
fig.show()

# Output HTML File (UnicodeEncodeError fixed by asksaveasfilename)
output_html = tkinter.filedialog.asksaveasfilename(
title='Select Output HTML File Name',
initialdir=os.path.dirname(input_csv_file),
initialfile='plotly_animation.html',
filetypes=[('HTML','.html')])

fig.write_html(output_html)

print('End func_mapplot_plotly()')
  • DataFrameから直接描画することができる。
  • 右上に凡例が表示され、IDごとにメモリーエリアが確保されるようである。そのためIDごとのデータが時間的に継続していない場合、表示がおかしくなる。
    • Current Animation Limitations and Caveats
      • Animations are designed to work well when each row of input is present across all animation frames, and when categorical values mapped to symbol, color and facet are constant across frames. Animations may be misleading or inconsistent if these constraints are not met.