不断增加区域图中的不透明度

时间:2019-05-01 10:54:33

标签: python plot plotly

如何增加剧情“已填充”区域的opacityalpha?我尝试过:

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)

但它似乎不起作用。

1 个答案:

答案 0 :(得分:1)

似乎没有一种简单的内置方法可以实现这一目标。但是,一种解决方法是先获取图的图形对象,对其进行修改以更改不透明度,然后再对其进行绘制。

您可以使用asFigure属性来获得图形对象,如下所示:

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值。我做了一个骇人听闻的小函数来更新图形对象中所有迹线的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)

输出:

plotly output with full opacity