Black Lives Matter. Please consider donating to Black Girls Code today.

Python: Bubble chart/Box plot from a dataframe

My prices_top10 df has top brands and respective prices. I want to plot median, high, highest, low, lowest in box plot for all 10 brands, and none of these code works:

Try 1:

fig1_2 = go.Figure()

traces = list()

for brandname in prices_top10.brandname.unique():

    traces.append(go.Box(y=prices_top10[prices_top10.brandname == brandname].price.tolist(),

                                        name = brandname

                                        )

                    )

fig1_2.add_traces(traces)

fig.append_trace(traces, 2, 1)

Try 2:

fig1_2 = go.Figure()

traces = list()

for brandname in prices_top10.brandname.unique():

    fig1_2.add_trace(go.Box(y=prices_top10[prices_top10.brandname == brandname].price.tolist(),

                                        name = brandname

                                        )

                    )

fig.append_trace(fig1_2, 2, 1)

I get the following error:

ValueError:
Invalid element(s) received for the ‘data’ property of
Invalid elements include: [[Box({
‘name’: ‘Yoplait’,
‘y’: [5.299847, 1.541767, 1.541767, 1.875003, 5.040011, 3.187395, 5.299847,
7.737461, 1.875003, 0.940988, 5.224979, 0.550133, 1.541767, 1.875003,
1.041913, 1.541767, …

Hi @porel,
welcome to the forum! The error comes from the fact that you have a list of lists of traces for the data property of your figure. It should be a list of traces instead. In the for loop, you can directly call fig.append_trace to add your trace.

Thank you, works like a charm. Here is the updated code:

for brandname in prices_top10.brandname.unique():

    fig.append_trace(go.Box(y=prices_top10[prices_top10.brandname == brandname].price.tolist(),

                                        name = brandname

                                        ), 2, 1

                    )

I had to provide position since I am adding this as a subplot to row 2 column 1