绘图区域图,如何设置填充不透明度?

时间:2019-01-17 16:32:11

标签: python 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)

1 个答案:

答案 0 :(得分:1)

您可以通过在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