Python Plotly - how to add multiple colors to each trace from a unique Scattergeo object?

Viewed 2248

I'm developing a dashboard application using dash/plotly in Python.

I need to build a map with more than 1000 traces. Because of performance issues, I'm trying to use one single Scattergeo object, but, ordinarily, it does not provide a way to plot each "trace" with a different color.

I would like that each trace contains a different color. For markers, each one can have a single color, but using lines is not possible.

The picture below illustrates what I said above.

enter image description here

Is there any workaround that I could use to solve this plotly issue?

I send a reproducible code below.

Thank you in advance.

Reproducible code:

##################################################
# plotly
##################################################

# Source: https://plotly.com/python/lines-on-maps/

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scattergeo(
    lat = [
        40.7127, 51.5072, None,
        30.7127, 41.5072, None,
        20.7127, 31.5072
    ],
    lon = [
        -74.0059, 0.1275, None,
        -64.0059, -10.1275, None,
        -54.0059, -20.1275
    ],
    mode = 'lines',
    line = dict(
        width=4,
        color = 'blue',
        # I would like something like this:
        # color = [
        #     'red',
        #     None,
        #     'yellow',
        #     None,
        #     'green'
        # ],
    ),
))

fig.add_trace(go.Scattergeo(
    lat = [
        40.7127,
        30.7127,
        20.7127
    ],
    lon = [
        -74.0059,
        -64.0059,
        -54.0059,
    ],
    mode = 'markers',
    marker = dict(
        size = 10,
        color = ['red', 'yellow', 'green'],
        line = dict(
            width = 3,
            color = 'rgba(68, 68, 68, 0)'
        ),

    )
))

fig.update_layout(
    title_text = 'Traces',
    showlegend = False,
    geo = dict(
        resolution = 50,
        showland = True,
        showlakes = True,
        landcolor = 'rgb(204, 204, 204)',
        countrycolor = 'rgb(204, 204, 204)',
        lakecolor = 'rgb(255, 255, 255)',
        projection_type = "equirectangular",
        coastlinewidth = 2,
        lataxis = dict(
            range = [0, 70],
            showgrid = True,
            dtick = 10
        ),
        lonaxis = dict(
            range = [-100, 20],
            showgrid = True,
            dtick = 20
        ),
    )
)

##################################################
# dash
##################################################

import dash
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash()
app.layout = html.Div([
    dcc.Graph(figure=fig)
])

app.run_server(debug=True, use_reloader=True)
1 Answers

You can create a list containing your latitudes, longitudes, and colors. Then loop through the lists, adding your traces one at a time.

import plotly.graph_objects as go

fig = go.Figure()

## create lists containing your information
lat_list = [[40.7127, 51.5072, None],[30.7127, 41.5072, None],[20.7127, 31.5072]]
lon_list = [[-74.0059, 0.1275, None],[-64.0059, -10.1275, None],[-54.0059, -20.1275]]
colors_list = ['red','yellow','green']

for idx in range(len(colors_list)):
    fig.add_trace(go.Scattergeo(
        lat = lat_list[idx],
        lon = lon_list[idx],
        mode = 'lines',
        line = dict(
            width=4,
            color = colors_list[idx],
        ),
    ))

fig.add_trace(go.Scattergeo(
    lat = [
        40.7127,
        30.7127,
        20.7127
    ],
    lon = [
        -74.0059,
        -64.0059,
        -54.0059,
    ],
    mode = 'markers',
    marker = dict(
        size = 10,
        color = ['red', 'yellow', 'green'],
        line = dict(
            width = 3,
            color = 'rgba(68, 68, 68, 0)'
        ),

    )
))

fig.update_layout(
    title_text = 'Traces',
    showlegend = False,
    geo = dict(
        resolution = 50,
        showland = True,
        showlakes = True,
        landcolor = 'rgb(204, 204, 204)',
        countrycolor = 'rgb(204, 204, 204)',
        lakecolor = 'rgb(255, 255, 255)',
        projection_type = "equirectangular",
        coastlinewidth = 2,
        lataxis = dict(
            range = [0, 70],
            showgrid = True,
            dtick = 10
        ),
        lonaxis = dict(
            range = [-100, 20],
            showgrid = True,
            dtick = 20
        ),
    )
)

fig.show()

enter image description here

Related