如何在plotly-dash中向树状图添​​加自定义刻度标签?

时间:2019-11-07 01:12:45

标签: plotly-dash

我有这个回调函数,可以生成热图和可选的树状图。但是,在创建树状图时,该图缺少yticklabels。我希望标签显示在树状图的左侧或热图的右侧。

@app.callback(
    Output('peakAreas', 'figure'),
    [Input('b_peakAreas', 'n_clicks'),
     Input('table', 'derived_virtual_indices'),
     Input('checklist', 'value')]) def plot_0(n_clicks, ndxs, options):
    title = 'Heatmap'
    if n_clicks is None:
        return {}
    df = mint.crosstab.T.iloc[ndxs]
    df.index = [basename(i).replace('.mzXML', '') for i in df.index]

    if 'normed' in options:
        df = df / df.max()
        title = f'Normalized {title}'



    if 'clustered' in options:
        D = squareform(pdist(df, metric='euclidean'))
        Y = linkage(D, method='complete')
        Z = dendrogram(Y, orientation='left', no_plot=True)['leaves']
        df = df.iloc[Z,:]
        dendro_side = ff.create_dendrogram(df, orientation='right')

    heatmap = go.Heatmap(z=df.values,
                         x=df.columns,
                         y=df.index,
                         colorscale = 'Blues')

    if not 'dendrogram' in options:
        fig = go.Figure(heatmap)
        fig.update_layout(
            title={'text': title,  },
            yaxis={'title': '', 
                'tickmode': 'array', 
                'automargin': True}) 
        return fig

    fig = go.Figure()

    for i in range(len(dendro_side['data'])):
            dendro_side['data'][i]['xaxis'] = 'x2'

    for data in dendro_side['data']:
        fig.add_trace(data)

    y_labels = heatmap['y']


    heatmap['y'] = dendro_side['layout']['yaxis']['tickvals']
    dendro_side['layout']['yaxis']['ticktext'] = y_labels

    fig.add_trace(heatmap)     

    fig.update_layout({'showlegend':False, 'hovermode': 'closest'})
    fig.update_layout(
            title={'text': title,  },
            yaxis={'title': '', 
                'tickmode': 'array', 
                'automargin': True}) 


    fig.update_layout(xaxis2={'domain': [0, .1],
                              'mirror': False,
                              'showgrid': True,
                              'showline': False,
                              'zeroline': False,
                              'showticklabels': False,
                              'ticks':""})        

    fig.update_layout(xaxis={'domain': [.1, 1],
                             'mirror': False,
                             'showgrid': False,
                             'showline': False,
                             'zeroline': False,
                             'showticklabels': True,
                             'ticks':""})
    # Edit yaxis
    fig.update_layout(yaxis={'domain': [0, 1],
                             'mirror': False,
                             'showgrid': False,
                             'showline': False,
                             'zeroline': False,
                             'showticklabels': True,
                            })

    fig.update_layout(yaxis_ticktext = y_labels)

    print(dendro_side.layout['yaxis'])
    return fig

当我绘制y轴布局时,我得到以下信息:

layout.YAxis({
    'mirror': 'allticks',
    'rangemode': 'tozero',
    'showgrid': False,
    'showline': True,
    'showticklabels': True,
    'tickmode': 'array',
    'ticks': 'outside',
    'ticktext': array(['SA1', 'SA3', 'SA2', 'SA4', 'CA1', 'CA2', 'CA3', 'CA4', 'EC2', 'EC4',
                       'EC1', 'EC3'], dtype=object),
    'tickvals': [5.0, 15.0, 25.0, 35.0, 45.0, 55.0, 65.0, 75.0, 85.0, 95.0, 105.0,
                 115.0],
    'type': 'linear',
    'zeroline': False
})

因此,对我来说,由于正确的数据存储在ticktext下,因此图形应该具有正确的标签。但是它没有出现。

enter image description here

0 个答案:

没有答案