I’m wondering if it’s possible to add horizontal lines for specific bars in a bar chart. I’ve attached a random bar plot with added red lines here. Essentially I would like to add a dotted line that shows a specific value for each bar. Any ideas? Thanks in advance!
Are you looking for arbitrary lines or do they actually mean something in your data.
You could just utilize the stacked/grouped bar graph in both plotly.express or plotly.graph_objects if the lines mean something in your data.
If you’re just trying to to make some arbitrary lines you could do the following:
import plotly.graph_objects as go
animals=['giraffes', 'orangutans', 'monkeys']
fig = go.Figure(
go.Bar(x=animals, y=[12, 14, 21],showlegend=False), #This would be the "lines" . Set showlegend=False so this trace won't show in legend
go.Bar(name='SF Zoo', x=animals, y=[25, 18, 33]), #This would be the data for the bar
fig.update_layout(barmode='stack') #set the bars to stack
fig.update_traces(marker_color='red', marker_line_color='black', marker_line_width=2) #Marker_color will set the bar color, marker_line_color will set the "line" colour
Thanks for the response, I think this is a workaround for a regular bar chart. However, I have a floating bar chart, which indicates the minimum and maximum of my data, if I do it your way, the added bar is going to cover the minimum part of my bars.
I have a thought based on your idea, if I change the opacity of the added bar to 0, it’s gonna work well. However, when I set the opacity to 0, the line also disappear
I was able to find another work around, by adding another set of floating bars that have very short length – those essentially look like lines.
Hi @dzheng4 ,
would you mind sharing the code of your solution?
Sure, I’ll use Payton’s animals graph as an example.
In stead of adding the first go.bar “go.Bar(x=animals, y=[12, 14, 21],showlegend=False)”, we’ll do the following:
x = animals,
y = [1, 1, 1, 1], # Width of the "line"
base = [12 - 1, 14 - 1, 21 - 1] , # Here 12,14,21 are the values of the lines we want to show for each bar.