Hi all,
I am trying to implement a violin plot in Dash Framework.
Here the X (color) will be a value from dropdown and y will be another value from dropdown.
But when I am returning the fig to the browser it’s showing error:
import dash
from dash.dependencies import Input, Output
from dash import dash_table
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objects as go
df=pd.read_csv('data/chronos.csv')
cols=set(df.columns)
meta_df=pd.read_csv('data/metadata.csv')
tot_col=set(meta_df.columns)
app = dash.Dash(__name__)
app.layout = html.Div([
html.H1("Elucidata Dashboard"),
html.Hr(),
html.Div([
dcc.Dropdown(
id='x_file-dropdown',
options=[
{'label': 'Chronos_data', 'value': 'chronos.csv'},
{'label': 'CN_data', 'value': 'cn.csv'},
{'label': 'Expression_data', 'value': 'expression.csv'}
],
value='chronos.csv'
),
]),
html.Div([
dcc.Dropdown(
id='x_col-dropdown',
options=[
{'label':i ,'value':i }
for i in cols
],
value='HOXD8'
),
]),
html.Div([
dcc.Dropdown(
id='color_col-dropdown',
options=[
{'label':i ,'value':str(i) }
for i in tot_col
],
value='source'
),
]),
html.Br(),
html.Div( id='datatable-interactivity'),
])
@app.callback(
Output('datatable-interactivity', 'children'),
[Input('x_file-dropdown', 'value'),Input('x_col-dropdown', 'value'),Input('color_col-dropdown', 'value')]
)
def update_table(x_file,x_col,cols):
xdf = pd.read_csv('data/'+x_file)
print(x_file)
xdf1=xdf[['Sample_ID',x_col]]
zdf1=meta_df[['Sample_ID',cols]]
final_df = pd.merge(xdf1, zdf1, on="Sample_ID")
final_df1=final_df.dropna()
meta_colors = set(final_df1[cols])
for mc in meta_colors:
fig.add_trace(go.Violin(x=final_df1[cols][df[cols] == mc],
y=df[x_col][df[cols] == mc],
name=mc,
box_visible=True,
meanline_visible=True))
#fig.show()
return html.Div([
dcc.Graph(figure=fig)
])
if __name__ == '__main__':
app.run_server(debug=True)
In server the error is coming.
Error:
Traceback (most recent call last):
File "C:\Users\Sawon\Desktop\elucidata\viz2.py", line 72, in update_table
fig.add_trace(go.Violin(x=final_df1[cols][df[cols] == mc],
NameError: name 'fig' is not defined
Thanks in advance.