Is this a bug in `px.line_mapbox`

I want to have multiple distinct lines on a map, some should have the same colour, and some should be distinct. Due to the number of lines, it isn’t practical to have them as individual traces. If we run this:

l1 = [1,2,3, None, 4,5,6]
l2 = [1,2,3, None, 4,5,6]
_hover_names = ["a","b","c",None,"d","e","f"]
_colour_idx = [0,0,0,None,1,1,1]
fig = px.line_mapbox(lat=l1, lon=l2, hover_name=_hover_names, zoom=1.5, color_discrete_sequence=colours, color = _colour_idx)
fig.show()

You get two individual lines. However, if you keep all the None’s but give the second line the same colour group as the first

_colour_idx = [0,0,0,None,0,0,0]
fig = px.line_mapbox(lat=l1, lon=l2, hover_name=_hover_names, zoom=1.5, color_discrete_sequence=colours, color = _colour_idx)

The lines are now being connected. I appreciate they are the same colour group, but this would seem unusual to be a feature, especially given in the plotly docs there is an example were None is used to disconnect lines which are all of the same colour.

If this is a bug I can report it on the plotly github

I still wonder if this is a bug, but there is a workaround, for the _colour_idx don’t use None, make each line’s index len+1.

_colour_idx = [0,0,0,0,0,0,0]
fig = px.line_mapbox(lat=l1, lon=l2, hover_name=_hover_names, zoom=4, color_discrete_sequence=colours, color = _colour_idx)

Hi @this_josh !
Welcome on the forum! :tada:

Actually setting color in a px figure tells to plotly.express to split your data making groups by value you provided in the color parameter and create a trace for each group with different color for each trace.
It’s used like a mask, to filter the data.

Explanation details

If you provide:
_colour_idx = [0,0,0,None,1,1,1]
You will have 2 traces:

  • l1 = [1,2,3]
    l2 = [1,2,3]
    _hover_names = [β€œa”,β€œb”,β€œc”]
  • l1 = [4,5,6]
    l2 = [4,5,6]
    _hover_names = [β€œd”,β€œe”,β€œf”]

Actually if you remove the None from your data you will have the same result :slightly_smiling_face:


If you provide:
_colour_idx = [0,0,0,None,0,0,0]
You will have 1 trace:

  • l1 = [1,2,3,4,5,6]
    l2 = [1,2,3,4,5,6]
    _hover_names = [β€œa”,β€œb”,β€œc”,β€œd”,β€œe”,β€œf”]

Note that the None are filtered out


If you provide:
_colour_idx = [0,0,0,0,0,0,0]
You will have 1 trace:

  • l1 = [1,2,3,None,4,5,6]
    l2 = [1,2,3,None,4,5,6]
    _hover_names = [β€œa”,β€œb”,β€œc”,None,β€œd”,β€œe”,β€œf”]

The None are still here, thus your plot is split


So if you want to have lines, some with same color and some distinct color, it is a good idea to use the color parameter to split your data by color values.
But then indeed, to keep the None splitting the same color trace, you have to set the corresponding color value to the value corresponding to the trace.

For instance:
l1 = [1, 2, 3, None, 4, 5, 6, 7, 8, 9, None, 10, 11, 12]
l2 = [1, 2, 3, None, 4, 5, 6, 7, 8, 9, None, 10, 11, 12]
_hover_names = [β€œa”, β€œb”, β€œc”, None, β€œd”, β€œe”, β€œf”, β€œg”, β€œh”, β€œi”, None, β€œj”, β€œk”, β€œl”]
_colour_idx = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1]

Will give you 2 traces:

  • Corresponding to 0:
    l1 = [1, 2, 3, None, 4, 5, 6]
    l2 = [1, 2, 3, None, 4, 5, 6]
    _hover_names = [β€œa”, β€œb”, β€œc”, None, β€œd”, β€œe”, β€œf”]

  • Corresponding to 1:
    l1 = [7, 8, 9, None, 10, 11, 12]
    l2 = [7, 8, 9, None, 10, 11, 12]
    _hover_names = [β€œg”, β€œh”, β€œi”, None, β€œj”, β€œk”, β€œl”]

Then the line of each trace will be split with the None

So not a bug it’s a (expected) feature :grin:

1 Like

Hi @Skiks,

Thank you for your detailed response. Plotly Express often has the feeling of an impressive black box vs graph_objects so it’s nice to gain a better understanding of its internals.

Perhaps this is covered in a section of the docs which I haven’t read, but if not it would be good to document that the colour_idx should use the previous lines colour rather than None.