hunny
hunny

Reputation: 11

Changing colors based on columns and adding error bars on the bar graph using plotly? in VS code

I would like to change the color based on elements column and also want to add error bars on graph. My code is given below. Also want to change the labels?

   ```import plotly.graph_objects as go

     Elements=["A", "B", "C", "D", "E", "F", "G", "H"]
     Error=[0.2,0.1,0.05,0.1, 0.2, 0.5,0.1,0.3]
     fig = go.Figure()
     fig.add_trace(go.Bar(
     y=[0.1, 2, 5, 7, 5, 4, 1.5, 1.2
     ],
     x=Elements,
     name='Element 1',
     marker_color='indianred'
     ))
     fig.add_trace(go.Bar(
     y=[3.3,1.9, 0.2, 1.5, 2.1, 1.7, 3.7,1.5
     ],
     x=Elements,
     name='ELement 2',
     marker_color='lightsalmon',
     ))

     fig.update_layout(barmode='group', xaxis_tickangle=-45)
     fig.show()```

Upvotes: 1

Views: 57

Answers (1)

r-beginners
r-beginners

Reputation: 35205

To add an error bar, see here in the reference. In a graph object, it is specified in dictionary form. How would you like your labels to look like?

import plotly.graph_objects as go

Elements=["A", "B", "C", "D", "E", "F", "G", "H"]
Error=[0.2,0.1,0.05,0.1, 0.2, 0.5,0.1,0.3]

fig = go.Figure()

fig.add_trace(go.Bar(
    y=[0.1, 2, 5, 7, 5, 4, 1.5, 1.2],
    x=Elements,
    name='Element 1',
    marker_color='indianred',
    error_y=dict(type='data', array=Error)
))
fig.add_trace(go.Bar(
    y=[3.3,1.9, 0.2, 1.5, 2.1, 1.7, 3.7,1.5],
    x=Elements,
    name='ELement 2',
    marker_color='lightsalmon',
    error_y=dict(type='data', array=Error)
))

fig.update_layout(barmode='group', xaxis_tickangle=-45)
fig.show()

enter image description here

Upvotes: 1

Related Questions