Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse the color scale in a Plotly Express map?

I am trying to draw an animated map using plotly_express. here is a sample code

import plotly.express as px
gapminder = px.data.gapminder()
fig = px.choropleth(gapminder, locations="iso_alpha", 
                    color="lifeExp", hover_name="country", 
                    animation_frame="year", 
                    range_color=[20,80],  
                    color_continuous_scale='RdYlGn')
fig.show()

This shows the scale from Red to Green. But I want to reverse it I want it to start from green to a maximum of red. This is done simply using Matplotlib by adding '_r' at the end of the color scale name, i.e., to be color_continuous_scale='RdYlGn_r', but this does not work with plotly_express. In the documentation, it is written that we can express the normal color scale in the methods form color_continuous_scale=px.colors.diverging.RdYlGn, this works too. However, when I add the .reverse method, i.e. color_continuous_scale=px.colors.diverging.RdYlGn.reverse it gives the following error:

TypeError                                 Traceback (most recent call last)
<ipython-input-63-9103eb8a4cd9> in <module>
      5                      range_color=[4, 23],
      6                      title='Fasting durations (h) for the world througout the year',
----> 7                      color_continuous_scale=px.colors.diverging.RdYlGn.reverse)#'RdYlGn')
      8 fig2.show()

~\Anaconda3\lib\site-packages\plotly\express\_chart_types.py in choropleth(data_frame, lat, lon, locations, locationmode, color, hover_name, hover_data, size, animation_frame, animation_group, category_orders, labels, color_continuous_scale, range_color, color_continuous_midpoint, size_max, projection, scope, center, title, template, width, height)
    822         args=locals(),
    823         constructor=go.Choropleth,
--> 824         trace_patch=dict(locationmode=locationmode),
    825     )
    826 

~\Anaconda3\lib\site-packages\plotly\express\_core.py in make_figure(args, constructor, trace_patch, layout_patch)
   1007         colorvar = "z" if constructor == go.Histogram2d else "color"
   1008         range_color = args["range_color"] or [None, None]
-> 1009         d = len(args["color_continuous_scale"]) - 1
   1010 
   1011         colorscale_validator = ColorscaleValidator("colorscale", "make_figure")

TypeError: object of type 'builtin_function_or_method' has no len()

What is the problem, and how can I override this error and applies the reverse colormap?

like image 429
Mohammad ElNesr Avatar asked Dec 17 '22 16:12

Mohammad ElNesr


2 Answers

The colorscales in px.colors are all simple lists, meaning you can use color_continuous_scale=px.colors.diverging.RdYlGn[::-1]

(I should add that the reason you're seeing an error like this is that since px.colors.diverging.RdYlGn is a list, px.colors.diverging.RdYlGn.reverse is defined, so you're currently trying to pass in a function to an argument that expects a string or a list)

like image 96
nicolaskruchten Avatar answered Dec 20 '22 06:12

nicolaskruchten


Adding _r to the palette name now works (notebook).

import plotly.express as px
gapminder = px.data.gapminder()
fig = px.choropleth(gapminder, locations="iso_alpha", 
                    color="lifeExp", hover_name="country", 
                    animation_frame="year", 
                    range_color=[20,80],  
                    color_continuous_scale='RdYlGn_r')
fig.show()

enter image description here

like image 44
Max Ghenis Avatar answered Dec 20 '22 05:12

Max Ghenis