Python Plotly 图表更新有两个下拉菜单
我正在尝试在Jupyter Lab中构建一个情节散点图,以便能够在数据框架中的各个列之间看到依赖关系。 我想有两个下拉菜单(与X和Y轴相对应),在每个菜单中都有完整的DF列列表。当我在任何菜单中选择一列时,应将适当轴上的数据替换为我选择的列(因此,如果我为X和Y选择了同一列,我希望直线直线)。
以下是我使用示例数据框架的当前实现:
# Creating the DataFrame
temp = pd.DataFrame(np.random.randint(0, 1000, (100, 10)))
col_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
temp.columns = col_list
# Init figure with the A column on both axes by default
fig = go.Figure()
default_col = 0
fig.add_trace(
go.Scatter(
x=temp[col_list[default_col]].values,
y=temp[col_list[default_col]].values,
name="Metric correlation",
mode="markers"
),
)
fig.update_xaxes(title_text=col_list[default_col])
fig.update_yaxes(title_text=col_list[default_col])
col_list = temp.columns
# Building options for each of the lists
btns_x = [
dict(
label=c,
method="update",
args=[
{"x": temp[c].fillna(0).values,
'xaxis': {'title': c}
}],
) for c in col_list]
btns_y = [
dict(
label=c,
method="update",
args=[
{"y": temp[c].fillna(0).values,
'yaxis': {'title': c}
}],
) for c in col_list]
# Adding the lists to the figure
fig.update_layout(
updatemenus=[
dict(
buttons=btns_x,
# method="update",
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
dict(
buttons=btns_y,
# method="update",
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="right",
y=1.1,
yanchor="top"
),
]
)
fig.update_layout(width=1000, height=1000)
fig.show()
仍然存在一些问题:
I am trying to build a plotly scatterplot in Jupyter Lab to be able to see dependencies between various columns in a DataFrame.
I want to have two dropdown menus (corresponding to the X and Y axes), in each of which a full list of the DF columns will be available. When I select a column in any of the menus, the data on the appropriate axis should be replaced by the column I selected (so, if I select the same column for X and Y, I would expect a straight line).
Below is my current implementation with a sample DataFrame:
# Creating the DataFrame
temp = pd.DataFrame(np.random.randint(0, 1000, (100, 10)))
col_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
temp.columns = col_list
# Init figure with the A column on both axes by default
fig = go.Figure()
default_col = 0
fig.add_trace(
go.Scatter(
x=temp[col_list[default_col]].values,
y=temp[col_list[default_col]].values,
name="Metric correlation",
mode="markers"
),
)
fig.update_xaxes(title_text=col_list[default_col])
fig.update_yaxes(title_text=col_list[default_col])
col_list = temp.columns
# Building options for each of the lists
btns_x = [
dict(
label=c,
method="update",
args=[
{"x": temp[c].fillna(0).values,
'xaxis': {'title': c}
}],
) for c in col_list]
btns_y = [
dict(
label=c,
method="update",
args=[
{"y": temp[c].fillna(0).values,
'yaxis': {'title': c}
}],
) for c in col_list]
# Adding the lists to the figure
fig.update_layout(
updatemenus=[
dict(
buttons=btns_x,
# method="update",
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
dict(
buttons=btns_y,
# method="update",
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="right",
y=1.1,
yanchor="top"
),
]
)
fig.update_layout(width=1000, height=1000)
fig.show()
The figure draws correctly initially:
Still, there are a few problems:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这几乎是在列表综合上进行系统的。以下完全有效,允许选择任何列并更新适当的轴标题。
It's just about being systematic around the list comprehensions. Below fully works, allows selection of any column and updates appropriate axis title.