Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly display plotly density_mapbox?

I am starting to learn about plotly color scales. I have this code:


import plotly.express as px
import plotly.graph_objects as go 
import plotly.io as pio
pio.renderers.default = 'firefox'


fig = px.density_mapbox(df_chestie_psq_2018, lat='Lat', lon='Long', z='Psq', radius=30,
                                                        center=go.layout.mapbox.Center (
                                   lat=44.439663,
                                   lon=26.096306
                                ), zoom=10, color_continuous_scale= [
                [0.0, "green"],
                [0.5, "green"],
                [0.51111111, "yellow"],
                [0.71111111, "yellow"],
                [0.71111112, "red"],
                [1, "red"]],
                        opacity = 0.5
                        )
fig.update_layout(mapbox_style="dark", mapbox_accesstoken='some_token')
fig.update_layout(margin=dict(b=0, t=0, l=0, r=0))
fig.show()

Where df_chestie_psq_2018 is a dataframe and I want to distinguish the values in that dataframe by Psq. The proble is that it displays the following map:

enter image description here

The problem is that green should be the dominant color and as you can see, it displays the red color. Also, if I zoom in, the color disappear. I just want to see the green value between some values , yellow at other values and red at what is left. But I can manage to do it successfully.

An easy example with the same problem that is easy to reproduce:

import plotly.io as pio
import plotly.express as px
import json
import pandas as pd
import plotly.graph_objects as go 
import plotly.figure_factory as ff
import plotly.express as px


df = px.data.carshare()


fig = px.density_mapbox(df, lat='centroid_lat', lon='centroid_lon', z='car_hours', radius=30,
             color_continuous_scale= [
                [0.0, "green"],
                [0.5, "green"],
                [0.51111111, "yellow"],
                [0.71111111, "yellow"],
                [0.71111112, "red"],
                [1, "red"]],
                        opacity = 0.5
                        )


fig.update_layout(mapbox_style="carto-darkmatter")
fig.update_layout(margin=dict(b=0, t=0, l=0, r=0))

fig.show()

I just want to see green color, red color and yellow color based on car_hours value.

like image 640
TheGainadl Avatar asked Jul 05 '26 05:07

TheGainadl


1 Answers

You should use scatter_mapbox instead

import plotly.io as pio
import plotly.express as px
import json
import pandas as pd
import plotly.graph_objects as go 
import plotly.figure_factory as ff
import plotly.express as px


df = px.data.carshare()


fig = px.scatter_mapbox(df, lat='centroid_lat', lon='centroid_lon', color='car_hours',
             color_continuous_scale= [
                [0.0, "green"],
                [0.5, "green"],
                [0.51111111, "yellow"],
                [0.71111111, "yellow"],
                [0.71111112, "red"],
                [1, "red"]],
                        opacity = 0.5
                        )


fig.update_layout(mapbox_style="carto-darkmatter")
fig.update_layout(margin=dict(b=0, t=0, l=0, r=0))

fig.show()

enter image description here

like image 161
Wenhao Wu Avatar answered Jul 06 '26 18:07

Wenhao Wu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!