Hello,
I plot a GeoJSON file. I use the following code to get the lat, lon and names from the GeoJSON file.
Example from Plotly.com:
import plotly.express as px
import geopandas as gpd
import shapely.geometry
import numpy as np
import wget
# download a zipped shapefile
wget.download("https://plotly.github.io/datasets/ne_50m_rivers_lake_centerlines.zip")
# open a zipped shapefile with the zip:// pseudo-protocol
geo_df = gpd.read_file("zip://ne_50m_rivers_lake_centerlines.zip")
lats = []
lons = []
names = []
for feature, name in zip(geo_df.geometry, geo_df.name):
if isinstance(feature, shapely.geometry.linestring.LineString):
linestrings = [feature]
elif isinstance(feature, shapely.geometry.multilinestring.MultiLineString):
linestrings = feature.geoms
else:
continue
for linestring in linestrings:
x, y = linestring.xy
lats = np.append(lats, y)
lons = np.append(lons, x)
names = np.append(names, [name]*len(y))
lats = np.append(lats, None)
lons = np.append(lons, None)
names = np.append(names, None)
Now, instead of:
fig = px.line_mapbox(lat=lats, lon=lons, hover_name=names,
mapbox_style="stamen-terrain", zoom=1)
fig.show()
like on Plotly.com I use graph objects like this:
fig = go.Figure(go.Scattermapbox(
mode = "lines",
lon = lons,
lat = lats,
color='names',
text=Names
fig.show()
Since I have 20 different lines with their own name. My question now is, how can I get the lines to have seperate colors and have the legend to show me each line? Right now there is one element in the legend and the color of the lines is the same.