[英]Plotly-Dash: Adding new yaxis per selected df column
I have a basic dash app that graphs some data from a dictionary of dataframes.我有一个基本的破折号应用程序,可以从数据框字典中绘制一些数据。 The first dropdown selects the df, while the second selects the columns of the df to be plotted.
第一个下拉列表选择 df,而第二个下拉列表选择要绘制的 df 列。
This works well, but I can't seem to add a new yaxis for each of the plotted columns.这很好用,但我似乎无法为每个绘制的列添加新的 y 轴。 I have a large number of columns in each df and they change depending on the df that is selected.
我在每个 df 中有大量列,它们会根据选择的 df 而变化。
First, I tried to change the updateGraph callback to include yaxis=i
after defining x, y & name.首先,我尝试在定义 x、y 和名称后更改 updateGraph 回调以包含 yaxis
yaxis=i
。 Looking at the documentation, it seems that I can define the yaxis in go.Scatter
but that I would need to set them as 'y2', 'y3, 'y4' etc. I've also tried to update the layout via go.Figure.add_trace
in this way but neither has worked.查看文档,似乎我可以在 go.Scatter 中定义
go.Scatter
,但我需要将它们设置为“y2”、“y3”、“y4”等。我还尝试通过go.Figure.add_trace
以这种方式但都没有奏效。 The code is below, where dict_main
is a dictionary of dataframes of various sizes.代码如下,其中
dict_main
是各种大小的数据帧的字典。
All help is appreciated!感谢所有帮助!
data = list(dict_main.keys())
channels = dict_main[data[0]]
app.layout = html.Div(
[
html.Div([
dcc.Dropdown(
id='data-dropdown',
options=[{'label': speed, 'value': speed} for speed in data],
value=list(dict_main.keys())[0],
searchable=False
),
], style={'width': '49%', 'display': 'inline-block'}),
html.Div([
dcc.Dropdown(
id='channel-dropdown',
multi=True
),
], style={'width': '49%', 'display': 'inline-block'}
),
html.Div([
dcc.Graph(
id='Main-Graph',
),
], style={'width': '98%', 'display': 'inline-block'}
)
]
)
@app.callback(
Output('channel-dropdown', 'options'),
[Input('data-dropdown', 'value')])
def update_date_dropdown(speed):
return [{'label': i, 'value': i} for i in dict_main[speed]]
@app.callback(
Output('Main-Graph', 'figure'),
[Input('channel-dropdown', 'value')],
[State('data-dropdown', 'value')])
def updateGraph(channels, speed):
if channels:
return go.Figure(data=[go.Scatter(x=dict_main[speed].index, y=dict_main[speed][i], name=i, yaxis='y2') for i in channels])
else:
return go.Figure(data=[])
if __name__ == '__main__':
app.run_server()
UPDATE, This works;更新,这行得通; although some small changes to color and position are still needed - Thanks to @Philipp for all the help;
尽管仍然需要对颜色和 position 进行一些小的更改 - 感谢@Philipp 的所有帮助;
@app.callback(
Output('Main-Graph', 'figure'),
[Input('channel-dropdown', 'value')],
[State('rpm-dropdown', 'value')])
def updateGraph(channels, test):
if channels:
j=1
my_layout = {}
my_axis = list("")
for index, column in enumerate(list(channels)):
my_layout['yaxis' + str(j) if j > 1 else 'yaxis'] = {}
my_layout['yaxis' + str(j) if j > 1 else 'yaxis']['title'] = column
my_layout['yaxis' + str(j) if j > 1 else 'yaxis']['overlaying'] = 'y' if j > 1 else 'free'
my_layout['yaxis' + str(j) if j > 1 else 'yaxis']['anchor'] = 'free'
my_layout['yaxis' + str(j) if j > 1 else 'yaxis']['side'] = 'left'
my_axis.append('y' + str(j) if j > 1 else 'y')
j+=1
return go.Figure(data=[go.Scatter(x=dict_main[test].index, y=dict_main[test][column], name=column, yaxis=my_axis[index]) for index, column in enumerate(channels)],layout=my_layout)
else:
return go.Figure(data=[])
You have to define every y-axis in the layout property of your graph (right now you're only setting the data property).您必须在图形的布局属性中定义每个 y 轴(现在您只设置数据属性)。 See this example .
请参阅此示例。
If you don't want to draw all y-axes (if your df has many columns) you have to set some of them invisible via setting variables like [overlaying, ticks, showticklabels, showgrid, zeroline] (you can find info about them here ) but they still have to be defined accordingly in the layout so you can refer to them in the scatter function.如果您不想绘制所有 y 轴(如果您的 df 有很多列),则必须通过设置 [overlaying, ticks, showticklabels, showgrid, zeroline] 等变量将其中一些设置为不可见(您可以找到有关它们的信息在这里),但它们仍然必须在布局中相应地定义,以便您可以在散点图 function 中引用它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.