简体   繁体   中英

Color specific bar (discrete or continuous) like plotly express based on X axis value

I want to use bar and line chart in same axis with bar having a color code based on a different variable and pass that as a return in Dash.In plotly express a color argument is passed into trace but in dash its not retruning fig to dcc.graph.Can someone advice me where I am going wrong?

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objs as go
import pandas as pd
import time
import plotly.express as px
import numpy as np
pd.set_option(‘display.width’, 400)
pd.set_option(‘display.max_columns’, 50)

df1 = pd.read_csv(r"E:\OI\test_mar_2020.csv")
df1[‘Date’] = pd.to_datetime(df1[‘Date’])
df1[‘Monthly_Exp_End’] = pd.to_datetime(df1[‘Monthly_Exp_End’])
df1[‘Monthly_Exp_Start’] = pd.to_datetime(df1[‘Monthly_Exp_Start’])
df1[‘Monthly_exp_end_ind’] = np.where(df1[‘Monthly_Exp_End’]==df1[‘Date’] , True, False)
df = df1[df1[‘Year’].isin([2020])]

external_stylesheets = [‘https://codepen.io/chriddyp/pen/bWLwgP.css’]

app = dash.Dash(name, external_stylesheets=external_stylesheets)

party_data = 
for client in df[‘Client_Type’].unique():
party_data.append({‘label’: client, ‘value’: client})


app.layout = html.Div([
html.Div([
dcc.Dropdown(id=‘client-picker’,options=party_data,value= ‘Test1’ )]),
dcc.Graph(id=‘graph’)
])

@app.callback(Output(‘graph’, ‘figure’), [Input(‘client-picker’, ‘value’)])
def update_figure(client_picker_name):
df_new = df[df.Client_Type == client_picker_name]

I want to add a color argument lile plotly.express— fig = px.scatter(df, x=“sepal_width”, #y=“sepal_length”, color=“species”) into below trace1

 trace1 = go.Bar(x=df_new.Date, y=df_new.colA)
 trace2 = go.Bar(x=df_new.Date, y=df_new.colB)
 trace3 = go.Scatter(x=df_new.Date, y=df_new.colC, mode=‘lines+markers’, yaxis=‘y2’)

return {
‘data’ : [trace1,trace2,trace3]
}

if name == ‘main’:
app.run_server(debug=True,port = 8080)

You need to pass color parameter inside marker .

trace1 = go.Bar(x=df_new.Date, y=df_new.colA, marker=dict(color='green'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM