6

I need to plot two histograms together in Plotly, where each histogram has a line drawn where the mean is, with a label showing the mean value. My code currently draws both histograms, however I have no idea how to add a mean line with the label. Any idea?

    import numpy as np
    import random
    from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
    import plotly.graph_objs as go

    init_notebook_mode() # run at the start of every ipython notebook

    a = np.random.normal(1500, 100, 1000)
    b = np.random.normal(1500, 150, 1000)

    trace1 = go.Histogram(
        x=a,
        opacity=0.75,
        histnorm='probability',
        name='> 180 t/h'
    )
    trace2 = go.Histogram(
        x=b,
        opacity=0.75,
        histnorm='probability',
        name='< 160 t/h',
        yaxis='y2'
    )

    data = [trace1, trace2]

    layout = go.Layout(
        title='title',
        barmode='overlay',
        xaxis=dict(
        title=''
        ),
        yaxis=dict(
            title='Normalized Frequency < 160 t/h'
        ),
        yaxis2=dict(
            title='Normalized Frequency > 180 t/h',
            anchor='free',
            overlaying='y',
            side='right',
            position=1
        )        
    ) 

    fig = go.Figure(data=data, layout=layout)
    iplot(fig)
7
  • 1
    It'd be nice if you could provide a sample of your data to create a mcve Commented Nov 16, 2016 at 10:40
  • I've added a random normal distribution to generate some dummy data. A lot of data is required to generate a proper histogram, so it is impractical to add the original data source Commented Nov 16, 2016 at 10:53
  • Can you use cufflinks? Commented Nov 16, 2016 at 11:14
  • No idea what cufflinks are. Will read up on it Commented Nov 16, 2016 at 11:19
  • It basically binds plotly to the pandas dataframe, so that you can use df.iplot(). Do you know how to calculate the mean probability directly from a and b? Commented Nov 16, 2016 at 11:37

1 Answer 1

11

After hours of fiddling, I think I got to something that roughly works:

a = np.random.normal(1200, 100, 1000)
b = np.random.normal(1500, 150, 1000)
df = pd.DataFrame(np.transpose([a,b]), columns=['a','b'])
a = df.a
b = df.b

trace1 = go.Histogram(
    x=df.a,
    opacity=0.75,
    histnorm='probability',
    name='> 180 t/h'
)
trace2 = go.Histogram(
    x=df.b,
    opacity=0.75,
    histnorm='probability',
    name='< 160 t/h',
    yaxis='y2'
)

# Create traces


data = [trace1, trace2]

layout = go.Layout(
    title='item',
    barmode='overlay',
    xaxis=dict(
    title=''
    ),
    yaxis=dict(
        title='Normalized Frequency < 160 t/h'
    ),
    yaxis2=dict(
        title='Normalized Frequency > 180 t/h',
        anchor='free',
        overlaying='y',
        side='right',
        position=1
    ),

    # Mean lines
    shapes= [{'line': {'color': '#0099FF', 'dash': 'solid', 'width': 1},
    'type': 'line',
    'x0': df.a.mean(),
    'x1': df.a.mean(),
    'xref': 'x',
    'y0': -0.1,
    'y1': 1,
    'yref': 'paper'},
   {'line': {'color': '#FDAB5A', 'dash': 'solid', 'width': 1},
    'type': 'line',
    'x0': df.b.mean(),
    'x1': df.b.mean(),
    'xref': 'x',
    'y0': -0.1,
    'y1': 1,
    'yref': 'paper'}],

    # Annotations
    annotations=[
        dict(
            x=df.a.mean(),
            y=1,
            xref='x',
            yref='paper',
            text="Mean a = {:,.0f}".format(df.a.mean()),
            showarrow=True,
            arrowhead=7,
            ax=1,
            ay=1,
            axref='paper',
            ayref='paper'
        ),
        dict(
            x=df.b.mean(),
            y=0.95,
            xref='x',
            yref='paper',
            text="Mean b = {:,.0f}".format(df.b.mean()),
            showarrow=True,
            arrowhead=7,
            ax=1,
            ay=1,
            axref='paper',
            ayref='paper'
        )
    ]

) 
fig = go.Figure(data=data, layout=layout)
py.iplot(fig)

Result graph


Initially I tried to achieve it using cufflinks. This works fine:

import cufflinks as cf

df.iplot(kind='histogram', histnorm='probability', barmode='overlay',
     vline=[dict(x=df.a.mean(),color='#5283AD'), dict(x=df.b.mean(),color='#FDAB5A')])

enter image description here

But if you also try to add the annotations, it's going to remove the vlines.

In the end I used asFigure to return the plotdict with the vlines but no layout. I then extracted the shapes bit to create the solution above manually.

# Return a dict
plotdict = df.iplot(kind='histogram', histnorm='probability', barmode='overlay',
     vline=[dict(x=df.a.mean(),color='#5283AD'), dict(x=df.b.mean(),color='#FDAB5A')],
     asFigure=True)

https://plot.ly/~jmarrec/326/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Julien! Great help you've been

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.