如何用Plotly为Pandas多列数据添加下拉选择菜单绘制图表
问题描述
我有一个Pandas DataFrame,想要用Plotly绘制柱状图,并且通过下拉菜单选择不同的股票名称列来查看对应数据。尝试相关方法后,不确定如何把动态变化的列名添加到updatemenus中,附上当前代码和参考DataFrame:
import plotly.graph_objects as go # plotly setup fig = go.Figure() # Add traces, starting from First Column. Each column is a stock data. for mycolumn in df.columns[1:]: fig.add_traces( go.Scatter(x=list(df.Quarters), y=list(df[mycolumn])) ) updatemenus = None for mycolumn in df.columns[1:]: # construct menus updatemenus = [{'buttons': [{'method': 'update', 'label' : column, # what? 'args' : [{'y': [values_1, values_1b]},] # what? } 'direction': 'down', 'showactive': True,} ] # update layout with buttons, and show the figure fig.update_layout(updatemenus=updatemenus) fig.show()
参考DataFrame:
| Quarters | 526433 | AARTIIND | DEEPAKNTR | GREENPANEL | METROBRAND | MOLDTECH | MUFTI | MUTHOOTFIN | YASHO | ZENTEC |
|---|---|---|---|---|---|---|---|---|---|---|
| Mar-21 | 4.6 | 1.69 | ||||||||
| Jun-21 | 2.99 | 4.55 | 22.19 | 2.43 | 1.16 | 24.37 | 10.01 | -0.14 | ||
| Sep-21 | 3.77 | 4.14 | 18.65 | 5.47 | 1.89 | 1.3 | 24.97 | 12.44 | 0 | |
| Dec-21 | 4.3 | 20.01 | 17.78 | 5.14 | 3.69 | 0.59 | 25.91 | 12.71 | -0.02 | |
| Mar-22 | 1.54 | 5.34 | 19.59 | 6.57 | 2.49 | 1.67 | 24.84 | 11.69 | 0.42 | |
| Jun-22 | 3.35 | 3.75 | 17.2 | 6.33 | 3.8 | 1.1 | 20.41 | 16.99 | 0.94 | |
| Sep-22 | 2.84 | 3.42 | 12.79 | 5.91 | 2.72 | 2.43 | 80.9 | 22.22 | 19.61 | 0.71 |
| Dec-22 | 2.25 | 3.77 | 15.33 | 3.06 | 4.22 | 3.26 | 60.25 | 23.11 | 9.18 | 1.19 |
| Mar-23 | 0.25 | 4.11 | 17.15 | 5.62 | 2.44 | 3.58 | 67.43 | 24.25 | 13.76 | 2.54 |
| Jun-23 | 0.36 | 1.93 | 10.99 | 3.04 | 3.91 | 2.21 | 1.33 | 25.46 | 12.97 | 5.6 |
| Sep-23 | -1.59 | 2.51 | 15.04 | 3.34 | 2.92 | 2.86 | 4.35 | 26.39 | 10.23 | 1.82 |
| Dec-23 | -0.19 | 3.42 | 14.81 | 2.82 | 3.52 | 2.47 | 2.42 | 27.49 | 11.88 | 3.64 |
| Mar-24 | -2.59 | 3.64 | 18.61 | 2.43 | 6.05 | 2.26 | 1.1 | 28.37 | 15.74 | 4.16 |
| Jun-24 | 2.83 | 3.78 | 14.85 | 3.4 | 1.81 | 1.51 | 28.99 | -2.16 | 9.14 |
解决方案
核心思路
- 先添加所有股票的柱状图轨迹,默认仅显示第一个股票的数据
- 遍历股票列动态生成下拉按钮,点击按钮时切换对应轨迹的可见状态并更新图表标题
修正后的完整代码
import plotly.graph_objects as go import pandas as pd # 替换为你的DataFrame加载逻辑 # df = pd.read_csv("your_data_source.csv") fig = go.Figure() stock_columns = df.columns[1:] # 批量添加柱状图轨迹,默认只显示第一个股票 for idx, col in enumerate(stock_columns): fig.add_trace(go.Bar( x=df['Quarters'], y=df[col], name=col, visible=(idx == 0) )) # 动态生成下拉菜单按钮 buttons = [] for idx, col in enumerate(stock_columns): buttons.append({ 'label': col, 'method': 'update', 'args': [ # 仅让当前选中的股票轨迹可见 {'visible': [i == idx for i in range(len(stock_columns))]}, # 更新图表标题,明确当前查看的股票 {'title': f'季度数据:{col}'} ] }) # 配置下拉菜单的位置和样式 updatemenus = [{ 'buttons': buttons, 'direction': 'down', 'showactive': True, 'x': 0.1, 'xanchor': 'left', 'y': 1.15, 'yanchor': 'top' }] # 完善图表整体布局 fig.update_layout( updatemenus=updatemenus, title='季度股票数据', xaxis_title='季度', yaxis_title='数值' ) fig.show()
关键说明
- 动态列适配:通过遍历
df.columns[1:]生成按钮和轨迹,无需硬编码列名,自动适配列名变化的场景 - 可见性控制:利用
visible参数实现轨迹的显示/隐藏切换,默认仅展示第一个股票 - 交互体验优化:点击按钮时同步更新标题,让用户清晰知晓当前查看的股票
内容的提问来源于stack exchange,提问作者RSW
相关产品推荐
相关产品推荐

