2

How can I plot a colorbar when plotting data from in datashader. I'm using vehicle data with position and heading (0-360*)

Minimal Datashader code:

import seaborn as sns, datashader as ds
from matplotlib.cm import ListedColormap

cmap = ListedColormap(sns.color_palette("hls", 8).as_hex())
agg = canvas.points(df, "longitude", "latitude", ds.mean("heading"))
img = tf.shade(agg, cmap=cmap, how="eq_hist")
img

This produces a colorful image with clear roads and directions, except it's impossible to tell without a legend which color maps to which direction.

At the moment I'm using Plotly to make the plot interactive as for some reason the holoviews/bokeh method is quite broken. This is the tutorial I followed to combine them: https://plot.ly/python/v3/change-callbacks-datashader/.

Eden Trainor
  • 571
  • 5
  • 17

2 Answers2

1

Datashader isn't made to render things like axes and colorbars, relying on embedding into an external library for such things. How you do it depends on the external library. For the plotly example you linked, it would be most direct to change the gen_ds_image function to return agg_scatter instead of img, and then use Plotly's array plotting functionality to do the colormapping and create a colorbar that would then automatically match. I don't use Plotly enough to give you any guidance on that, but it should be straightforward for a Plotly user, as the result of gen_ds_image will then just be a 2D array of directions per pixels, which presumably can easily be plotted as colors.

I'm not sure what problems you had with the Bokeh+HoloViews approach, but it should take much less code than that Plotly example; you'd just need to be sure to use rasterize() and not datashade() (to let Bokeh do the colormapping and matching colorbar). See https://anaconda.org/jbednar/datashade_vs_rasterize/notebook for background info.

James A. Bednar
  • 3,195
  • 1
  • 9
  • 13
  • 1
    I have been looking for the information in this notebook for hours! Finally understanding why I can't do something that seems easy like make a colorbar for my map. Please share this notebook widely. It didn't come up in any of my (many many) Google searches. – mkosmala Mar 26 '20 at 17:08
  • @mkosmala Did you find a solution? I look at the notebook and I cannot see any information you can transpose to plotly. Array plotting in plotly is not something you can use with Scatternapbox. I don't get why this has been upvoted, there isn't any answer just discussion about other packages. – Boorhin Jan 11 '22 at 18:39
0

I used the datashader in the original post as well as added a colorbar_trace as done here : a link And it worked.

The only change I made was where it says colorscale=red_blue, I updated to fire and I turned the backgroundcolor of the scatterplot to "black" to match the map.

so in update_layout section of datashader link add the following:

plot_bgcolor="black",
paper_bgcolor="black"

And in the marker section of stackoverflow link add the following:
colorscale=fire

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 29 '22 at 07:20