isic5
isic5

Reputation: 191

Python - Dash creating a stacked bar chart from Excel file

How can I converte a graph like this:

df1 = pd.read_excel("C:/Users/Tim/PycharmProjects/firstdash/Analysis.xlsx")

     html.Div(
                [
                    html.H3("Graph 1"),
                    dcc.Graph(
                        id="g1",
                        figure={
                            "data": [
                                go.Bar(
                                    x=df1["Marketsegment"],
                                    y=df1["test"],
                                )]})],)

sdasdasdsadasdasda

Into a stacked Bar Chart. So one Bar that includes Total Residential and Business stacked up with different colors.

Somehow I cant seem to get it to work.

All I found was this syntax, which has not worked for me:

trace1 = go.Bar(
    x=['giraffes', 'orangutans', 'monkeys'],
    y=[20, 14, 23],
    name='SF Zoo'
)
trace2 = go.Bar(
    x=['giraffes', 'orangutans', 'monkeys'],
    y=[12, 18, 29],
    name='LA Zoo'
)

data = [trace1, trace2]
layout = go.Layout(
    barmode='group'
)

Upvotes: 3

Views: 7596

Answers (1)

PieCot
PieCot

Reputation: 3639

I think the problem is how you create the figure attribute and layout of the Graph component.

Try this code, it should solve your problem.

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go

app = dash.Dash()

trace1 = go.Bar(
    x=['giraffes', 'orangutans', 'monkeys'],
    y=[20, 14, 23],
    name='SF Zoo'
)
trace2 = go.Bar(
    x=['giraffes', 'orangutans', 'monkeys'],
    y=[12, 18, 29],
    name='LA Zoo'
)

app.layout = html.Div([
    dcc.Graph(id='bar_plot',
              figure=go.Figure(data=[trace1, trace2],
                               layout=go.Layout(barmode='stack'))
              )
    ])

It produces the following result: enter image description here

Upvotes: 6

Related Questions