尝试为plotly(python)中的每个bar子图制作统一的色标

时间:2019-12-09 15:44:26

标签: python plotly plotly-python

我创建了一个图形,其中有8个子图,分别对应于一个农场中每个风力涡轮机的能源产量。每个子图对应于不同的运行年份。我设法对每个子图应用了一个不错的色标,但是每个色标都有不同的范围(基于每个子图中的数据)。

我想制作一个“全局”色标,并且每个图中的值都对应于固定色。感谢您的建议。

def aep_turbine_subplot_fig(years, AEP):

    fig = make_subplots(rows = 4, cols = 2, subplot_titles = years)


    fig.add_trace(go.Bar(x = get_turbine_names(),
                    y = AEP.iloc[0,:],
                    name = '2012',
                    marker = {'color': AEP.iloc[0,:],
                              'colorscale': 'RdBu'}),
                    row = 1, col = 1)

    fig.add_trace(go.Bar(x = get_turbine_names(),
                    y = AEP.iloc[1,:],
                    name = '2013',
                    marker = {'color': AEP.iloc[1,:],
                              'colorscale': 'RdBu'}),
                    row = 1, col = 2)

    fig.add_trace(go.Bar(x = get_turbine_names(),
                    y = AEP.iloc[2,:],
                    name = '2014',
                    marker = {'color': AEP.iloc[2,:],
                              'colorscale': 'RdBu'}),
                    row = 2, col = 1)


    fig.add_trace(go.Bar(x = get_turbine_names(),
                    y = AEP.iloc[3,:],
                    name = '2015',
                    marker = {'color': AEP.iloc[3,:],
                              'colorscale': 'RdBu'}),
                    row = 2, col = 2)


    fig.add_trace(go.Bar(x = get_turbine_names(),
                    y = AEP.iloc[4,:],
                    name = '2016',
                    marker = {'color': AEP.iloc[4,:],
                              'colorscale': 'RdBu'}),
                    row = 3, col = 1)


    fig.add_trace(go.Bar(x = get_turbine_names(),
                    y = AEP.iloc[5,:],
                    name = '2017',
                    marker = {'color': AEP.iloc[5,:],
                              'colorscale': 'RdBu'}),
                    row = 3, col = 2)

    fig.add_trace(go.Bar(x = get_turbine_names(),
                    y = AEP.iloc[6,:],
                    name = '2018',
                    marker = {'color': AEP.iloc[6,:],
                              'colorscale': 'RdBu'}),
                    row = 4, col = 1)

    fig.add_trace(go.Bar(x = get_turbine_names(),
                    y = AEP.iloc[7,:],
                    name = '2019 (Jan to Jun)',
                    marker = {'color': AEP.iloc[7,:],
                              'colorscale': 'RdBu'}),
                    row = 4, col = 2)



    # editing the yaxes in each subplot
    fig.update_yaxes(title_text='AEP [GWh] in 2012', title_font = dict(size = 14), row=1, col=1, range = [0,8.2])
    fig.update_yaxes(title_text='AEP [GWh] in 2013', title_font = dict(size = 14), row=1, col=2, range = [0,8.2])
    fig.update_yaxes(title_text='AEP [GWh] in 2014', title_font = dict(size = 14), row=2, col=1, range = [0,8.2])
    fig.update_yaxes(title_text='AEP [GWh] in 2015', title_font = dict(size = 14), row=2, col=2, range = [0,8.2])
    fig.update_yaxes(title_text='AEP [GWh] in 2016', title_font = dict(size = 14), row=3, col=1, range = [0,8.2])
    fig.update_yaxes(title_text='AEP [GWh] in 2017', title_font = dict(size = 14), row=3, col=2, range = [0,8.2])
    fig.update_yaxes(title_text='AEP [GWh] in 2018', title_font = dict(size = 14), row=4, col=1, range = [0,8.2])
    fig.update_yaxes(title_text='AEP [GWh] in 2019', title_font = dict(size = 14), row=4, col=2, range = [0,8.2])

    # LAYOUT
    fig.update_layout(
            title = 'AEP per turbine',
            xaxis_tickfont_size = 14,
            barmode='group',
            bargap=0.15, # gap between bars of adjacent location coordinates.
            bargroupgap=0.1, # gap between bars of the same location coordinate.
            showlegend = False,
            plot_bgcolor ='rgb(160,160,160)',

        )
    fig.write_image(get_fig_dir() + 'AEP_perTurbine.png', width = 800, height = 800)
    fig.show(renderer = 'png', width = 800, height = 1000)
    return plot(fig, auto_open = True)

The graph I got with the above code. As you may see, each of the subplots has a separate colorscale.

1 个答案:

答案 0 :(得分:0)

coloraxis参数正是针对此用例:https://plot.ly/python/colorscales/#share-color-axis