Slartibartfast
Slartibartfast

Reputation: 1190

Changing color depth for Plotly maps

I have the following code:

import plotly.express as px
import pandas as pd
import numpy as np
import io
df = wb.data.DataFrame('FP.CPI.TOTL.ZG')
df1 = df['YR2021']
df1 = df1.dropna()
df1 = pd.Series(df1,name="counts")
df1 = df1.reset_index()
    
fig = px.choropleth(df1, locations="economy",
                    locationmode='ISO-3',
                    color="counts", 
                    title="Global Inflation Rate 2021",
                    hover_name="economy",
                    scope ='asia',
                    color_continuous_scale=px.colors.sequential.Reds)
fig.update_layout(title_text='Global Inflation Rate 2021', title_x=0.5)
fig.show()

It produces the following plot: enter image description here

The problem I am facing is that the color are not deep enough for visual distinction since the data which I am using is global data. Is there anyway I can deepen the color contract so visually it is apparent in the Asia context or any other scope? Or could you advise how can I remove all Non-Asian country without marking each off one by one; since it wound be very time consuming especially when other continents to specify each country.

Upvotes: 0

Views: 204

Answers (1)

r-beginners
r-beginners

Reputation: 35115

The solution to this issue is to set the color range: setting range_color from 0 to a maximum value of Kyrgyzstan will paint the image as shown in the attached image. Since no data was presented, I searched for similar data from here to create the code.

import pandas as pd

df = pd.read_csv('./data/API_FP.CPI.TOTL.ZG_DS2_en_csv_v2_4488104.csv', skiprows=4)
df = df[['Country Name', 'Country Code', '2021']]
df = df.dropna()

import plotly.express as px

fig = px.choropleth(df, locations="Country Code",
                    locationmode='ISO-3',
                    color="2021", 
                    title="Global Inflation Rate 2021",
                    hover_name="Country Name",
                    scope ='asia',
                    range_color=[0,12], # Kyrgyz Republic:11.90504
                    color_continuous_scale=px.colors.sequential.Reds)
fig.update_layout(
    autosize=True,
    height=600,
    title_text='Global Inflation Rate 2021',
    title_x=0.5
)
fig.show()

enter image description here

Upvotes: 1

Related Questions