python - 在 plotly 中增加区域图的不透明度

标签 python plot plotly

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

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)

但是好像不行。

最佳答案

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

您可以使用 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 值。我做了一个 hacky 小函数来更新图形对象中所有迹线的 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

关于python - 在 plotly 中增加区域图的不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55935108/

相关文章:

python - 向字典添加元素会破坏编码

python - 使用 Python (matplotlib) 的自定义标记

matlab - matlab 中是否有任何选项可以优化(行,列)以显示子图?

R geom_tile 宽度和高度在 ggplotly 中被忽略

python - Plotly python offline - 点击访问 url?

Python 反转/反转映射(但每个键有多个值)

python - 优雅地访问 networkx 中的边缘属性

python - Matplotlib/pyplot : easy way for conditional formatting of linestyle?

r - 如何在 R 中绘制数据子集与整个数据集的箱线图?

python - 如何在plotly3(不是plotly4)中并排绘制表格和散点图