当下拉列表中的值随 go.Bar() (绘图)发生变化时如何更新轴?

问题描述 投票:0回答:1

我尝试在下拉值更改时更新

xaxis_title_text
(update_layout) 以及
tickvals
ticktext
(update_xaxes)。 我尝试改编 Plotly 示例(link)和 this 答案,但我仍坚持轴更新。在图1和图2中,我们可以从图1到图2看到,当下拉菜单发生变化时,轴不会更新。 Tkx 的帮助。 图1 图2

代码

import pandas as pd
import numpy as np
import plotly.graph_objects as go

### My df has more than 1,5M rows with 79 columns. Each column may vary the scale of the number.
### E.g.: Age, weight, income, etc aren't normalized.
df = pd.DataFrame() # any dataframe to compute a histogram
num_cols = df.columns

def compute_bins(x, bins, return_var):
   hist, bins_edges = np.histogram(x, bins=bins)
   bins_text = [f"({np.round(bins_edges[i],2)}, {np.round(bins_edges[i+1],2)}]" for i in range(len(bins_edges)-1) ]

   if return_var == 'h':
      return hist
   else:
      return bins_text

hist = compute_bins(x=df[x_title].values, bins=10, return_var='h')
hist = compute_bins(x=df[x_title].values, bins=10, return_var='b')

fig = go.Figure(go.Bar(x=np.array(range(len(bins_text))), y=hist))

my_buttons = [dict(
                   mehotd='update',
                   args=[{"y": [ compute_bins(x=df[x_title].values, bins=10, return_var='h'), 'underfined' ]
                          "x": [ compute_bins(x=df[x_title].values, bins=10, return_var='h'), 'underfined' ]}
                        ],
                   label = c
                  ) for k, c in enumerate(num_cols)]

fig.update_axes(tickvals==np.array(range(len(bins_text))), ticktext=bins_text)
fig.update_layout(bargap=0, xaxis_title_text=x_title, yaxis_title_text='Count',
                  updatemenus=[dict(
                                   active=0,
                                   x=0,y=1.2,
                                   xanchor='left',
                                   yanchor='top',
                                   buttons=my_buttons
                              )]
                  )

python graph drop-down-menu plotly plotly.graph-objects
1个回答
0
投票

作为下拉列表的输入值,需要与Fig.data和Fig.layout相关的设置。由于您没有提供任何数据,我已使用 iris 示例数据修改了您的代码。

import pandas as pd
import numpy as np
import plotly.graph_objects as go
import plotly.express as px

df = px.data.tips()
num_cols = df.columns[:2]

x_title = 'total_bill'

def compute_bins(x, bins, return_var):
   hist, bins_edges = np.histogram(x, bins=bins)
   bins_text = [f"({np.round(bins_edges[i],2)}, {np.round(bins_edges[i+1],2)}]" for i in range(len(bins_edges)-1) ]

   if return_var == 'h':
      return hist
   else:
      return bins_text

hist = compute_bins(x=df[x_title].values, bins=10, return_var='h')
bins_text = compute_bins(x=df[x_title].values, bins=10, return_var='b')
    
fig = go.Figure(go.Bar(x=bins_text, y=hist))

my_buttons = [dict(
                   method='update',
                   args=[{"y": [compute_bins(x=df[c].values, bins=10, return_var='h')],
                          "x": [ compute_bins(x=df[c].values, bins=10, return_var='b')]},
                         {'xaxis': [compute_bins(x=df[c].values, bins=10, return_var='b')]}
                        ],
                   label = c
                  ) for k, c in enumerate(num_cols)]

fig.update_xaxes(tickvals=np.array(range(len(bins_text))), ticktext=bins_text)
fig.update_layout(bargap=0, xaxis_title_text=x_title, yaxis_title_text='Count',
                  updatemenus=[dict(
                                   active=0,
                                   x=0,y=1.2,
                                   xanchor='left',
                                   yanchor='top',
                                   buttons=my_buttons
                              )]
                  )
fig.show()

© www.soinside.com 2019 - 2024. All rights reserved.