简体   繁体   中英

Python plotly Scattermapbox define colors by category

I want to draw some colored areas on a map. The coordinates are defined in a dataframe and I want each area to have a different color depending on the test_type value.

How can I do this? (Question is similar to a previous unanswered post )

import numpy as np
import pandas as pd
import plotly.graph_objects as go

# my test dataset
df = pd.DataFrame(data={'names':['area1','area2','area3'], 'coords': 0})
df['coords'] = df['coords'].astype(object)
df.at[0,'coords'] = [[-73.606352888, 45.507489991], [-73.606133883, 45.50687600], [-73.617533258, 45.527512253], [-73.618074188, 45.526759105], [-73.618271651, 45.526500673], [-73.618446320, 45.526287943], [-73.620201713, 45.524298907], [-73.620775593, 45.523650879]]
df.at[1,'coords'] = [[-73.706352888, 45.507489991], [-73.706133883, 45.50687600], [-73.717533258, 45.527512253], [-73.718074188, 45.526759105], [-73.718271651, 45.526500673], [-73.718446320, 45.526287943], [-73.720201713, 45.524298907], [-73.720775593, 45.523650879]]
df.at[2,'coords'] = [[-73.606352888, 45.497489991], [-73.606133883, 45.49687600], [-73.617533258, 45.517512253], [-73.618074188, 45.516759105], [-73.618271651, 45.516500673], [-73.618446320, 45.516287943], [-73.620201713, 45.514298907], [-73.620775593, 45.513650879]]
df['test_type'] = ['alpha','beta','beta']

def get_close_coords(coords):
    # points must define a closed area
    c = np.zeros((len(coords) + 1, 2))
    c[:-1, :] = coords
    c[-1, :] = c[0, :]
    return c

fig = go.Figure(go.Scattermapbox(mode = "markers", lon = [-73.605], lat = [45.51]))

for i, c in enumerate(df.coords):
    coords = get_close_coords(c)
    fig.add_trace(go.Scattermapbox(lat=coords[:, 1], lon=coords[:, 0], color=df.test_type[i], mode="lines", fill="toself", name="Area %s" % i, opacity=0.4))

fig.update_layout(
    mapbox = {
        'style': "stamen-terrain",
        'center': {'lon': -73.6, 'lat': 45.5},
        'zoom': 12,
        'color': df.test_type,
    },
    margin = {'l':0, 'r':0, 'b':0, 't':0})
fig.show()

First, the color setting is not simply a color specification, but a marker attribute, which is set by the marker specification in the scattermapbox . Then the color value must be an rgb value, hex color value, or color name. For this purpose, a column of colors to be referenced is added to the original data.

fig = go.Figure(go.Scattermapbox(mode="markers", lon=[-73.605], lat=[45.51]))
                               
for i, c in enumerate(df.coords):
    coords = get_close_coords(c)
    fig.add_trace(go.Scattermapbox(lat=[x[1] for x in coords], 
                                   lon=[x[0] for x in coords],
                                   mode="lines",
                                   marker=go.scattermapbox.Marker(
                                       color=df.test_type_color[i]
                                   ),
                                   fill="toself",
                                   name="Area %s" % i,
                                   opacity=0.4
                                  ))

fig.update_layout(
    mapbox = {
        'style': "stamen-terrain",
        'center': {'lon': -73.6, 'lat': 45.5},
        'zoom': 11,
        #'color': df.test_type,
    },
    margin = {'l':0, 'r':0, 'b':0, 't':0})
fig.show()

在此处输入图像描述

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