plotly_express icon indicating copy to clipboard operation
plotly_express copied to clipboard

Color line_mapbox by continuous variable

Open WillKoehrsen opened this issue 5 years ago • 8 comments

Is there a way to color a line_mapbox plot by a continuous variable? It looks like when I specify the color for a line_mapbox as a continuous float column, the float is interpreted as a category:

import pandas as pd
import plotly_express as px

df = pd.read_csv('https://raw.githubusercontent.com/WillKoehrsen/Data-Analysis/master/nyc_traffic_data/nyc_traffic_speed_routes_sample.csv', 
                 parse_dates=['time'])

px.set_mapbox_access_token(secrets['mapbox'])

px.line_mapbox(df.iloc[:10000], 
                  lat='latitude', lon='longitude', 
                  animation_frame='time', line_group='id',
                  animation_group='id',
                 color='speed', title='NYC Traffic Speeds',
                 height=800, zoom=9)

line_mapbox_continuous_variable

One solution would be to create my own column mapping the speeds to colors and then use that to color the lines, but I would still run into the issue of showing a legend. Thanks for the great library!

WillKoehrsen avatar Apr 09 '19 02:04 WillKoehrsen

This will be a bit tricky... still thinking about the best way forward :)

nicolaskruchten avatar May 10 '19 16:05 nicolaskruchten

Did this ever get resolved? I am interested in doing something similar.

sjordan29 avatar Aug 30 '21 18:08 sjordan29

Hi! I am also interested in doing the same. Have you solved it? Very nice library!

josbenco avatar Apr 20 '22 15:04 josbenco

There is no support for this yet, no.

nicolaskruchten avatar Apr 20 '22 17:04 nicolaskruchten

Thanks for the reply @nicolaskruchten !

josbenco avatar Apr 21 '22 15:04 josbenco

would be a cool feature

kmhaeren avatar Sep 26 '23 22:09 kmhaeren

any update on this? Did anyone find a workaround? I'm currently working on a project doing a similar thing...

jamesrwrc avatar Feb 20 '24 12:02 jamesrwrc

@jamesrwrc, I ended up using geoviews' Path in place of this -- so, not a solution but an alternative package/tool:

import geoviews as gv

line_map = gv.Path(gdf, vdims=[var]).opts(
	height = kwargs['height'],
	width = kwargs['width'],
	color = var, 
	colorbar = kwargs['colorbar'],
	cnorm = kwargs['cnorm'], 
	cmap = kwargs['cmap'], 
	clim = (kwargs['vmin'],
        kwargs['vmax']),
	title = var, 
)											)
line_map * kwargs['basemap'] 

sjordan29 avatar Feb 20 '24 17:02 sjordan29