[英]Plotly: Plotly express area chart with a single color for fill and line
Hi i try to figure out how to use just one color per country - filled color.嗨,我试图弄清楚如何在每个国家/地区仅使用一种颜色 - 填充颜色。 The following example gives a mix of transparent and a darker color per country.以下示例给出了每个国家/地区的透明和较深颜色的混合。 i expect just one color.我只希望一种颜色。 Thanks for help感谢帮助
import plotly
#import plotly.graph_objects as go
import plotly.express as px
df = px.data.gapminder()
fig = px.area(df, x="year", y="pop", color="country",
groupnorm='fraction')
plotly.io.write_image(fig, file='areatest.png', format='png')
There's no argument for px.area
that will give you a single color directly. px.area
没有任何参数可以直接为您提供单一颜色。 But that should not stop you.但这不应该阻止你。 Just use:只需使用:
fig.for_each_trace(lambda trace: trace.update(fillcolor = trace.line.color))
... to get: ... 要得到:
... instead of the default result which is: ...而不是默认结果:
import plotly
import plotly.express as px
df = px.data.gapminder()
df = df[df['continent']=='Americas']
fig = px.area(df, x="year", y="pop", color="country",
groupnorm='fraction')
fig.for_each_trace(lambda trace: trace.update(fillcolor = trace.line.color))
fig.show()
px.area
will assign a color to each line associated with the color
argument, and then apply a partially transparent version of that color to the corresponding area / fill. px.area
将为与color
参数关联的每一行分配一种颜色,然后将该颜色的部分透明版本应用于相应的区域/填充。 If you run fig.show
you can check the color for the first category:如果你运行fig.show
你可以检查第一个类别的颜色:
Scatter({
'fillcolor': '#636efa',
'groupnorm': 'fraction',
'hovertemplate': 'country=Argentina<br>year=%{x}<br>pop=%{y}<extra></extra>',
'legendgroup': 'Argentina',
'line': {'color': '#636efa'},
Here you can only see that 'line': {'color': '#636efa'}
and it can undoubtedly be a bit confusing that fillcolor is nowhere to be found.在这里你只能看到'line': {'color': '#636efa'}
毫无疑问,fillcolor 无处可寻。 But if you use fig.full_figure_for_development
, you can see that the fillcolor that is in fact passed to javascript is 'rgba(99, 110, 250, 0.5)'
:但是如果你使用fig.full_figure_for_development
,你可以看到实际上传递给 javascript 的填充颜色是'rgba(99, 110, 250, 0.5)'
:
Scatter({
'connectgaps': False,
'error_x': {'visible': False},
'error_y': {'visible': False},
'fill': 'tonexty',
'fillcolor': 'rgba(99, 110, 250, 0.5)',
And that's exactly what you're overriding when you run:这正是您在运行时要覆盖的内容:
fig.for_each_trace(lambda trace: trace.update(fillcolor = trace.line.color))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.