[英]Switch between scatterpolar plots in plotly
我想在 Python 中創建一個交互式 plotly plot 我在兩個散點圖(記錄版本和線性版本)之間切換。 我嘗試使用這里使用的相同想法,但沒有運氣。 我的代碼是:
import pandas as pd
import plotly.graph_objs as go
# Get data
url = "https://raw.githubusercontent.com/mpudil/projects/master/slc.csv"
slc_df = pd.read_csv(url)
fig_log = go.Figure(data=
go.Scatterpolar(
r = list(slc_df['distance']),
theta = list(slc_df['bearing']),
mode = 'markers',
name = 'log'
))
fig_log.update_layout(
polar = dict(
radialaxis = dict(type = "log", tickangle = 45),
angularaxis = dict(
thetaunit = "degrees",
dtick = 45,
rotation=90,
direction = "clockwise",
tickmode="array",
tickvals=[0, 45, 90, 135, 180, 225, 270, 315],
ticktext=["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
)
))
fig_linear = go.Figure(data=
go.Scatterpolar(
r = list(slc_df['distance']),
theta = list(slc_df['bearing']),
mode = 'markers',
name='linear'
))
fig_linear.update_layout(
polar = dict(
radialaxis = dict(type = "linear", tickangle = 45),
angularaxis = dict(
thetaunit = "degrees",
dtick = 45,
rotation=90,
direction = "clockwise",
tickmode="array",
tickvals=[0, 45, 90, 135, 180, 225, 270, 315],
ticktext=["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
),
))
data = [fig_log, fig_linear]
updatemenus = list([
dict(active=-1,
buttons=list([
dict(label = 'log',
method = 'update',
args = [{'visible': [True, False]},
{'title': 'Logged Distance'}]),
dict(label = 'linear',
method = 'update',
args = [{'visible': [False, True]},
{'title': 'Linear Distance'}])
]),
)
])
layout = dict(title='Photos near SLC', showlegend=False,
updatemenus=updatemenus)
fig = dict(data=data, layout=layout)
plotly.offline.plot(fig, auto_open=False, show_link=False)
但是,我收到值錯誤:收到的“數據”屬性無效元素。 我在這里做錯了嗎?
注 1:地塊本身顯示得很好。
注2:數據集可以在這里訪問
顯然,可以只使用一條跡線的按鈕。
import pandas as pd
import plotly.graph_objs as go
# Get data
url = "https://raw.githubusercontent.com/mpudil/projects/master/slc.csv"
df = pd.read_csv(url)
trace = go.Scatterpolar(
r = df['distance'],
theta = df['bearing'],
mode = 'markers',
name = 'log')
# here there are your buttons
updatemenus = list([
dict(active=0,
buttons=list([dict(label="Linear",
method="relayout",
args=[{"polar.radialaxis.type": "linear"}]),
dict(label="Log",
method="relayout",
args=[{"polar.radialaxis.type": "log"}]),
]),
)
])
# here the layout (which is the same in the two cases)
layout = dict(polar=dict(
radialaxis=dict(tickangle=45),
angularaxis=dict(
thetaunit="degrees",
dtick = 45,
rotation=90,
direction = "clockwise",
tickmode="array",
tickvals=[0, 45, 90, 135, 180, 225, 270, 315],
ticktext=["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
)),
updatemenus=updatemenus)
fig = go.Figure(data=trace, layout=layout)
fig.show()
或者,如果您想像以前一樣使用trace
和updatemenus
使用update_layout
fig = go.Figure(trace)
fig = fig.update_layout(polar=dict(
radialaxis=dict(tickangle=45),
angularaxis=dict(
thetaunit="degrees",
dtick = 45,
rotation=90,
direction = "clockwise",
tickmode="array",
tickvals=[0, 45, 90, 135, 180, 225, 270, 315],
ticktext=["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
)),
updatemenus=updatemenus)
fig.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.