How is a grouped bar chart with line chart using Plotly?

https://plot.ly/python/bar-charts/#bar-chart-with-line-plot

I want to create a bar chart with line plot like in the example above using plotly and iPython. On the other hand, I want the bar chart to be a horizontal glass bar chart like in the example below using plotly and iPython. How should I do it?

https://plot.ly/python/bar-charts/#colored-bar-chart

y_saving_yes = [1, 2, 4, 6, 7, 7]
y_saving_no = [10, 10, 10, 10, 10, 10]
y_net_worth = [93453, 81666, 69889, 78381, 141395, 92969]

x_saving = ['Premium', 'Spot Shadow', 'Slow Motion', 'Highlight Music','Extra Text', 'Top Play']
x_net_worth = ['Premium', 'Spot Shadow', 'Slow Motion', 'Highlight Music','Extra Text', 'Top Play']

trace1 = Bar(
    x=y_saving,
    y=x_saving,
    marker=Marker(
        color='rgba(50, 171, 96, 0.6)',
        line=Line(
            color='rgba(50, 171, 96, 1.0)',
            width=1,
        ),
    ),
    name='Highlight Properties',
    orientation='h',
)

trace2 = Bar(
    x=y_saving,
    y=x_saving,
    marker=Marker(
        color='rgba(50, 171, 96, 0.6)',
        line=Line(
            color='rgba(50, 171, 96, 1.0)',
            width=1,
        ),
    ),
    name='Highlight Properties',
    orientation='h',
)

data = Data([trace1, trace2])
layout = Layout(barmode='stack')

fig1 = Figure(data=data, layout=layout)

trace3 = Scatter(
    x=y_net_worth,
    y=x_net_worth,
    mode='lines+markers',
    line=Line(
        color='rgb(128, 0, 128)',
    ),
    name='Highlight Views',
)

fig = tools.make_subplots(rows=1, cols=2, specs=[[{}, {}]], shared_xaxes=True,
                          shared_yaxes=False, vertical_spacing=0.001)

fig.append_trace(trace1, 1, 1)
fig.append_trace(trace3, 1, 2)

fig['layout'].update(layout)
py.iplot(fig, filename='oecd-networth-saving-bar-line')

      

+3


source to share


1 answer


Andrew from here. Super close! I think you just missed fig.append_trace(trace2, 1, 1)

. Here's a simple example doing basically the same thing for your reference.



import plotly.plotly as py
from plotly import tools
from plotly.graph_objs import Bar, Data, Figure, Layout, Marker, Scatter

x_0 = [1, 2, 4, 6, 7, 7]
x_1 = [10, 10, 10, 10, 10, 10]
y_0 = [2, 3, 4, 2, 3, 3]

trace1 = Bar(
    x=x_0,
    marker=Marker(color='#001f3f'),
    orientation='h',
)

trace2 = Bar(
    x=x_1,
    marker=Marker(color='#0074D9'),
    orientation='h',
)

trace3 = Scatter(y=y_0)

fig = tools.make_subplots(1, 2)
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 1)
fig.append_trace(trace3, 1, 2)
fig['layout'].update(barmode='stack')

py.iplot(fig, filename='oecd-networth-saving-bar-line')

      

+5


source







All Articles