Adding color to a line_mapbox fundamentally changes how line segments are plotted

I have some lat lon data where I have inserted NaN in, in order to break up the line segments into different pieces. This works well and can quickly plot even large amounts of data.

However, if I add colors to these line segments the lines are suddenly no longer broken into pieces by the NaN values and instead I end up with something like this:

The code I run looks like this:


    fig = px.line_mapbox(lat=lat_ext[:count_ext],
                            lon=lon_ext[:count_ext],
                         # color=pipe_type_ext[:count_ext],
                            zoom=12,
                            height=1600,
                            width=1600)
    fig.update_layout(mapbox_style="open-street-map")
    fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
    fig.show()

When you add colors on a line mapbox, it first groups your data by categorical color this what youโ€™re seeing here. I believe thereโ€™s no way to do continuous colors on lines, only on markers

So I have to essentially split up my plotting into each individual color and then use fig.add_traces to join all of them together into a single map? Or will that also not work?

Iโ€™m not sure what your data looks like but if itโ€™s something where each line is a different colour then yes that works. I would say try making the data ordered so that all of 1 color comes together in the right order.

I just reread what you originally wrote and I think that there is some misconception and that I wasnโ€™t actually quite clear.

I actually wanted my data grouped by categorical colors and not continuous colors.
The main problem I was having was that whenever I specified colors it suddenly stopped breaking my long line segment into smaller pieces by the use of the NaN values..

So for instance my lat data might look like this:

56.1
56.2
56.3
NaN
54.1
54.3
NaN
55.4
55.2
NaN
...

Now when I plot this with similar longitudes I expect to get a linesegment of length 3, and 2 length 2 line segments. (which is exactly what I get whenever color is not specified. However when I specify color, then suddenly the whole sequence of lattitudes is connected as one long line segment even though there are NaN values in it.