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

2024-09-27 02:17:25 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的代码是从plotly tutorialhttps://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)

Tags: dataplotmodelineareargbplotlywidth
1条回答
网友
1楼 · 发布于 2024-09-27 02:17:25

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

相关问题 更多 >

    热门问题