I am trying to create a combined figure that consists of traces from two figures generated with plotly express - one that contains the raw scatter points of the data (which can be grouped by ‘Subject’ and ‘Channels’ in my example), and one that contains a mathematical model fit to each group of points. I need to plot both of these data sets on the same figure, as below.
I’ve done this by iterating through the traces in the second figure and calling
add_trace on the first figure:
for trace in fig_lines.data: fig_scatter.add_trace(trace)
If there is a more efficient/standard way to do this I would love to know. However, trying this method has resulted in a problem – when I add the lines, many lines (which i have even tried to delineate using
line_group) are assigned to one trace due to plotly express automatically grouping traces only by color for the line plot. This then prevents me from fully combining the plots with matching legends and legend groupings. I think that ideally, using
line_group (in conjunction with color, line dash, etc) should cause plotly express to assign each grouping to a different trace. Additionally, line_group should be able to take a list of column names as an argument.
I did find this page on a group by function, however it seems like old documentation and there is no plotly express implementation. It also seems like it requires explicit knowledge about the groups.
So, in the meantime, how should I combine these figures? Is this something that might be able to be accomplished with px’s new wide-form capabilities?