Rug plot from distplot does not appear in subplot

I have the following code:

import numpy as np
import plotly.graph_objs as go
import plotly.figure_factory as ff
from plotly import tools
alpha = np.random.normal(size=2000)
fig = go.FigureWidget(tools.make_subplots(rows=1, cols=2, print_grid=False))

# Traces
trace1 = go.Scatter(y=alpha, line=dict(width=0.5, color='#37AA9C'), name='alpha')
dens1 = ff.create_distplot([alpha], ['alpha'], show_rug=True, show_hist=False, colors=['#333F44'])

fig.append_trace(trace1, 1, 1)
fig.append_trace(fig1['data'][0], 1, 2)


fig.layout.update(height=400, title='Traceplot and density', showlegend=False)
fig

Notice that I set show_rug=True. However, it does not appear.

I suspect that it is there, but it does not fit in the subplot. Is that fixable?

Hi @ursus,

The issue is that the distplot and rug plot are actually separate traces, so you’ll need to add both to you new figure. Here’s an example (note that I’m imporing the v4_subplots future flag to opt in to the version of make_subplots that will be used by default in version 4, this has a variety of bug fixes that seem to be needed here).

from _plotly_future_ import v4_subplots
import numpy as np
import plotly.graph_objs as go
import plotly.figure_factory as ff
from plotly import tools

alpha = np.random.normal(size=2000)

fig = go.FigureWidget(
    tools.make_subplots(
        rows=2, cols=2, print_grid=False,
        specs=[[{'rowspan': 2}, {}],[None, {}]],
        shared_xaxes=True,
        row_heights=[1, 0.5]
    )
)

# Traces
trace1 = go.Scatter(y=alpha, line=dict(width=0.5, color='#37AA9C'), name='alpha')
dens1 = ff.create_distplot([alpha], ['alpha'], show_rug=True, show_hist=False, colors=['#333F44'])

fig.append_trace(trace1, 1, 1)
fig.append_trace(dens1['data'][0], 1, 2)
fig.append_trace(dens1['data'][1], 2, 2)


fig.layout.update(
    height=400, title='Traceplot and density', showlegend=False,
    yaxis3={'ticktext': [''], 'tickvals': [0]}
)

fig

Hope that helps,
-Jon

Thank you, @jmmease. I figured it’s better to just add the rug trace to the displot just below the zero line (now that I know how to produce them) :slight_smile:

1 Like