Plotly面积图,如何设置填充透明度?

6
下面的代码是从plotly教程https://plot.ly/python/filled-area-plots/复制的,除了具有opacity设置的行。
然而,这并不起作用。我该如何设置填充区域的透明度?
import plotly.plotly as py
import plotly.graph_objs as go

# Add original data
x = ['Winter', 'Spring', 'Summer', 'Fall']

trace0 = dict(
    x=x,
    y=[40, 60, 40, 10],
    hoverinfo='x+y',
    mode='lines',
    ##########
    opacity = 0.5,
    ##########
    line=dict(width=0.5,
              color='rgb(131, 90, 241)'),
    stackgroup='one'
)
trace1 = dict(
    x=x,
    y=[20, 10, 10, 60],
    hoverinfo='x+y',
    mode='lines',
    ##########
    opacity = 0.5,
    ##########
    line=dict(width=0.5,
              color='rgb(111, 231, 219)'),
    stackgroup='one'
)
trace2 = dict(
    x=x,
    y=[40, 30, 50, 30],
    hoverinfo='x+y',
    mode='lines',
    ##########
    opacity = 0.5,
    ##########
    line=dict(width=0.5,
              color='rgb(184, 247, 212)'),
    stackgroup='one'
)
data = [trace0, trace1, trace2]

fig = dict(data=data)
py.iplot(fig, filename='stacked-area-plot-hover', validate=False)
2个回答

7
您可以通过在fillcolor中提供RGBA颜色来设置填充区域图的不透明度,例如:
import plotly.plotly as py

x = ['Winter', 'Spring', 'Summer', 'Fall']

y_values = [[40, 60, 40, 10],
            [20, 10, 10, 60],
            [40, 30, 50, 30]]

colors = ['rgba(131, 90, 241, 0.25)',
          'rgba(111, 231, 219, 0.5)',
          'rgba(184, 247, 212, 1)']

data = []

for i, y in enumerate(y_values):
    data.append(dict(x=x,
                     y=y,
                     hoverinfo='x+y',
                     mode='lines',
                     line=dict(width=0.5,
                               color=colors[i]),
                     fillcolor=colors[i],
                     stackgroup='one'))
          


fig = dict(data=data)
py.iplot(fig, filename='stacked-area-plot-hover', validate=False)

为您提供

enter image description here


有没有一种方法可以在没有颜色列表的情况下设置不透明度? - Soren
你想使用默认颜色并仅修改不透明度吗? - Maximilian Peters
2
是的,是的,是的,是的,是的。 - Soren
@Soren:你可以为线条和标记设置不透明度,但对于区域图表,我没有找到明显的方法来提供不透明度而不使用颜色。 - Maximilian Peters

1
截至今天(2023年01月20日),没有办法在不显式声明颜色的情况下更改填充颜色的不透明度。
在Plotly的GitHub存储库中有一个未解决的问题:#3344 - fillcolor opacity attribute,虽然它在过去几年中没有得到太多关注,因此我不会抱太大希望。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接