简体   繁体   English

试图为 plotly (python) 中的每个条形子图制作统一的色阶

[英]Trying to make a uniform colorscale for each of the bar subplots in plotly (python)

I have created a graph with 8 subplots corresponding to the energy production of each wind turbine in a farm per year.我创建了一个图表,其中包含 8 个子图,对应于一个农场中每个风力涡轮机每年的能源生产。 Each subplot corresponds to a different year of operation.每个子图对应不同的运营年份。 I managed to get a nice colorscale applied to each of the subplots but each of the colorscales has a different range (based on the data in each of the subplots).我设法将一个很好的色阶应用于每个子图,但每个色阶都有不同的范围(基于每个子图中的数据)。

I would like to make one where there is a “global” colorscale and the values in each plot correspond to the fixed colours.我想制作一个“全局”色标,并且每个图中的值对应于固定颜色。 I would be grateful for your suggestions.我将不胜感激您的建议。

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)

我用上面的代码得到的图表。如您所见,每个子图都有一个单独的色标。

coloraxis参数正是针对这个用例: https : coloraxis

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM