【问题标题】:Switch between scatterpolar plots in plotly在 plotly 中的 scatterpolar 图之间切换
【发布时间】: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

【问题讨论】:

    标签: python plotly


    【解决方案1】:

    显然可以只使用一条轨迹的按钮。

    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()
    

    或者,如果您想像以前一样使用traceupdatemenus 来使用update_layout

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      • 2021-03-06
      • 1970-01-01
      相关资源
      最近更新 更多