Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9f4dff4

Browse files
Secondary y working with px_combine, but
Cannot yet copy annotation-like things to the final plot as we need to find out which row and column in the subplots they are referring to.
1 parent 29b2c8b commit 9f4dff4

File tree

7 files changed

+32
-73
lines changed

7 files changed

+32
-73
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import plotly.express as px
2+
import test_data
3+
from px_combine import px_simple_combine
4+
5+
df = test_data.multilayered_data(d_divs=[6, 3, 2], rwalk=0.1)
6+
last_cat = df.columns[2]
7+
last_cat_types = list(set(df[last_cat]))
8+
fig0 = px.line(
9+
df.loc[df[last_cat] == last_cat_types[0]],
10+
x="x",
11+
y="y",
12+
facet_col=df.columns[0],
13+
facet_col_wrap=3,
14+
color=df.columns[1],
15+
).update_layout(title="%s=%s" % (last_cat, last_cat_types[0]))
16+
fig1 = px.line(
17+
df.loc[df[last_cat] == last_cat_types[1]],
18+
x="x",
19+
y="y",
20+
facet_col=df.columns[0],
21+
facet_col_wrap=3,
22+
color=df.columns[1],
23+
).update_layout(title="%s=%s" % (last_cat, last_cat_types[1]))
24+
fig = px_simple_combine(fig0, fig1, fig1_secondary_y=True)
25+
fig0.show()
26+
fig1.show()
27+
fig.show()
File renamed without changes.

proto/px_combine/px_combine.py renamed to proto/px_overlay/px_combine.py

Lines changed: 4 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def px_simple_combine(fig0, fig1, fig1_secondary_y=False):
7676
]
7777
fig = make_subplots(*fig_grid_ref_shape(fig0), specs=specs)
7878
for r, c in multi_index(*fig_grid_ref_shape(fig)):
79+
print("row,col", r + 1, c + 1)
7980
for (tr, f), color in zip(
8081
chain(
8182
*[
@@ -94,7 +95,9 @@ def px_simple_combine(fig0, fig1, fig1_secondary_y=False):
9495
fig.add_trace(
9596
tr, row=r + 1, col=c + 1, secondary_y=(fig1_secondary_y and (f == fig1))
9697
)
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)
98101
# title will be wrong
99102
fig.layout.title = None
100103
# preserve bar mode
@@ -151,59 +154,6 @@ def get_first_set_barmode(figs):
151154
return barmode
152155

153156

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-
207157
df = test_data.aug_tips()
208158

209159

@@ -218,25 +168,6 @@ def simple_combine_example():
218168
return fig
219169

220170

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-
237171
if __name__ == "__main__":
238172
fig_simple = simple_combine_example()
239-
fig_secondary_y = secondary_y_combine_example()
240173
fig_simple.show()
241-
fig_secondary_y.show()
242-
fig_secondary_y.write_json("/tmp/fig.json")

proto/px_overlay/secondary_y_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Put the second plot's y data on a secondary y
File renamed without changes.

0 commit comments

Comments
 (0)