如何反转 Plotly Express 地图中的色阶?
How to reverse the color scale in a Plotly Express map?
我正在尝试使用 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()
这显示了从红色到绿色的比例。但我想扭转它
我希望它从绿色开始到最大红色。这只需使用 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
被定义了,所以你目前正在尝试传递一个对需要字符串或列表的参数的函数)
现在可以将 _r
添加到调色板名称 (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()
我正在尝试使用 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()
这显示了从红色到绿色的比例。但我想扭转它
我希望它从绿色开始到最大红色。这只需使用 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
被定义了,所以你目前正在尝试传递一个对需要字符串或列表的参数的函数)
现在可以将 _r
添加到调色板名称 (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()