@@ -76,6 +76,7 @@ def px_simple_combine(fig0, fig1, fig1_secondary_y=False):
76
76
]
77
77
fig = make_subplots (* fig_grid_ref_shape (fig0 ), specs = specs )
78
78
for r , c in multi_index (* fig_grid_ref_shape (fig )):
79
+ print ("row,col" , r + 1 , c + 1 )
79
80
for (tr , f ), color in zip (
80
81
chain (
81
82
* [
@@ -94,7 +95,9 @@ def px_simple_combine(fig0, fig1, fig1_secondary_y=False):
94
95
fig .add_trace (
95
96
tr , row = r + 1 , col = c + 1 , secondary_y = (fig1_secondary_y and (f == fig1 ))
96
97
)
97
- fig .update_layout (fig0 .layout )
98
+ # TODO: How to preserve axis sizes when adding secondary y?
99
+ # TODO: How to put annotations on the correct subplot when using secondary y?
100
+ # fig.update_layout(fig0.layout)
98
101
# title will be wrong
99
102
fig .layout .title = None
100
103
# preserve bar mode
@@ -151,59 +154,6 @@ def get_first_set_barmode(figs):
151
154
return barmode
152
155
153
156
154
- def px_combine_secondary_y (fig0 , fig1 ):
155
- """
156
- Combines two figures that have the same faceting but whose y axes refer
157
- to different data by referencing the second figure's y-data to secondary
158
- y-axes.
159
- """
160
- if not all (check_figs_trace_types_xy ([fig0 , fig1 ])):
161
- raise ValueError ('Only subplots containing "xy" trace types may be combined' )
162
- grid_ref_shape = fig_grid_ref_shape (fig0 )
163
- if grid_ref_shape != fig_grid_ref_shape (fig1 ):
164
- raise ValueError (
165
- "Only two figures with the same subplot geometry can be combined."
166
- )
167
- if fig0 .layout .annotations != fig1 .layout .annotations :
168
- raise ValueError (
169
- "Only two figures created with Plotly Express with "
170
- "identical faceting can be combined."
171
- )
172
- specs = [
173
- [dict (secondary_y = True ) for __ in range (grid_ref_shape [1 ])]
174
- for _ in range (grid_ref_shape [0 ])
175
- ]
176
- fig = make_subplots (* grid_ref_shape , specs = specs , start_cell = "bottom-left" )
177
- fig0_ax_titles = extract_axis_titles (fig0 )
178
- fig1_ax_titles = extract_axis_titles (fig1 )
179
- # set primary y
180
- for r , c in multi_index (* grid_ref_shape ):
181
- for tr in fig0 .select_traces (row = r + 1 , col = c + 1 ):
182
- fig .add_trace (tr , row = r + 1 , col = c + 1 )
183
- if r == 0 :
184
- t = fig0_ax_titles [1 ][c ]
185
- fig .update_xaxes (title = t , row = r + 1 , col = c + 1 )
186
- if c == 0 :
187
- t = fig0_ax_titles [0 ][r ]
188
- fig .update_yaxes (
189
- selector = lambda ax : ax ["side" ] != "right" , title = t , row = r + 1 , col = c + 1
190
- )
191
- # set secondary y
192
- for r , c in multi_index (* grid_ref_shape ):
193
- for tr in fig1 .select_traces (row = r + 1 , col = c + 1 ):
194
- # TODO: How to set meaningful color regardless of trace type?
195
- tr ["marker_color" ] = "red"
196
- fig .add_trace (tr , row = r + 1 , col = c + 1 , secondary_y = True )
197
- t = fig1_ax_titles [0 ][r ]
198
- # TODO: How to best set the secondary y's title standoff?
199
- t ["standoff" ] = 0
200
- fig .update_yaxes (
201
- selector = lambda ax : ax ["side" ] == "right" , title = t , row = r + 1 , col = c + 1
202
- )
203
- fig .update_layout (annotations = fig0 .layout .annotations )
204
- return fig
205
-
206
-
207
157
df = test_data .aug_tips ()
208
158
209
159
@@ -218,25 +168,6 @@ def simple_combine_example():
218
168
return fig
219
169
220
170
221
- def secondary_y_combine_example ():
222
- fig0 = px .scatter (df , x = "total_bill" , y = "tip" , facet_row = "sex" , facet_col = "smoker" )
223
- fig1 = px .scatter (
224
- df ,
225
- x = "total_bill" ,
226
- y = "calories_consumed" ,
227
- facet_row = "sex" ,
228
- facet_col = "smoker" ,
229
- trendline = "ols" ,
230
- )
231
- fig1 .update_traces (marker_size = 3 )
232
- fig = px_combine_secondary_y (fig0 , fig1 )
233
- fig .update_layout (title = "Figure combination with secondary y-axis" )
234
- return fig
235
-
236
-
237
171
if __name__ == "__main__" :
238
172
fig_simple = simple_combine_example ()
239
- fig_secondary_y = secondary_y_combine_example ()
240
173
fig_simple .show ()
241
- fig_secondary_y .show ()
242
- fig_secondary_y .write_json ("/tmp/fig.json" )
0 commit comments