python - Plotly 下拉菜单执行功能
问题描述
我正在尝试使用 plotly 下拉菜单来执行绘图功能。
我编写了一个函数,它将根据输入值(“状态”)生成多个子图。似乎在下拉列表中选择一个将执行我的功能的状态应该是相对微不足道的,但是我已经用尽了很多资源。
下拉值应该是 list(option_dict.keys()),任何帮助将不胜感激!
功能代码如下:
def plot_generator(state, option_dict):
deltas = new_delta_dict(option_dict) # generate a fresh delta dict for the respective region
state_dict = deltas[state].copy()
state_dict = collections.OrderedDict(sorted(state_dict.items()))
state_dfs = total_dfs(state_dict) # get the count of total dataframes in dict (nested)
state_plots = {}
for dates in list(state_dict):
for option_type in state_dict[dates]:
plotname = str(dates[0]) + ' ' + str(dates[1]) + ' ' + option_type
sub_df = state_dict[dates][option_type]
## Rename the tuples in the columns ##
newcols = list(sub_df)[:-2]
newcols = ['{}_{}'.format(x[0],x[1]) for x in newcols]
keepcols = list(sub_df)[-2:]
colnames = newcols + keepcols
sub_df.columns = colnames
# Generate the plot
state_plots[plotname] = px.line(sub_df)
# Generate the figures
fig = make_subplots(rows = len(state_plots), cols=1, subplot_titles=list(state_plots))
for i, plotname in enumerate(list(state_plots)):
for dat in state_plots[plotname].data:
fig.add_trace((go.Scatter(x=dat['x'], y=dat['y'], name=dat['name'])), row=i+1, col=1)
# Obtain vline from underlying_dict
vline_date = (int(plotname[:4]), str(plotname[5:7]))
if 'American' in plotname:
vline_type = 'American'
elif 'European' in plotname:
vline_type = 'European'
else: print('Option Type Error')
# Obtain underlying and add to figure
fig.add_vline(x=underlying_dict[state][vline_date][vline_type], line_width=2, line_dash="dash", row=i+1, col=1)
fig.layout.template=None
fig.update_layout(height=(state_dfs*100), width=800, title=("{} Option Payoff Diagrams".format(state)))
return fig
解决方案
推荐阅读
- php - 呼叫 URL 友好的正确链接
- autocomplete - Visual Studio 2019 中的自动完成/智能感知损坏
- c++ - std::string 的 C++ c_str 返回空
- javascript - 如何修复代码不移动类别下的频道
- javascript - 根据 ID 设置 React 组件的样式
- firebase - 找不到文件时,FlutterFire Storage 重新抛出 PlatformException
- python - 如何获取 youtube_dl 下载的路径?
- android - 从不同页面返回 null 的文本
- ethereum - 我的主网 geth 快速同步会赶上吗?
- android - Angular 应用程序的 Android 模拟器中的 Cordova 错误