【发布时间】:2020-06-04 00:39:27
【问题描述】:
我想在 Python 中创建一个交互式绘图图,在其中我在两个散点图(一个记录版本和一个线性版本)之间切换。我尝试使用与here 相同的想法,但没有运气。我的代码是:
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:数据集可以访问here
【问题讨论】: