Hey guys,
I have several boxplots and donuts on the same figure. Below is provided a simple example of
- the initial issue,
- a workaround I tried to apply, with its flaws
Consider below code:
fig3 = go.Figure(layout=go.Layout(legend=dict(orientation="h", x=0.5, xanchor="center")))
fig3.add_trace(go.Pie(labels=["C1", "C4", "C5"], values=[2, 4, 1], sort=False,domain={'x': [0, 0.3], 'y': [0.1, 0.9]}))
fig3.add_trace(go.Pie(labels=["-1C", "C2"], values=[1, 5], sort=False,domain={'x': [0.35, 0.65], 'y': [0.1, 0.9]}))
fig3.add_trace(go.Pie(labels=["C3", "C4", "C5"], values=[2, 4, 1], sort=False,domain={'x': [0.7, 1], 'y': [0.1, 0.9]}))
fig3.show()
It yields this:
Problem: legends appears in the order they appear in each donut, so, first C1, then C4, then C5, then -1C and C2, and eventually C3.
I cannot use “legendrank”, as it defines the same rank to all the legend of the same donut.
The best workaround I could think of consisted in defining a dummy donut, first, like this:
dummydataset= pd.DataFrame({
'Category': ["-1C", "C1", "C2", "C3", "C4", "C5"],
'Count': [1, 1, 1, 1, 1, 1] # Une colonne avec 1 sur chaque ligne
}) # This dataset us used to force the donuts legends to be displayed in the expected order, before the other traces get added to the figure
fig4 = go.Figure(layout=go.Layout(legend=dict(orientation="h", x=0.5, xanchor="center")))
fig4.add_trace(go.Pie(labels=dummydataset["Category"], values=dummydataset["Count"], sort=False,domain={'x': [0, 0.1], 'y': [0.7, 1]}))
fig4.add_trace(go.Pie(labels=["C1", "C4", "C5"], values=[2, 4, 1], sort=False,domain={'x': [0, 0.3], 'y': [0, 0.7]}))
fig4.add_trace(go.Pie(labels=["-1C", "C2"], values=[1, 5], sort=False,domain={'x': [0.35, 0.65], 'y': [0, 0.7]}))
fig4.add_trace(go.Pie(labels=["C3", "C4", "C5"], values=[2, 4, 1], sort=False,domain={'x': [0.7, 1], 'y': [0, 0.7]}))
fig4.show()
Which gives:
Legends are now sorted, but I would like to hide the dummy donuts. If i set visible=“legendonly”
fig4.add_trace(go.Pie(labels=dummydataset["Category"], values=dummydataset["Count"], sort=False,domain={'x': [0, 0.1], 'y': [0.7, 1]}, visible="legendonly"))
Then the group of legend of the dummy donut is replaced by a black square, and the 3 other donuts legends are in their original order again:
I tried to set opacity = 0 to the dummy donut, but then the legenditem don’t have any color.
I can’t use z-index to force the dummy piechart to be behind everything else, so, how would you proceed ?
Is there a cleaner way than setting a microscoping size and texposition to none, like below
fig4.add_trace(go.Pie(labels=dummydataset["Category"], values=dummydataset["Count"], sort=False,domain={'x': [0, 0.00000000001], 'y': [0.00, 0.00001]}, textposition="none"))
Which gives