gpt4 book ai didi

python - 如何反转 Plotly Express map 中的色阶?

转载 作者:太空宇宙 更新时间:2023-11-04 11:15:15 24 4
gpt4 key购买 nike

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

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()

这显示了从红色到绿色的比例。但我想扭转它我希望它从绿色开始到最大红色。这只需使用 Matplotlib 通过在色标名称末尾添加 '_r' 即可完成,即 color_continuous_scale='RdYlGn_r',但这不适用于plotly_express。在documentation ,据记载,我们可以用 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()

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

最佳答案

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

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

关于python - 如何反转 Plotly Express map 中的色阶?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57167826/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com