如何反转 Plotly Express 地图中的色阶?

Moh*_*esr 4 python reverse express plotly colormap

我正在尝试使用plotly_express绘制动画地图。这是一个示例代码

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()
Run Code Online (Sandbox Code Playgroud)

这显示了从红色到绿色的比例。但我想反转它我希望它从绿色开始到最大的红色。这只需使用 Matplotlib 通过'_r'在色标名称的末尾添加即可完成,即 to be color_continuous_scale='RdYlGn_r',但这不适用于 plotly_express。在文档中,写到我们可以在方法形式中表达正常的色阶color_continuous_scale=px.colors.diverging.RdYlGn,这也有效。但是,当我添加 .reverse 方法时,即color_continuous_scale=px.colors.diverging.RdYlGn.reverse会出现以下错误:

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()
Run Code Online (Sandbox Code Playgroud)

有什么问题,如何覆盖此错误并应用反向颜色图?

nic*_*ten 9

中的色阶px.colors都是简单的列表,这意味着您可以使用color_continuous_scale=px.colors.diverging.RdYlGn[::-1]

(我应该补充一点,您看到这样的错误的原因是因为它px.colors.diverging.RdYlGn是一个列表,px.colors.diverging.RdYlGn.reverse已定义,因此您当前正尝试将函数传递给需要字符串或列表的参数)

  • `px.colors.diverging.RdYlGn.reverse()` 与使用 `[::-1]` 具有相同的效果吗? (2认同)
  • 不,因为 `.reverse()` 不返回任何内容,它会改变列表。如果您愿意,您可以执行“list(reversed(px.colors.diverging.RdYlGn))”,但这更冗长。`reversed()` 不返回列表,而是返回迭代器,因此您需要包装它:( (2认同)

Max*_*nis 6

现在可以添加_r到调色板名称(笔记本)。

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述