I am trying to plot a world map with all the countries having different risk levels (low, moderate and high). I would like to make each risk level a different color but am not sure how to change the color scheme so that each risk category has a color of my choice.
The df.risk variable currently has low as 1, moderate as 2 and high as 3 so that it is a continuous variable, however I would like to use discrete,
fig = go.Figure(data=go.Choropleth(
locations = df['code'],
z = df['risk'],
text = df['COUNTRY'],
colorscale = 'Rainbow',
autocolorscale=False,
reversescale=True,
marker_line_color='darkgray',
marker_line_width=0.5,
colorbar_tickprefix = '',
colorbar_title = 'Risk level',
))
fig.update_layout(
title_text='Risk map',
geo=dict(
showframe=False,
showcoastlines=False,
projection_type='equirectangular'
),
annotations = [dict(
x=0.55,
y=0.15,
xref='paper',
yref='paper',
text='Source: <a href="www.google.com">\
Google</a>',
showarrow = False
)]
)
fig.show()
My sample df is:
{'Country': {0: 'Afghanistan',
1: 'Albania',
2: 'Algeria',
3: 'American Samoa',
4: 'Andorra'},
'code': {0: 'AFG', 1: 'ALB', 2: 'DZA', 3: 'ASM', 4: 'AND'},
'risk': {0: 'High', 1: 'Moderate', 2: 'High', 3: 'Low', 4: 'High'}}
In this case I would rather use plotly.express
with color=df['risk']
and then set color_discrete_map={'High':'red', 'Moderate':'Yellow','Low':'Green'}
:
import plotly.express as px
import pandas as pd
fig = px.choropleth(locations=df['Country'],
locationmode="country names",
color=df['risk'],
color_discrete_map={'High':'red',
'Moderate':'Yellow',
'Low':'Green'}
#scope="usa"
)
fig.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With