Python 在plotly中增加面积打印的不透明度

Python 在plotly中增加面积打印的不透明度,python,plot,plotly,Python,Plot,Plotly,如何增加绘图“填充”区域的不透明度或α?我试过: import pandas as pd import plotly.offline as py import plotly.graph_objs as go import cufflinks as cf from plotly import tools plotly.offline.init_notebook_mode() cf.go_offline() df = pd.DataFrame(np.random.rand(10, 4), c

如何增加绘图“填充”区域的
不透明度
α
?我试过:

import pandas as pd
import plotly.offline as py
import plotly.graph_objs as go
import cufflinks as cf
from plotly import tools

plotly.offline.init_notebook_mode() 
cf.go_offline()


df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df.iplot(kind='area', fill=True, filename='cuflinks/stacked-area', opacity=.1)

但它似乎不起作用。

似乎没有一种简单的内置方式来实现这一点。但是,解决方法是首先获取绘图的figure对象,修改它以更改不透明度,然后进行绘图

您可以使用
asFigure
属性获取figure对象,如下所示:

figure = df.iplot(asFigure=True, kind='area', fill=True, filename='cuflinks/stacked-area')
本例中的地物对象如下所示:

Figure({
    'data': [{'fill': 'tonexty',
              'fillcolor': 'rgba(255, 153, 51, 0.3)',
              'line': {'color': 'rgba(255, 153, 51, 1.0)', 'dash': 'solid', 'shape': 'linear', 'width': 1.3},
              'mode': 'lines',
              'name': 'a',
              'text': '',
              'type': 'scatter',
              'uid': '4dcc1a3e-fba3-4a32-bb2a-40925b4fae5b',
              'x': array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int64),
              'y': array([0.91229144, 0.63049138, 0.22855077, 0.13470399, 0.9114691 , 0.39640368,
                          0.46534334, 0.20508211, 0.00203548, 0.41343938])},
             {'fill': 'tonexty',
              'fillcolor': 'rgba(55, 128, 191, 0.3)',
              'line': {'color': 'rgba(55, 128, 191, 1.0)', 'dash': 'solid', 'shape': 'linear', 'width': 1.3},
              'mode': 'lines',
              'name': 'b',
              'text': '',
              'type': 'scatter',
              'uid': '1015b30d-7c09-456c-875c-8a211a6ebdeb',
              'x': array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int64),
              'y': array([1.81115175, 1.57534372, 0.41288126, 0.38068805, 1.72268856, 0.87778503,
                          1.32714727, 0.848242  , 0.51605283, 0.58190402])},
             {'fill': 'tonexty',
              'fillcolor': 'rgba(50, 171, 96, 0.3)',
              'line': {'color': 'rgba(50, 171, 96, 1.0)', 'dash': 'solid', 'shape': 'linear', 'width': 1.3},
              'mode': 'lines',
              'name': 'c',
              'text': '',
              'type': 'scatter',
              'uid': '7d1852ac-b8e7-44e6-ae69-54229d7e2c83',
              'x': array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int64),
              'y': array([2.79222081, 1.58812634, 1.1439478 , 1.30453731, 2.50881795, 1.67681961,
                          1.85609861, 1.36657712, 0.89024486, 0.82749039])},
             {'fill': 'tonexty',
              'fillcolor': 'rgba(128, 0, 128, 0.3)',
              'line': {'color': 'rgba(128, 0, 128, 1.0)', 'dash': 'solid', 'shape': 'linear', 'width': 1.3},
              'mode': 'lines',
              'name': 'd',
              'text': '',
              'type': 'scatter',
              'uid': '89b85012-fc95-487c-b7ba-9cb6c249b768',
              'x': array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int64),
              'y': array([3.54740551, 1.79856232, 2.1326556 , 2.10560567, 2.64867039, 2.55519564,
                          2.73888819, 2.23274393, 1.16987343, 1.42794202])}],
    'layout': {'legend': {'bgcolor': '#F5F6F9', 'font': {'color': '#4D5663'}},
               'paper_bgcolor': '#F5F6F9',
               'plot_bgcolor': '#F5F6F9',
               'title': {'font': {'color': '#4D5663'}},
               'xaxis': {'gridcolor': '#E1E5ED',
                         'showgrid': True,
                         'tickfont': {'color': '#4D5663'},
                         'title': {'font': {'color': '#4D5663'}, 'text': ''},
                         'zerolinecolor': '#E1E5ED'},
               'yaxis': {'gridcolor': '#E1E5ED',
                         'showgrid': True,
                         'tickfont': {'color': '#4D5663'},
                         'title': {'font': {'color': '#4D5663'}, 'text': ''},
                         'zerolinecolor': '#E1E5ED'}}
})
您会注意到数据中的每个跟踪都有一个
fillcolor
属性:
'fillcolor':'rgba(255、153、51、0.3)
。最后一个数字是要修改的alpha值。我制作了一个小函数来更新figure对象中所有轨迹的
fillcolor
属性:

def update_opacity(figure,opacity):
    for trace in range(len(figure['data'])):
        # print(figure['data'][trace]['fillcolor'],'-> ',end='')
        rgba_split = figure['data'][trace]['fillcolor'].split(',')
        figure['data'][trace]['fillcolor'] = ','.join(rgba_split[:-1] + [' {})'.format(opacity)])
        # print(figure['data'][trace]['fillcolor'])
    return figure
对于完全不透明度,可以执行以下操作:

figure = update_opacity(figure,1)
然后,简单地用

py.iplot(figure)
输出: