Hello! I’m wondering if there is a way to make a custom legend for a graph that isn’t tied to traces. For instance I have this scattermapbox here.
And I want a legend that says that the blue dot is the starting point and the red is the ending point. Adding traces ins’t an option for me unfortunately because it would complicate other aspects of my code. Any advice on how to do this or any plans to add a feature like this in the future?
Hi @Krichardson, I don’t think you can do what you’re looking for with a legend, but I have two different suggestions:
- you have one trace with several colors, so the natural object to describe the colors and what they correspond to is a colorbar (https://plot.ly/python/colorscales/#colorscale-for-scatter-plots). You could add the labels with the
ticktext
parameter ofgo.scatter.marker.ColorBar
. This is in my opinion the “cleanest” solution because it uses how the figure is built. However, you have less flexibility for controlling the appearance than the other solution below. - you can also use a more ad hoc solution with shapes and annotations to draw filled circles and display text (see https://plot.ly/python/text-and-annotations/ and https://plot.ly/python/shapes/).
1 Like
Thank you Emmanuelle! I’ll look into these solutions!
Actually I still have one question. How do add an annotation to a Scattermapbox? When creating an annotation, the x and y parameters don’t seem to correspond to the latitude and longitude of the mapbox. I’m attempting to create the annotation within the layout like this:
annotations=
[
go.layout.Annotation(
x=longitudes[0],
y=latitudes[0],
xref="x",
yref="y",
text='Start',
showarrow=True,
)
],