rohith santosh
rohith santosh

Reputation: 40

How to add colorscale to bar graph in dash python

I am plotting a bar graph that updates every second I want to add colorscale to it.

import plotly.graph_objects as go

@app.callback(Output('my-graph', 'figure'),
              [Input('my-interval-component', 'n_intervals')])
def update_graph_soc(n):
    connection = sqlite3.connect(r'C:\Users\user\Desktop\database\newbase.db', )
    connection.execute('pragma journal_mode=wal')
    data = connection.cursor()
    latest = None
    try:
        data = connection.cursor()
        latest = (data.execute("""SELECT
                                    v1,v2,v3,v4
                                    SNO FROM sop where SNO ==(SELECT MAX([SNO]) FROM [sop])""").fetchall().__getitem__(0))
    except Exception as e:
        print(e)
    z = []
    values = go.Bar(
        x=['v1','v2','v3','v4'], y=latest)
    return {'data': [values], 'layout': go.Layout(title='graph',yaxis_title='values(in x)',
                                                  xaxis_title='sample: ' + str(int(latest[4])),colorscale='Viridis')}

I was trying to do it in the above way where I was giving Viridis as colorscale but it is not working. Any help will be appreciated Thanks in advance.

Upvotes: 1

Views: 621

Answers (1)

Derek O
Derek O

Reputation: 19600

@eliasdabbas answered this in the Plotly Community Forum: you need to pass colorscale to the marker parameter dictionary instead of the layout:

values = go.Bar(
    x=['v1','v2','v3','v4'], 
    y=latest, 
    marker={'color': [i+1 for i,_ in enumerate(y)], 'colorscale': 'Viridis'}
)

Upvotes: 2

Related Questions