如何在 Plotly 中启用和禁用对数刻度作为查看器?
问题描述
我最近在探索 Plotly,我想知道是否有一种方法可以共享绘图并让查看器在对数轴和线性轴之间切换.
I am recently exploring Plotly and I wonder if there is a way for sharing a plot and let the viewer switch between a logarithmic axis and linear axis.
有什么建议吗?
解决方案
Plotly 有一个 dropdown 功能它允许用户动态更新绘图样式和/或正在显示的轨迹.下面是一个绘图的最小工作示例,用户可以在对数和线性刻度之间切换.
Plotly has a dropdown feature which allows the user to dynamically update the plot styling and/or the traces being displayed. Below is a minimal working example of a plot where the user can switch between a logarithmic and linear scale.
import plotly
import plotly.graph_objs as go
x = [1, 2, 3]
y = [1000, 10000, 100000]
y2 = [5000, 10000, 90000]
trace1 = go.Bar(x=x, y=y, name='trace1')
trace2 = go.Bar(x=x, y=y2, name='trace2', visible=False)
data = [trace1, trace2]
updatemenus = list([
dict(active=1,
buttons=list([
dict(label='Log Scale',
method='update',
args=[{'visible': [True, True]},
{'title': 'Log scale',
'yaxis': {'type': 'log'}}]),
dict(label='Linear Scale',
method='update',
args=[{'visible': [True, False]},
{'title': 'Linear scale',
'yaxis': {'type': 'linear'}}])
]),
)
])
layout = dict(updatemenus=updatemenus, title='Linear scale')
fig = go.Figure(data=data, layout=layout)
plotly.offline.iplot(fig)
我在 data
列表中添加了两条迹线,以显示如何在绘图中添加或删除迹线.这可以由每个 button
的 updatemenus
中的 visible
列表控制.
I added two traces to the data
list to show how traces can also be added or removed from a plot. This can be controlled by the visible
list in updatemenus
for each button
.
相关文章