Subplots go.Figure()

Hello everyone.

I have coded the following image :

And now I would like to turn it into subplots. I’m just looking for to get the principle so I’m fine with any subplots with my plot in. I will adapt it then.

Here’s my code :

from plotly import graph_objects as go

data = {
    "Objectif":[15, 23, 32],
    "Repas 1": [4,   8, 18],
    "Repas 2": [11, 18, 18],
    "Repas 3":[20,32,22],
    "labels": [
        "Protéine",
        "Glucide",
        "Lipide",
    ]
}

print(pd.DataFrame(data))

data_plot=[
        go.Bar(
            name="Objectif",
            x=data["Objectif"],
            y=data["labels"],
            offsetgroup=0,
            orientation = "h",
            text = data["Objectif"],
            textposition='auto',
            textfont_size=12
        )
]
nb_meal = 3
for meal in range(1,nb_meal+1):
    print(meal)
    data_plot.append(
        go.Bar(
            name=f"Repas {meal}",
            x=data[f"Repas {meal}"],
            y=data["labels"],
            offsetgroup=1,
            orientation = "h",
            text = data[f"Repas {meal}"],
            textposition='auto',
            textfont_size=12,
            base=None if meal == 1 else [sum(x) for x in zip(*[data[f"Repas {i}"] for i in range(1,meal)])]
        )
    )

fig = go.Figure(
    data_plot,
    layout=go.Layout(
        title="Issue Types - Original and Models",
        yaxis_title="Number of Issues"
    )
)

                  
fig.show()

I’ve been searching for hours to turn this code into subplots, maybe the way I code it is not suitable to become subplots… But I really wanted multiples double bar. And the only way I’ve found is to use go.figure(data = data, …)
So I can change my code it’s fine as long as the plot does not change, any which works is good haha.

Thank you for your help.

Best regards.

The docs for subplots are here: Subplots in Python and you can see that it’s fairly straightforward, you just need to create a figure with subplots populated with go.Figure().set_subplots() (at the bottom of the page) and then add your traces using fig.add_traces(row=..., col=...) instead of passing your data_plot variable to the go.Figure() constructor as you’re doing now.

1 Like

(You can of course from plotly.subplots import make_subplots and use the make_subplots() function instead of go.Figure().set_subplots() as well, as documented at the top of the page :slight_smile: )

1 Like

Hi nicolaskruchten

Thank you for your help.

I’m having a discussion about the same subject in a other topic. So my answer will be almost the same here.
I said that I didn’t think about

go.Figure(data=data).set_subplots(2, 3, horizontal_spacing=0.1)

I didn’t see this function on stack exchange during my research. But I’m not sure I’ve understood how to use it, once you’ve used set_suplots the next step is to use add_traces and that is exactly what causes trouble in the first place.
For example how could we make a subplots (1,2) with my plot at the place (1,1) and at the place (1.2) ?

We can also use my plot with this following plot. Because for me the issue is the same, I’m stuck when I want to convert plot with data = data into subplots.

data = [# Portfolio (inner donut)
        go.Pie(values=[20,40],
        labels=["Reds","Blues"],
        domain={"x":[0.2,0.8], "y":[0.1,0.9]},
        hole=0.5,
        direction="clockwise",
        sort=False,
        marker={"colors":["#CB4335","#2E86C1"]}),
        # Individual components (outer donut)
        go.Pie(values=[5,15,30,10],
        labels=["Medium Red","Light Red","Medium Blue","Light Blue"],
        domain={"x":[0.1,0.9], "y":[0,1]},
        hole=0.75,
        direction="clockwise",
        sort=False,
        marker={"colors":["#EC7063","#F1948A","#5DADE2","#85C1E9"]},
        showlegend=False)
                 ]

fig = go.Figure(data=data, layout={"title":"Nested Pie Chart"})

fig.show()

Thank you for your help,

Best regards

I guess it is not possible…

Hi,

Do you have any particular reason why you would want to initialize the Figure object with traces on it?

go.Figure.set_subplots will convert your figure as subplot, but it won’t modify the traces. So in a way it is equivalent of constructing a subplot without traces and add the traces (via fig.add_trace) without specifying row and col (it will default to the first row and first column). You can certainly modify the traces to move some of them to other subplots and here it is a quick example based on your code:

data = [
    go.Scatter(
        x=[1, 2],
        y=[3, 4]
    ),
    go.Scatter(
        x=[1, 2],
        y=[5, 6]
    )
]

fig = go.Figure(data=data).set_subplots(1, 2)

fig.data[1].xaxis = "x2"
fig.data[1].yaxis = "y2"

So, in the case of cartesian traces, it is enough to change the axis to the one defined for the second subplot.

Since you asked about pie charts, I should also mention that they are from a “domain” type (see here for more info on that) and you would have to 1- specify in the subplot specs the type explicitly (or it will assume they are all cartesian) and 2- change the domain on each trace to restrict the pie charts to the subplot domain. The last step is “equivalent” to change the x and y axis in the example I provided, as the domain of cartesian traces is specified via the domain of their axis.

Hope this clarifies a bit! :slight_smile:

Hello jlfsjunior !

Thank you for you answer, I have no particular reason to initialize the Figure object with traces on it, any code which will provides a solution to subplots (1,2) with the two figures above or one of them twice will be the answer ! I mean I expect to be able to generalize it to my code ^^.

(post deleted by author)

I’ve made a photo on paint to explain what I’m struggling to do :