Fig_px "works", but fig is broken

What is wrong with my code?

I like the fig_px “simplicity”, but I need to use subplots.

import math

import pandas as pd
import plotly.express as px
from plotly.subplots import make_subplots


def main():
    df = pd.read_csv("data.csv")
    cola = "Date"
    colb = "Task"

    fig_px = px.bar(
        df,
        title="title",
        x=cola,
        y="Allocation",
        color=colb,
    )
    per_point_data = df.groupby(cola).sum()
    data = per_point_data["Allocation"].agg(["max", "min"])
    a = data["max"]
    b = math.fabs(data["min"])

    fig = make_subplots(
        rows=1,
        cols=1,
        vertical_spacing=0.07,
        subplot_titles=("a", "b"),
        row_heights=[
            # per_point_data["Worktime"].max(),
            b
            + a,
        ],
    )

    # Add the second bar plot to the subplot
    for trace in fig_px.data:
        # fig.add_trace(trace, row=2, col=1)
        fig.add_trace(trace, row=1, col=1)

    fig.update_layout(
        barmode="stack",
        yaxis1={"title": "title"},
        yaxis2={"title": "title"},
        margin={"l": 0, "r": 0, "t": 32, "b": 0},
        legend={
            "title": "title",
            "tracegroupgap": 0,
            "y": 0.92,
        },
    )

    fig.show()
    fig_px.show()


if __name__ == "__main__":
    main()
,Date,Task,Worktime,Allocation
0,2028-01-31,000040-Gabon,0.0,0.0
1,2028-01-31,000041-Anabaptists,0.0,0.0
2,2028-01-31,000042-Clergymen,51.0,-40.3333333333
3,2028-01-31,000043-Montana,0.0,0.0
4,2028-01-31,000044-Friars,0.0,0.0
5,2028-01-31,000045-Cunoniaceae,53.0,-42.3333333333
6,2028-01-31,000046-Goodeniaceae,58.0,-47.3333333333
7,2028-01-31,000047-Algebraists,64.0,-64.0
8,2028-01-31,000048-Royalty,1.0,-1.0
9,2028-01-31,000049-Mayors,0.0,0.0
10,2028-01-31,000050-Syntacticians,6.0,-6.0
11,2028-01-31,000051-Assyriologists,43.0,-43.0
12,2028-01-31,000052-Subsahara,0.0,0.0
13,2028-01-31,000053-Fiction,0.0,0.0
14,2028-01-31,Overallocation,0.0,0.0
15,2028-02-01,000040-Gabon,27.0,-22.9333333333
16,2028-02-01,000041-Anabaptists,40.0,-31.0
17,2028-02-01,000042-Clergymen,73.0,0.0
18,2028-02-01,000043-Montana,5.0,-0.1333333333
19,2028-02-01,000044-Friars,4.0,-1.0
20,2028-02-01,000045-Cunoniaceae,56.0,0.0
21,2028-02-01,000046-Goodeniaceae,61.0,0.0
22,2028-02-01,000047-Algebraists,64.0,-50.8666666667
23,2028-02-01,000048-Royalty,22.0,-18.2
24,2028-02-01,000049-Mayors,11.0,-5.0666666667
25,2028-02-01,000050-Syntacticians,6.0,-6.0
26,2028-02-01,000051-Assyriologists,43.0,0.0
27,2028-02-01,000052-Subsahara,2.0,0.0
28,2028-02-01,000053-Fiction,31.0,0.0
29,2028-02-01,Overallocation,0.0,138.0666666667
30,2028-02-02,000040-Gabon,26.0,-25.0
31,2028-02-02,000041-Anabaptists,52.0,-50.8666666667
32,2028-02-02,000042-Clergymen,73.0,0.0
33,2028-02-02,000043-Montana,6.0,0.0
34,2028-02-02,000044-Friars,5.0,-3.7333333333
35,2028-02-02,000045-Cunoniaceae,56.0,0.0
36,2028-02-02,000046-Goodeniaceae,61.0,0.0
37,2028-02-02,000047-Algebraists,64.0,-29.0
38,2028-02-02,000048-Royalty,18.0,-16.9333333333
39,2028-02-02,000049-Mayors,10.0,-8.5333333333
40,2028-02-02,000050-Syntacticians,6.0,-6.0
41,2028-02-02,000051-Assyriologists,43.0,0.0
42,2028-02-02,000052-Subsahara,2.0,-0.0666666667
43,2028-02-02,000053-Fiction,31.0,0.0
44,2028-02-02,Overallocation,0.0,148.1333333333
45,2028-02-03,000040-Gabon,26.0,-25.3333333333
46,2028-02-03,000041-Anabaptists,48.0,-47.2666666667
47,2028-02-03,000042-Clergymen,74.0,0.0
48,2028-02-03,000043-Montana,6.0,0.0
49,2028-02-03,000044-Friars,3.0,-1.0666666667
50,2028-02-03,000045-Cunoniaceae,56.0,0.0
51,2028-02-03,000046-Goodeniaceae,61.0,0.0
52,2028-02-03,000047-Algebraists,64.0,-27.0
53,2028-02-03,000048-Royalty,21.0,-20.3333333333
54,2028-02-03,000049-Mayors,10.0,-8.1333333333
55,2028-02-03,000050-Syntacticians,6.0,-6.0
56,2028-02-03,000051-Assyriologists,43.0,0.0
57,2028-02-03,000052-Subsahara,2.0,0.0
58,2028-02-03,000053-Fiction,0.0,0.0
59,2028-02-03,Overallocation,0.0,126.8666666667
60,2028-02-04,000040-Gabon,32.0,-31.2
61,2028-02-04,000041-Anabaptists,54.0,-41.6666666667
62,2028-02-04,000042-Clergymen,89.0,0.0
63,2028-02-04,000043-Montana,9.0,-0.0666666667
64,2028-02-04,000044-Friars,4.0,-3.0666666667
65,2028-02-04,000045-Cunoniaceae,56.0,0.0
66,2028-02-04,000046-Goodeniaceae,61.0,0.0
67,2028-02-04,000047-Algebraists,64.0,-24.3333333333
68,2028-02-04,000048-Royalty,24.0,-22.9333333333
69,2028-02-04,000049-Mayors,11.0,-10.0
70,2028-02-04,000050-Syntacticians,6.0,-6.0
71,2028-02-04,000051-Assyriologists,43.0,0.0
72,2028-02-04,000052-Subsahara,2.0,-0.0666666667
73,2028-02-04,000053-Fiction,0.0,0.0
74,2028-02-04,Overallocation,0.0,111.8666666667
75,2028-02-05,000040-Gabon,32.0,-31.2
76,2028-02-05,000041-Anabaptists,61.0,-60.1333333333
77,2028-02-05,000042-Clergymen,89.0,0.0
78,2028-02-05,000043-Montana,6.0,0.0
79,2028-02-05,000044-Friars,5.0,-4.2666666667
80,2028-02-05,000045-Cunoniaceae,56.0,0.0
81,2028-02-05,000046-Goodeniaceae,62.0,0.0
82,2028-02-05,000047-Algebraists,64.0,-26.0666666667
83,2028-02-05,000048-Royalty,23.0,-22.2
84,2028-02-05,000049-Mayors,11.0,-10.2
85,2028-02-05,000050-Syntacticians,6.0,-6.0
86,2028-02-05,000051-Assyriologists,43.0,0.0
87,2028-02-05,000052-Subsahara,2.0,-0.0666666667
88,2028-02-05,000053-Fiction,0.0,0.0
89,2028-02-05,Overallocation,0.0,102.1333333333
90,2028-02-06,000040-Gabon,0.0,0.0
91,2028-02-06,000041-Anabaptists,0.0,0.0
92,2028-02-06,000042-Clergymen,0.0,0.0
93,2028-02-06,000043-Montana,0.0,0.0
94,2028-02-06,000044-Friars,0.0,0.0
95,2028-02-06,000045-Cunoniaceae,0.0,0.0
96,2028-02-06,000046-Goodeniaceae,0.0,0.0
97,2028-02-06,000047-Algebraists,0.0,0.0
98,2028-02-06,000048-Royalty,0.0,0.0
99,2028-02-06,000049-Mayors,0.0,0.0
100,2028-02-06,000050-Syntacticians,0.0,0.0
101,2028-02-06,000051-Assyriologists,0.0,0.0
102,2028-02-06,000052-Subsahara,0.0,0.0
103,2028-02-06,000053-Fiction,0.0,0.0
104,2028-02-06,Overallocation,0.0,0.0
105,2028-02-07,000040-Gabon,25.0,-23.3333333333
106,2028-02-07,000041-Anabaptists,53.0,-51.3333333333
107,2028-02-07,000042-Clergymen,78.0,0.0
108,2028-02-07,000043-Montana,6.0,0.0
109,2028-02-07,000044-Friars,3.0,-1.1333333333
110,2028-02-07,000045-Cunoniaceae,56.0,0.0
111,2028-02-07,000046-Goodeniaceae,61.0,0.0
112,2028-02-07,000047-Algebraists,64.0,-62.3333333333
113,2028-02-07,000048-Royalty,18.0,-16.2
114,2028-02-07,000049-Mayors,10.0,-7.6666666667
115,2028-02-07,000050-Syntacticians,6.0,-6.0
116,2028-02-07,000051-Assyriologists,43.0,0.0
117,2028-02-07,000052-Subsahara,2.0,0.0
118,2028-02-07,000053-Fiction,0.0,0.0
119,2028-02-07,Overallocation,0.0,93.0
120,2028-02-08,000040-Gabon,26.0,-22.6
121,2028-02-08,000041-Anabaptists,44.0,-28.6
122,2028-02-08,000042-Clergymen,75.0,0.0
123,2028-02-08,000043-Montana,5.0,-0.0666666667
124,2028-02-08,000044-Friars,4.0,-1.1333333333
125,2028-02-08,000045-Cunoniaceae,56.0,0.0
126,2028-02-08,000046-Goodeniaceae,61.0,0.0
127,2028-02-08,000047-Algebraists,64.0,-48.6
128,2028-02-08,000048-Royalty,20.0,-17.0666666667
129,2028-02-08,000049-Mayors,10.0,-6.8
130,2028-02-08,000050-Syntacticians,6.0,-6.0
131,2028-02-08,000051-Assyriologists,43.0,0.0
132,2028-02-08,000052-Subsahara,2.0,-0.0666666667
133,2028-02-08,000053-Fiction,0.0,0.0
134,2028-02-08,Overallocation,0.0,51.0
135,2028-02-09,000040-Gabon,24.0,-21.7333333333
136,2028-02-09,000041-Anabaptists,56.0,-40.2666666667
137,2028-02-09,000042-Clergymen,76.0,-0.0666666667
138,2028-02-09,000043-Montana,6.0,-0.0666666667
139,2028-02-09,000044-Friars,4.0,-0.6000000000000001
140,2028-02-09,000045-Cunoniaceae,56.0,0.0
141,2028-02-09,000046-Goodeniaceae,61.0,0.0
142,2028-02-09,000047-Algebraists,64.0,-23.3333333333
143,2028-02-09,000048-Royalty,17.0,-14.6666666667
144,2028-02-09,000049-Mayors,9.0,-5.4666666667
145,2028-02-09,000050-Syntacticians,6.0,-6.0
146,2028-02-09,000051-Assyriologists,43.0,0.0
147,2028-02-09,000052-Subsahara,2.0,-0.0666666667
148,2028-02-09,000053-Fiction,0.0,0.0
149,2028-02-09,Overallocation,0.0,164.1333333333
150,2028-02-10,000040-Gabon,25.0,-24.4
151,2028-02-10,000041-Anabaptists,52.0,-51.4
152,2028-02-10,000042-Clergymen,76.0,0.0
153,2028-02-10,000043-Montana,6.0,0.0
154,2028-02-10,000044-Friars,3.0,-2.2666666667
155,2028-02-10,000045-Cunoniaceae,56.0,0.0
156,2028-02-10,000046-Goodeniaceae,61.0,-0.0666666667
157,2028-02-10,000047-Algebraists,64.0,-25.0
158,2028-02-10,000048-Royalty,20.0,-19.4
159,2028-02-10,000049-Mayors,9.0,-8.4
160,2028-02-10,000050-Syntacticians,6.0,-6.0
161,2028-02-10,000051-Assyriologists,43.0,-42.4
162,2028-02-10,000052-Subsahara,2.0,-0.0666666667
163,2028-02-10,000053-Fiction,0.0,0.0
164,2028-02-10,Overallocation,0.0,74.9333333333
165,2028-02-11,000040-Gabon,31.0,-31.0
166,2028-02-11,000041-Anabaptists,58.0,-58.0
167,2028-02-11,000042-Clergymen,93.0,0.0
168,2028-02-11,000043-Montana,9.0,0.0
169,2028-02-11,000044-Friars,4.0,-4.0
170,2028-02-11,000045-Cunoniaceae,56.0,-24.0
171,2028-02-11,000046-Goodeniaceae,61.0,-29.0
172,2028-02-11,000047-Algebraists,64.0,-32.0
173,2028-02-11,000048-Royalty,23.0,-23.0
174,2028-02-11,000049-Mayors,11.0,-11.0
175,2028-02-11,000050-Syntacticians,6.0,-6.0
176,2028-02-11,000051-Assyriologists,43.0,0.0
177,2028-02-11,000052-Subsahara,2.0,0.0
178,2028-02-11,000053-Fiction,0.0,0.0
179,2028-02-11,Overallocation,0.0,67.0
180,2028-02-12,000040-Gabon,32.0,-32.0
181,2028-02-12,000041-Anabaptists,64.0,-64.0
182,2028-02-12,000042-Clergymen,89.0,0.0
183,2028-02-12,000043-Montana,6.0,0.0
184,2028-02-12,000044-Friars,5.0,-5.0
185,2028-02-12,000045-Cunoniaceae,56.0,-24.0
186,2028-02-12,000046-Goodeniaceae,62.0,-30.0
187,2028-02-12,000047-Algebraists,64.0,-32.0
188,2028-02-12,000048-Royalty,23.0,-23.0
189,2028-02-12,000049-Mayors,11.0,-11.0
190,2028-02-12,000050-Syntacticians,6.0,-6.0
191,2028-02-12,000051-Assyriologists,43.0,0.0
192,2028-02-12,000052-Subsahara,3.0,0.0
193,2028-02-12,000053-Fiction,0.0,0.0
194,2028-02-12,Overallocation,0.0,60.9333333333
195,2028-02-13,000040-Gabon,0.0,0.0
196,2028-02-13,000041-Anabaptists,0.0,0.0
197,2028-02-13,000042-Clergymen,0.0,0.0
198,2028-02-13,000043-Montana,0.0,0.0
199,2028-02-13,000044-Friars,0.0,0.0
200,2028-02-13,000045-Cunoniaceae,0.0,0.0
201,2028-02-13,000046-Goodeniaceae,0.0,0.0
202,2028-02-13,000047-Algebraists,0.0,0.0
203,2028-02-13,000048-Royalty,0.0,0.0
204,2028-02-13,000049-Mayors,0.0,0.0
205,2028-02-13,000050-Syntacticians,0.0,0.0
206,2028-02-13,000051-Assyriologists,0.0,0.0
207,2028-02-13,000052-Subsahara,0.0,0.0
208,2028-02-13,000053-Fiction,0.0,0.0
209,2028-02-13,Overallocation,0.0,0.0
210,2028-02-14,000040-Gabon,25.0,-23.6
211,2028-02-14,000041-Anabaptists,53.0,-51.6
212,2028-02-14,000042-Clergymen,77.0,0.0
213,2028-02-14,000043-Montana,9.0,0.0
214,2028-02-14,000044-Friars,3.0,-0.33333333330000003
215,2028-02-14,000045-Cunoniaceae,56.0,0.0
216,2028-02-14,000046-Goodeniaceae,61.0,0.0
217,2028-02-14,000047-Algebraists,64.0,-30.4666666667
218,2028-02-14,000048-Royalty,22.0,-20.5333333333
219,2028-02-14,000049-Mayors,10.0,-7.3333333333
220,2028-02-14,000050-Syntacticians,6.0,-6.0
221,2028-02-14,000051-Assyriologists,45.0,0.0
222,2028-02-14,000052-Subsahara,2.0,0.0
223,2028-02-14,000053-Fiction,33.0,0.0
224,2028-02-14,Overallocation,0.0,130.8666666667
225,2028-02-15,000040-Gabon,26.0,-26.0
226,2028-02-15,000041-Anabaptists,45.0,-45.0
227,2028-02-15,000042-Clergymen,75.0,0.0
228,2028-02-15,000043-Montana,5.0,-0.0666666667
229,2028-02-15,000044-Friars,4.0,-4.0
230,2028-02-15,000045-Cunoniaceae,56.0,-10.8
231,2028-02-15,000046-Goodeniaceae,61.0,-10.2
232,2028-02-15,000047-Algebraists,64.0,-32.0
233,2028-02-15,000048-Royalty,21.0,-21.0
234,2028-02-15,000049-Mayors,10.0,-10.0
235,2028-02-15,000050-Syntacticians,6.0,-6.0
236,2028-02-15,000051-Assyriologists,43.0,0.0
237,2028-02-15,000052-Subsahara,2.0,-0.0666666667
238,2028-02-15,000053-Fiction,33.0,0.0
239,2028-02-15,Overallocation,0.0,102.9333333333
240,2028-02-16,000040-Gabon,25.0,-20.8
241,2028-02-16,000041-Anabaptists,56.0,-41.8
242,2028-02-16,000042-Clergymen,73.0,0.0
243,2028-02-16,000043-Montana,6.0,-0.0666666667
244,2028-02-16,000044-Friars,4.0,-1.0
245,2028-02-16,000045-Cunoniaceae,56.0,0.0
246,2028-02-16,000046-Goodeniaceae,61.0,0.0
247,2028-02-16,000047-Algebraists,64.0,-49.8
248,2028-02-16,000048-Royalty,17.0,-13.2
249,2028-02-16,000049-Mayors,9.0,-5.1333333333
250,2028-02-16,000050-Syntacticians,6.0,-6.0
251,2028-02-16,000051-Assyriologists,43.0,0.0
252,2028-02-16,000052-Subsahara,2.0,0.0
253,2028-02-16,000053-Fiction,33.0,0.0
254,2028-02-16,Overallocation,0.0,124.0
255,2028-02-17,000040-Gabon,25.0,-23.0
256,2028-02-17,000041-Anabaptists,52.0,-29.0666666667
257,2028-02-17,000042-Clergymen,75.0,0.0
258,2028-02-17,000043-Montana,6.0,-0.0666666667
259,2028-02-17,000044-Friars,3.0,-0.33333333330000003
260,2028-02-17,000045-Cunoniaceae,56.0,0.0
261,2028-02-17,000046-Goodeniaceae,61.0,0.0
262,2028-02-17,000047-Algebraists,64.0,-20.8666666667
263,2028-02-17,000048-Royalty,20.0,-17.9333333333
264,2028-02-17,000049-Mayors,9.0,-6.7333333333
265,2028-02-17,000050-Syntacticians,6.0,-6.0
266,2028-02-17,000051-Assyriologists,43.0,0.0
267,2028-02-17,000052-Subsahara,2.0,0.0
268,2028-02-17,000053-Fiction,0.0,0.0
269,2028-02-17,Overallocation,0.0,109.0
270,2028-02-18,000040-Gabon,31.0,-30.2666666667
271,2028-02-18,000041-Anabaptists,58.0,-57.2666666667
272,2028-02-18,000042-Clergymen,91.0,0.0
273,2028-02-18,000043-Montana,9.0,0.0
274,2028-02-18,000044-Friars,4.0,-3.2666666667
275,2028-02-18,000045-Cunoniaceae,56.0,0.0
276,2028-02-18,000046-Goodeniaceae,61.0,0.0
277,2028-02-18,000047-Algebraists,64.0,-25.0
278,2028-02-18,000048-Royalty,24.0,-23.3333333333
279,2028-02-18,000049-Mayors,11.0,-9.6666666667
280,2028-02-18,000050-Syntacticians,6.0,-6.0
281,2028-02-18,000051-Assyriologists,43.0,0.0
282,2028-02-18,000052-Subsahara,2.0,0.0
283,2028-02-18,000053-Fiction,0.0,0.0
284,2028-02-18,Overallocation,0.0,106.8666666667
285,2028-02-19,000040-Gabon,32.0,-32.0
286,2028-02-19,000041-Anabaptists,64.0,-64.0
287,2028-02-19,000042-Clergymen,89.0,0.0
288,2028-02-19,000043-Montana,6.0,-0.0666666667
289,2028-02-19,000044-Friars,5.0,-5.0
290,2028-02-19,000045-Cunoniaceae,56.0,-11.2
291,2028-02-19,000046-Goodeniaceae,62.0,-10.8
292,2028-02-19,000047-Algebraists,64.0,-32.0
293,2028-02-19,000048-Royalty,23.0,-23.0
294,2028-02-19,000049-Mayors,11.0,-11.0
295,2028-02-19,000050-Syntacticians,6.0,-6.0
296,2028-02-19,000051-Assyriologists,43.0,0.0
297,2028-02-19,000052-Subsahara,3.0,0.0
298,2028-02-19,000053-Fiction,0.0,0.0
299,2028-02-19,Overallocation,0.0,69.1333333333
300,2028-02-20,000040-Gabon,0.0,0.0
301,2028-02-20,000041-Anabaptists,0.0,0.0
302,2028-02-20,000042-Clergymen,0.0,0.0
303,2028-02-20,000043-Montana,0.0,0.0
304,2028-02-20,000044-Friars,0.0,0.0
305,2028-02-20,000045-Cunoniaceae,0.0,0.0
306,2028-02-20,000046-Goodeniaceae,0.0,0.0
307,2028-02-20,000047-Algebraists,0.0,0.0
308,2028-02-20,000048-Royalty,0.0,0.0
309,2028-02-20,000049-Mayors,0.0,0.0
310,2028-02-20,000050-Syntacticians,0.0,0.0
311,2028-02-20,000051-Assyriologists,0.0,0.0
312,2028-02-20,000052-Subsahara,0.0,0.0
313,2028-02-20,000053-Fiction,0.0,0.0
314,2028-02-20,Overallocation,0.0,0.0
315,2028-02-21,000040-Gabon,25.0,-25.0
316,2028-02-21,000041-Anabaptists,53.0,-53.0
317,2028-02-21,000042-Clergymen,77.0,0.0
318,2028-02-21,000043-Montana,7.0,0.0
319,2028-02-21,000044-Friars,4.0,-4.0
320,2028-02-21,000045-Cunoniaceae,56.0,-10.4
321,2028-02-21,000046-Goodeniaceae,61.0,-10.6
322,2028-02-21,000047-Algebraists,64.0,-32.0
323,2028-02-21,000048-Royalty,18.0,-18.0
324,2028-02-21,000049-Mayors,10.0,-10.0
325,2028-02-21,000050-Syntacticians,6.0,-6.0
326,2028-02-21,000051-Assyriologists,43.0,0.0
327,2028-02-21,000052-Subsahara,2.0,0.0
328,2028-02-21,000053-Fiction,0.0,0.0
329,2028-02-21,Overallocation,0.0,97.0666666667
330,2028-02-22,000040-Gabon,26.0,-24.8
331,2028-02-22,000041-Anabaptists,44.0,-42.7333333333
332,2028-02-22,000042-Clergymen,75.0,0.0
333,2028-02-22,000043-Montana,5.0,-0.0666666667
334,2028-02-22,000044-Friars,4.0,-2.8666666667
335,2028-02-22,000045-Cunoniaceae,56.0,0.0
336,2028-02-22,000046-Goodeniaceae,61.0,0.0
337,2028-02-22,000047-Algebraists,64.0,-31.0
338,2028-02-22,000048-Royalty,21.0,-19.8
339,2028-02-22,000049-Mayors,11.0,-9.0
340,2028-02-22,000050-Syntacticians,6.0,-6.0
341,2028-02-22,000051-Assyriologists,43.0,-7.8666666667000005
342,2028-02-22,000052-Subsahara,2.0,-0.0666666667
343,2028-02-22,000053-Fiction,0.0,0.0
344,2028-02-22,Overallocation,0.0,73.0

Search terms: make_subplots with plotly.express plots

Sounds like Combining ready-made figures into one, with separation by color - #10 by hoatran, but I want to use one graph; not a div/dcc combination

Could you add more information what exaclty is not working? I see two bar charts if using your code+data

1 Like

The result plot (fig) looks like this

Note e.g. the 149,2028-02-09,Overallocation,0.0,164.1333333333 trace; the xmax is 50.

Removing (or isolating) Overallocation renders as expected.

Also fig_px renders correctly.

I still don’t understand the issue. Keep in mind, that we do not know anything about your data and how you want the graphs to look like. From coding perspective, there is nothing wrong with the snippet you posted.

Keep in mind that by using px.bar() you create a figure object, i.e the layout is included there. By stripping the data from the figure object and using it in subplots as traces, you are loosing all the information concerning layout.

Did you run the script locally? the script displays two plots
… I am expecting that they look the same :confused:

Keep in mind that by using px.bar() you create a figure object, i.e the layout is included there.

I … can see that I am doing something “out of the ordinary”.
I cannot see how differently to achieve the “theoretically” easy task of merging two px.bar plots into one figure with make_subplots and one unified legend.

Try setting the barmode to relative

    fig.update_layout(
        barmode="relative",
        yaxis1={"title": "title"},
        yaxis2={"title": "title"},
        margin={"l": 0, "r": 0, "t": 32, "b": 0},
        legend={
            "title": "title",
            "tracegroupgap": 0,
            "y": 0.92,
        },
    )

Ooohhh, yes - that’s was it. Thank you.

Is there anything / any plans to make such a use-case more easy-to-work with? barmode="relative", vs barmode="stack", (and the for trace in fig_px.data hack) do feel like “plotly-esoteric” to be messed with :confused:

Not sure what you are referring to. But it’s actually not an use case, really IMHO.

I use plotly.express if the figures I’m after are output of the corresponding functions. If I want to create something different, I do the preprocessing of my data (grouping, trace creation…) and use plotly.graph_objects

Disclaimer: My experience with Plotly is limited to amateur-level usage.

I find myself somewhat perplexed by your statemen ttoo :confused:

The documentation for Plotly to me, often lacks detail and clarity. The IDE integration (type-checking, documentation hints, etc) can be inconsistent. One routinely needs to navigate manually to Python API reference for plotly — 5.18.0 documentation and find information, instead of using the search function (since there are the same names, but with different capitalization, e.g.: plotly.graph_objects.Bar and plotly.graph_objects.bar.

px.bar() offers considerable functionality compared to go.Bar, but converting between the two isn’t straightforward.

From what I understand, go.Bar requires a go.Figure to render, something like go.Figure(go.Bar(..)), right?

So why not adopt a similar structure in Plotly Express? Instead of mandatorily a px.bar be a complete figure, there could be a way to “inject” px.bar(...)s into a make_subplots

I understand that “Plotly Express” is an … Express way to … express a complete figure. A small top-level modularity to be able to join plots together does not sound terrible.

yes

I agree, the documentation could be better. Maybe it helps you to know, thatplotly.express uses pandas and plotly.graph_objects under the hood. I think every figure created with plotly.express can be created with plotly.graph_objects- it’s just more manual work.

Maybe. In your case (or at least the code you provided here) I can’t see the necessity to use make_subplots. plotly.express has some functionality concerning subplots, namely the parameters facet_col or facet_row.

Obviously, the Mn-WE was shortened (and anonymized) to fit the topic and the “debugging session”.

I’ll try to take a look at the facet_* arguments if they help :pray:

1 Like

Seems I’m not the first to try :sweat_smile: