Black Lives Matter. Please consider donating to Black Girls Code today.

Show vertical lines in 2d density histogram chart

I like to show mean lines on the 2d density histogram chart using Plotly.js. I saw that ggplot2 has the options to draw geom_vlines. For simple histograms, there are examples using layout, shapes. The shapes option seems to be a better choice. But I’m not clear on how to define the x0, y0, x1, y1 on the histogram subplots, xaxis2 and yaxis2.

Any suggestions? Thanks.

I think I got it figured out. It’s doable using shapes with xref, yref to set which coordinate axis to use. See shapes API

1 Like

Here is an example:

def p_values_density(p_values):
    critical_level = 0.05
    fig = ff.create_distplot([p_values], ['p-values'], show_hist=False)
    fig['layout'].update(
        showlegend=False,
        xaxis=dict(title='p-value'),
        yaxis=dict(title='Number of runs'),
    )
    fig['layout']['shapes'] = [*fig['layout']['shapes'], {
        'type': 'line',
        'xref': 'x1',
        'yref': 'paper',
        'x0': critical_level,
        'y0': 0,
        'x1': critical_level,
        'y1': 1,
        'line': {
            'color': 'black',
            'width': 1,
            'dash': 'dashdot',
        },
    }]
    return fig

Defining the shapes in fig['layout'].update() doesn’t work. We need to set fig['layout']['shapes'] explicitly.