我想在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
显然,可以仅使用带有一条迹线的按钮。
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()
或者,如果您想像以前一样使用update_layout
和trace
来使用updatemenus
,则>
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()