2017-02-23 44 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'), 
    ] 
)]