Black Lives Matter. Please consider donating to Black Girls Code today.

Still - Open: Getting different marker shapes in a Dash app

Is there a way to iterate through a list of marker symbols for a dash lines+markers graph? I am colorblind so 4 lines with circles don’t mean anything to me (even with the legend).

here’s my code for the dash app and there are 4 different study arms that I would like to have 4 different marker symbols. I’ve tried all the tricks that I know for jupyter notebook, but those aren’t working for Dash:

cart_pv = pd.pivot_table(jsw,index=['visit_label'], columns = ['study_arm'], values=['Med_JSW', 'Lat_JSW', 'Med_JSN', 
'Lat_JSN'])

xax = cart_pv.index


cartilage_cols = ['Med_JSW', 'Lat_JSW', 'Med_JSN', 'Lat_JSN']
study_arms =  list(set(jsw['study_arm']))

app = dash.Dash()
app.layout = html.Div(children=[
    html.H1(children='some title'),
    html.Div(children='''Location Options'''),

    
    dcc.Dropdown(
                id='location',
                options=[{'label': i, 'value': i} for i in cartilage_cols],
                value=cartilage_cols[0]
            ),
    
    dcc.Graph(id='cartilage')
    
])
    
@app.callback(
    dash.dependencies.Output('cartilage', 'figure'),
    [dash.dependencies.Input('location', 'value')])
def update_graph(location_name):
    traces = []
    for i in study_arms:
        yax = cart_pv[:][location_name,i]
        trace = {'x': xax, 'y': yax, 'name': i}
        traces.append(trace)

    
    return {'data': traces,
                   
        
        'layout': go.Layout(title='some graph title')
        
    }    


    #debug=True might cause Flask web server to hang on Exit
if __name__ == '__main__':
    app.run_server(debug=False)

Below is a picture of the graph. Any help would be much appreciated.

Hi @t3c

Here is the list of available marker symbols https://plot.ly/python/reference/#scatter-marker-symbol You could just add them and iterate like below:

symbols = ['circle', 'square', 'cross', 'star']

...

for i in study_arms:
        yax = cart_pv[:][location_name,i]
        trace = {'x': xax, 'y': yax, 'name': i, 'marker': dict(symbol=symbols[i])}
        traces.append(trace)



Thank You! However, after adding the list and the code provided, I get this error message:

Code below for reference:

Hello,

I tried the modified code of ‘marker’: dict(symbol=symbols[i]) below and still getting an error message.

Any chance you can take another look (I modified the error messages with the latest based on the code above)?

Sorry for any inconvenience on your part.

Kind Regards,

-Todd

I am still needing some help on this if possible.

I recommend placing a debugger in your code so that you can debug the code yourself by printing out different variables. To insert a debugger:
install ipdb:

pip install ipdb

insert a debugging statement in your code

import ipdb; ipdb.set_trace()

and then you should be able to print out different variables. You can insert the statement directly above where you are having trouble.

Thank You. However, this just points to the marker line code as the error. I will scrap this and go back to jupyter notebook. Much appreciated for the feedback. The colorblindness makes it difficult to work in something that’s not easy to iterate through different marker shapes. Jupyter’s way is much easier.

It looks like the I variable you are attempting to use to reference the list of symbols is a string, and not an integer. You can fix this by changing

for i in study_arms:

to

for i, name in enumerate(study_arms):

1 Like

@benn0 - that was it! Thank you very much!