plotly / plotly.py

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
https://plotly.com/python/
MIT License
16.13k stars 2.54k forks source link

Mapbox plotting wrong colors and negative scale for array o zeros (int64) #3115

Closed leo-smi closed 3 months ago

leo-smi commented 3 years ago

Mapbox plotting wrong color scale (negative), even the data have non negative values. I'm not sure if the colorscale code is changing something. I spected to see the map all white.

plotly version: 4.14.3

The map results in: image

The data type is int64 and is a zeros column of a dataframe

The piece of code is:

import plotly.express as px
import plotly.graph_objects as go
import json
import pandas   as     pd

path = r'C:\Users\Leandro\Documents\MEGAsync\Python\Dash\GYRO\Testes dados'

df_mapa2 = pd.read_csv(path + '/dados_completos_gyro.csv')
print(df_mapa2.date.max())
print(df_mapa2.dtypes)

token = " :) "

def map2_figure(estado = 'PR', coluna = 'dailyCases'):

    # Requisitando o geojson
    url = path + f'/geojson/{estado}.json'
    with open(url) as response:
        geojson = json.load(response)

    # dados
    df = df_mapa2.query(f'state == "{estado}"') 
    df = df.query('level == "city"')
    df = df.query(f'date == "{df.date.max()}"')
    df.to_csv(path + '/map2_dailyCases.csv')
    print(df.date.max())
    print(df)

    # para centralizar os estados
    coordenadas_estados = {
        "AC":{"lat":-8.794491, "lon":-70.5860},
        "AL":{"lat":-9.6244,   "lon":-36.6614},
        "AP":{"lat":1.475,     "lon":-51.9430},
        "AM":{"lat":-4.045,    "lon":-64.1380},
        "BA":{"lat":-12.935,   "lon":-41.1440},
        "CE":{"lat":-5.203,    "lon":-39.4850},
        "DF":{"lat":-15.7932,  "lon":-47.8187},
        "ES":{"lat":-19.7163,  "lon":-40.7538},
        "GO":{"lat":-16.070,   "lon":-49.5210},
        "MA":{"lat":-5.217,    "lon":-44.7470},
        "MT":{"lat":-13.162,   "lon":-55.7450},
        "MS":{"lat":-20.569,   "lon":-54.6840},
        "MG":{"lat":-19.039,   "lon":-45.2310},
        "PA":{"lat":-5.455,    "lon":-52.4320},
        "PB":{"lat":-8.4384,   "lon":-38.1528},
        "PR":{"lat":-24.74509, "lon":-51.6652},
        "PE":{"lat":-8.4357,   "lon":-38.2709},
        "PI":{"lat":-7.327,    "lon":-42.9350},
        "RJ":{"lat":-22.159,   "lon":-42.9070},
        "RN":{"lat":-5.8824,   "lon":-36.7245},
        "RS":{"lat":-29.78970, "lon":-53.6836},
        "RO":{"lat":-10.928,   "lon":-63.1440},
        "RR":{"lat":1.870,     "lon":-61.5010},
        "SC":{"lat":-26.77080, "lon":-51.0441},
        "SP":{"lat":-22.16664, "lon":-49.0804},
        "SE":{"lat":-10.5594,  "lon":-37.3343},
        "TO":{"lat":-9.877,    "lon":-48.1420}
    }
    # gerando o mapa
    fig = px.choropleth_mapbox(
        df,
        geojson                = geojson,
        color                  = coluna,
        color_continuous_scale = "BuGn", # https://plotly.com/python/builtin-colorscales/
        locations              = "ibgeCode", # colocar hover (quando passa o mouse em cima do ponto de dado) https://plotly.com/python/text-and-annotations/
        featureidkey           = "properties.id",
        center                 = coordenadas_estados[estado],
        mapbox_style           = "outdoors",
        zoom                   = 6,
        hover_name             = 'city', # importante,
        labels                 = {
            'state'                  : 'Estado',
            'ibgeCode'               : 'Código IBGE',
            'dailyCases'             : 'Casos<br>diários</br>',
            'dailyDeaths'            : 'Óbitos<br>diários</br>',
            'cumulatedCasesPer100k'  : 'Casos<br>acumulados</br>por 100k hab.',
            'cumulatedCases'         : 'Casos<br>acumulados</br>',
            'cumulatedDeaths'        : 'Óbitos<br>acumulados</br>',
            'cumulatedDeathsPer100k' : 'Óbitos<br>acumulados</br>por 100k hab.'
        },
    )
    fig.update_layout(
        margin             = {"r":0, "t":0, "l":0, "b":0},
        mapbox_accesstoken = token,
        paper_bgcolor      ='rgba(0,0,0,0)', # https://stackoverflow.com/questions/29968152/python-setting-background-color-to-transparent-in-plotly-plots
        coloraxis = dict(#https://plotly.com/python/reference/#heatmap-colorbar-tickfont
            colorbar = dict(
                thickness = 15,
                tickfont = dict(
                    size = 10
                )
            ),
            colorscale= [
                [0,        '#ffffff'], #0
                [1./50000, '#d0e2d0'], #10
                [1./5000,  '#a2c5a0'], #100
                [1./500,   '#73a871'], #1000
                [1./50,    '#507e4e'], #10000
                [1.,       '#324f30'], #100000
            ],
        ),
    )
    return fig

map2_figure().show()
gvwilson commented 3 months ago

Hi - we are trying to tidy up the stale issues and PRs in Plotly's public repositories so that we can focus on things that are still important to our community. Since this one has been sitting for several years, I'm going to close it; if it is still a concern, please add a comment letting us know what recent version of our software you've checked it with so that I can reopen it and add it to our backlog. Thanks for your help - @gvwilson