@@ -49,38 +49,51 @@ def extract_axis_titles(fig):
49
49
return (r_titles , c_titles )
50
50
51
51
52
- def px_simple_combine (fig0 , fig1 ):
52
+ def px_simple_combine (fig0 , fig1 , fig1_secondary_y = False ):
53
53
"""
54
54
Combines two figures by just using the layout of the first figure and
55
55
appending the data of the second figure.
56
56
"""
57
+ if fig1_secondary_y and (
58
+ ("px" not in fig0 ._aux .keys ()) or ("px" not in fig0 ._aux .keys ())
59
+ ):
60
+ raise ValueError (
61
+ "To place fig1's traces on secondary y-axes, both figures must have "
62
+ "been made with Plotly Express."
63
+ )
57
64
grid_ref_shape = fig_grid_ref_shape (fig0 )
58
65
if grid_ref_shape != fig_grid_ref_shape (fig1 ):
59
66
raise ValueError (
60
67
"Only two figures with the same subplot geometry can be combined."
61
68
)
62
69
# reflow the colors
63
70
colorway = fig0 .layout .template .layout .colorway
64
- fig = make_subplots (* fig_grid_ref_shape (fig0 ))
71
+ specs = None
72
+ if fig1_secondary_y :
73
+ specs = [
74
+ [dict (secondary_y = True ) for __ in range (grid_ref_shape [1 ])]
75
+ for _ in range (grid_ref_shape [0 ])
76
+ ]
77
+ fig = make_subplots (* fig_grid_ref_shape (fig0 ), specs = specs )
65
78
for r , c in multi_index (* fig_grid_ref_shape (fig )):
66
- for (tr , title ), color in zip (
79
+ for (tr , f ), color in zip (
67
80
chain (
68
81
* [
69
- zip (
70
- f .select_traces (row = r + 1 , col = c + 1 ),
71
- cycle ([f .layout .title .text ]),
72
- )
82
+ zip (f .select_traces (row = r + 1 , col = c + 1 ), cycle ([f ]),)
73
83
for f in [fig0 , fig1 ]
74
84
]
75
85
),
76
86
cycle (colorway ),
77
87
):
88
+ title = f .layout .title .text
78
89
set_main_trace_color (tr , color )
79
90
# use figure title to differentiate the legend items
80
91
tr ["name" ] = "%s %s" % (title , tr ["name" ])
81
92
# TODO: argument to group legend items?
82
93
tr ["legendgroup" ] = None
83
- fig .add_trace (tr , row = r + 1 , col = c + 1 )
94
+ fig .add_trace (
95
+ tr , row = r + 1 , col = c + 1 , secondary_y = (fig1_secondary_y and (f == fig1 ))
96
+ )
84
97
fig .update_layout (fig0 .layout )
85
98
# title will be wrong
86
99
fig .layout .title = None
0 commit comments