【问题标题】:plotly area chart, how can I set fill opacity?plotly 面积图,如何设置填充不透明度?
【发布时间】:2019-01-17 16:32:10
【问题描述】:

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

【问题讨论】:

    标签: python plotly


    【解决方案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)
    

    给你

    【讨论】:

    • 有没有办法在没有颜色列表的情况下设置不透明度?
    • 你想使用默认颜色,只修改不透明度吗?
    • 是的,是的,是的,是的,是的
    • @Soren:您可以为线条和标记设置不透明度,但对于面积图,我没有找到一种明显的方法来提供不带颜色的不透明度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    相关资源
    最近更新 更多