2017-02-23 88 views
0

我正在尝试向一组子绘图(为简单起见,下面仅使用一个空绘图创建一个简单的可重复示例)添加一个交互式形状(圆形)。在绘图交互式参数中访问形状参数

以下代码的预期行为是能够使用下拉菜单在两个不同的圆圈之间切换。

import plotly.plotly as py 
import plotly.graph_objs as go 
from plotly import tools 

# Create empty plot to put shapes into 
scatter = go.Scatter() 
fig = tools.make_subplots(cols=1) 
fig.append_trace(scatter, 1, 1) 

# Create two different shapes to select from 
fig['layout']['shapes'].append(
     { 
      'type': 'circle', 
      'xref': 'x', 'yref': 'y', 
      'x0': 0, 'y0': 0, 'x1': 1, 'y1': 1, 
      'visible':True 
     }) 

fig['layout']['shapes'].append(
     { 
      'type': 'circle', 
      'xref': 'x', 'yref': 'y', 
      'x0': 0, 'y0': 0, 'x1': 0.5, 'y1': 0.5, 
      'visible':False 
     }) 

# This doesn't work 
fig['layout']['updatemenus'] = 
    [{ 
     x:-0.05, y:0.8, 
     buttons=[ 
      {args:['layout.shapes.visible', [True, False]], label:'1', method:'restyle'}, 
      {args:['layout.shapes.visible', [False, True]], label:'2', method:'restyle'} 
     ] 
    }] 


py.plot(fig, filename='shape_select') 

我以为我的错误是,我指的是visible参数以错误的方式,并应layout.shapes.visible用别的东西来代替。

那么,在这种情况下,我该如何正确引用形状参数呢?

回答

1

要么我太笨找到明显的解决方案,但这看起来像是一个错误或不明确的行为给我。

最后8个下拉项目可靠地工作。第8具有取决于他们点击的顺序上的一些不确定的行为,并可能干扰一个与另一个..

建议的解决方案使用参数拆包创造的飞行形状的字典和每个形状设定visible

import plotly 

shape1 = { 
      'type': 'circle', 
      'xref': 'x', 'yref': 'y', 
      'x0': 0, 'y0': 0, 'x1': 1, 'y1': 1, 
      'line': {'color': 'rgb(0, 0, 255)'} 
     } 
shape2 = { 
      'type': 'circle', 
      'xref': 'x', 'yref': 'y', 
      'x0': 0, 'y0': 0, 'x1': 0.5, 'y1': 0.5, 
      'line': {'color': 'rgb(255, 0, 255)'} 
     } 

trace0 = plotly.graph_objs.Scatter(
    x= [0.2, 0.2, 0.3, 0.4, 0.2], 
    y= [0.2, 0.5, 0.8, 0.3, 0.2] 
) 

data = plotly.graph_objs.Data([trace0]) 
layout = plotly.graph_objs.Layout(shapes=[shape1, shape2]) 
fig = plotly.graph_objs.Figure(data=data, layout=layout) 
fig['layout']['shapes'].append(dict(visible=True, **shape1)) 
fig['layout']['shapes'].append(dict(visible=True, **shape2)) 


fig['layout']['updatemenus'] = [dict(
     x=-0.05, y=0.8, 
     buttons=[ 
      dict(args=['shapes.visible', [False, True]], label='Hide big - does not work', method='relayout'), 
      dict(args=['shapes.visible', [True, False]], label='Hide small - does not work', method='relayout'), 
      dict(args=['shapes[0].visible', False], label='Hide big - might work', method='relayout'), 
      dict(args=['shapes[1].visible', False], label='Hide small - might work', method='relayout'), 
      dict(args=['shapes[0].visible', True], label='Show big', method='relayout'), 
      dict(args=['shapes[1].visible', True], label='Show small', method='relayout'), 
      dict(args=['shapes', [dict(visible=True, **shape1), dict(visible=True, **shape2)]], label='Show all', method='relayout'), 
      dict(args=['shapes', [dict(visible=False, **shape1), dict(visible=False, **shape2)]], label='Hide all', method='relayout'), 
      dict(args=['shapes', [dict(visible=True, **shape1), dict(visible=False, **shape2)]], label='Show big, hide small', method='relayout'), 
      dict(args=['shapes', [dict(visible=False, **shape1), dict(visible=True, **shape2)]], label='Hide big, show small', method='relayout') 
     ] 
    )] 

plotly.offline.plot(fig, filename='shape_select.html') 
+0

谢谢,预期最后两行正在努力!是的,我确实尝试了第一个选项的第一件事,也惊讶它没有工作... – sashkello

+0

不知道如果错误或用户太愚蠢。 –

+0

另一个愚蠢的问题:)如果我想传递几个参数呢?也就是说,通过一个下拉菜单可以更改散点图的形状和数据,我该怎么做?似乎很明显,但在例子中找不到它,'args = ['shapes',['],'visible',[True,False]]似乎只是打破了整个事物...我可以单独发布... – sashkello

0

这一点更紧凑的解决方案的工作原理,以及:

fig['layout']['updatemenus'] = [dict(
    x=-0.05, y=0.8, 
     dict(args=[{'shapes[0].visible': True, 'shapes[1].visible': False}], label='First circle', method='relayout'), 
     dict(args=[{'shapes[0].visible': False, 'shapes[1].visible': True}], label='First circle', method='relayout'), 
    ] 
)]