i am using subplots to display several bar charts, most of which are grouped, but i want 1 bar chart to be stacked. i cannot see how to set the 1 bar chart to be stacked using the fig.update() method.
fig.update_layout(barmode=“stack”, row=r+1, col=c+1), this fails as it does not take rows and cols.
fig.update_xaxes(title_text=“Plan/Actual”, row=r+1, col=c+1), this works fine, but there seems to be nothing for example fig.update_barplot()
drawing a single chart for stacked bar, i can do this.
fig = go.Figure(
data=[
go.Bar(x=[“Plan”, “Actual”], y=[0, 100], showlegend=False),
go.Bar(x=[“Plan”, “Actual”], y=[125, 110], showlegend=False),
],
layout=go.Layout(width=400, height=600, barmode=‘stack’,
title=go.layout.Title(text=“Cost Gross/Nett”, font=dict(family=‘Arial’, size=14), x=0.5)
)
)
but how do i set my subplot bar chart to be barmode=‘stack’
Welcome @bruceg ! It looks like setting barmodes per subplot is not supported (update_layout updates parameters of the entire figure). However it seems some people have found workarounds, see this thread:
Ok I looked up the combination of grouped and stack item and found it to work for me, by adjusting the width and offset of the bars. Thanks.
I also have some other questions on subplots and adding shapes and annotations to the subplots. When adding these items to subplots they do not all render properly. Sometimes the annotation is not displayed at all and sometimes the shape and or annotation is displayed on the wrong subplot.
Also on the indicator gauge chart, is it possible to show a pointer? As it is now, all I can see, is to show a bar. I think showing a pointer to indicate the value looks much better.
Hi @bruceg, I’m glad you solved the bar problem.
As for the annotations, it’s hard to know exactly what’s going on, but when you add a shape, you can use fig.add_shape(dict(...describe shape...),row=<row>,col=<col>) to specify in which subplot the shape should go. You can do a similar thing with fig.add_annotation. I was going to refer you to the documentation, but it seems the annotation documentation doesn’t show adding to a subplot. The shapes documentation does, check it out here
To show a pointer, you can use the showarrow and arrowhead arguments for add_annotation, read more on this here
I have added a Word document to try and show you the issues with the gauges, add an arrow, and the sub plots adding annotations, where I am having problems.
Can you have a look at this and see if we can move forward with any solutions.
(Attachment Plotly Sub plots issues and gauge issues.docx is missing)
Here is the gauge with an arrow added, I can see how to add the arrow but it does not look very good. How can you easily control the arrow to point to the value you want to show. In this example I just draw a vertical arrow, which is easy. But let’s say we want the arrow to point to a point between horizontal and vertical, how to control that? We would have to use sin and cos values to get the co-ordinates of the desired end point.
That’s right, then you use those coordinates as the x and y values of the annotation. Depending on the version of plotly you have, the other end of the arrow (the end with the text) must be specified using relative coordinates in pixels by specifying ax and ay. In a newer version, I believe plotly.js 1.57, ax and ay can be absolute but they have to be the same units as x and y. So the whole annotation assignment would look something like this in plotly.py:
# assuming you're replacing an annotation already in the figure
fig['annotations'][0]=dict(
xref='x',
yref='y',
x=x_calculation_using_cos(...),
y=y_calculation_using_sin(...),
axref='x',
ayref='y',
ax=base_of_arrow_x,
ay=base_of_arrow_y
)