Кнопка «Добавить» для переключения с линейного на лог-масштаб в Plotly Dash - PullRequest
0 голосов
/ 15 апреля 2020

Этот вопрос почти такой же, как этот . Дело в том, что я не могу заставить это работать. Соответствующий код:

df = pd.DataFrame({'country': {4048: 'Chile',
  4053: 'Chile',
  17294: 'Spain',
  17303: 'Spain',
  17307: 'Spain',
  17312: 'Spain',
  17313: 'Spain',
  17316: 'Spain'},
 'total_cases': {4048: 1610.0,
  4053: 3031.0,
  17294: 430.0,
  17303: 9191.0,
  17307: 19980.0,
  17312: 47610.0,
  17313: 56188.0,
  17316: 78797.0},
 't_100_cases': {4048: 12.0,
  4053: 17.0,
  17294: 6.0,
  17303: 15.0,
  17307: 19.0,
  17312: 24.0,
  17313: 25.0,
  17316: 28.0}})


updatemenus = [
    dict(
        type="buttons",
        direction="left",
        buttons=list([
            dict(
                args=[{'yaxis': {'type': 'linear'}}],
                label="Linear Scale",
                method="update"
            ),
            dict(
                args=[{'yaxis': {'type': 'log'}}],
                label="Log Scale",
                method="update"
            )
        ])
    ),
]  

    fig = px.line(df, x='t_100_cases', y='total_cases', color='country',
                  labels={'t_100_cases': 'Días desde los 100 contagiados',
                          'total_cases': 'Total de casos'},
                  title='Evolución de casos totales')

    fig.update_layout(
        updatemenus=updatemenus
    )

Это приносит мне этот график:

enter image description here

Что выглядит хорошо, но, когда я нажимаю журнал масштаб, сюжет остается прежним:

enter image description here

Чего мне не хватает? Я думаю, это что-то простое.

1 Ответ

0 голосов
/ 15 апреля 2020

Я нашел ответ. Мне нужно было сделать два изменения в updatemenus:

С:

args=[{'yaxis': {'type': 'log'}}]
method="update"

Кому:

args=[{'yaxis.type': 'log'}}]
method="relayout"

Что делает для этого финала updatemenus:

updatemenus = [
    dict(
        type="buttons",
        direction="left",
        buttons=list([
            dict(
                args=[{'yaxis.type': 'linear'}],
                label="Linear Scale",
                method="relayout"
            ),
            dict(
                args=[{'yaxis.type': 'log'}],
                label="Log Scale",
                method="relayout"
            )
        ])
    ),
]
...