6

How do I make the bar appear when one of the value of y is zero? It just leaves a gap by default. Is there a way I can enable it to plot for zero values? I am able to see a line on the x-axis at y=0 for the same if just plotted using go.Box. I would like to see this in the Bar plot as well. So far, I set the base to zero. But that doesn't plot for y=0 either. Here is my sample code. My actual code contains multiple traces, that's why I would like to see the plot for y=0 Here is the sample python code:

 import plotly.graph_objects as go
 fig = go.Figure()
 fig.add_trace(go.Bar(x=[1, 2, 3], y=[0, 3, 2]))
 fig.show()
vestland
  • 55,229
  • 37
  • 187
  • 305
MissionAstro
  • 93
  • 1
  • 5

2 Answers2

6

Bar charts come with a line around the bars that by default are set to the same color as the background. In your case '#E5ECF6'. If you change that, the line will appear as a border around each bar that will remain visible even when y = 0 for any given x.

fig.update_traces(marker_line_color = 'blue', marker_line_width = 12)

If you set the line color to match that of the bar itself, you'll get this:

Plot 1: Bars with identical fill and line colors

enter image description here

If I understand correctly, this should be pretty close to what you're trying to achieve. At least visually. I would perhaps consider adjusting the yaxis range a bit to make it a bit clearer that the y value displayed is in fact 0.

Plot 2: Adjusted y axis and separate colors

enter image description here

Complete code for Plot 1:

import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Bar(x=[1, 2, 3], y=[0, 3, 2], marker_color = 'blue'))
fig.update_traces(marker_line_color = 'blue', marker_line_width = 12)
fig.show()

Complete code for Plot 2:

import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Bar(x=[1, 2, 3], y=[0, 3, 2], marker_color =  '#00CC96'))
f = fig.full_figure_for_development(warn=False)
fig.update_traces(marker_line_color = '#636EFA', marker_line_width = 4)

fig.update_yaxes(range=[-1, 4])
fig.show()

Edit after comments

Just to verify that the line color is the same as the background color using plotly version 5.4.0

Plot 1:

enter image description here

Plot 2: Zoomed in

enter image description here

vestland
  • 55,229
  • 37
  • 187
  • 305
  • Beautiful! Thank you. Very close to what I was looking for. But, if the line around the borders are by default has same color as background, then by increasing just the `marker_line_width` arg the line at y axis should become visible, correct? But looks like it would still be invisible if you don't explicitly set the marker_line_color. – MissionAstro Jan 11 '22 at 01:14
  • @KiranPrakash You may experience different behavior on different versions of Plotly. I'm on `5.4.0`. I'll include some details in my answer. – vestland Jan 11 '22 at 08:49
  • You are right, I had to zoom in. I was using 5.3.0 and upgraded to 5.5.0. Thanks for the edits. Isn't there a way to tell plotly to auto-pick the border color same as plot color instead of setting background color? The box plot for the same, chooses the border color little darker than the plot by default. – MissionAstro Jan 12 '22 at 05:52
  • What I meant in my previous comment is that, if there is a way to set the marker_line_color = marker_color automatically. One way would be to use the color code from the figure object. But that gets complicated with stacked bar plot containing wide data formats. – MissionAstro Jan 12 '22 at 07:07
3

It is possible to create a border using marker

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Bar(x=[1, 2, 3], y=[0, 3, 2])).update_traces(
    marker={"line": {"width": 3, "color": "rgb(0,0,0)"}}
).update_layout(yaxis={"range": [-1, 4]})

enter image description here

Rob Raymond
  • 29,118
  • 3
  • 14
  • 30