-1

I am trying to make a group bar graph in dash, I am plotting subject codes on the x-axis so they are not continuous numbers and I am getting empty bars for the missing subject codes so is there any way to remove these spaces or invisible bars. This is the bar graph I am getting. This is my code.

df = pd.read_csv('sampledata.csv')
a=df['SiteCode'].loc[df['SubjectStatus']=='In Progress'].value_counts()
a.index=a.index.astype(str)
b=df['SiteCode'].loc[df['SubjectStatus']=='Withdrawn'].value_counts()
b.index=b.index.astype(str)
x1=a.index
x2=b.index
trace1=go.Bar(
    x=x1,
    y=a.values,
    name='In Progress',
)
trace2=go.Bar(
    x=x2,
    y=b.values,
    name='Withdrawn',
)
app = dash.Dash()
app.layout = html.Div(
    dcc.Graph(id='graph',
        figure=go.Figure(data=[trace1,trace2],layout=go.Layout(barmode='group')))

if __name__=='__main__':
    app.run_server()

Thanks in advance

PS: I am a noob in dash and python both so go easy on me.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
lucifer
  • 7
  • 3

1 Answers1

1

You should try set barmode='stack', because barmode='group' added empty space if your one of your traces have empty values.

import dash
from dash.dependencies import Output, Input
import dash_core_components as dcc
import dash_html_components as html
import plotly
import plotly.graph_objs as go
import pandas as pd

app = dash.Dash(__name__)

df = pd.DataFrame({'x': [100, 100, 105, 110, 110, 115, 120, 125],
                   'y': [1, 2, 1, 1, 2, 2, 1, 1]})

colors = {
    'background': '#111111',
    'background2': '#FF0',
    'text': '#7FDBFF'
}
df1 = df.loc[df["y"] == 1]
df2 = df.loc[df["y"] == 2]
trace1 = go.Bar(
                x=df1["x"],
                y=df1["y"],
                name='In Progress',
)
trace2 = go.Bar(
                x=df2["x"],
                y=df2["y"],
                name='Withdrawn',
)

app.layout = html.Div(children=[
        html.Div([
            html.H5('ANNx'),
            dcc.Graph(
                id='cx1',
                figure=go.Figure(data=[trace1, trace2],
                                 layout=go.Layout(barmode='group')))],)])


if __name__ == '__main__':
    app.run_server(debug=True)

For example, in this code at value 105, 115 and 120 one trace is empty and this create space in plot: Bar group

Using another barmode solved this problem:

Bar stacked

Dmitriy Kisil
  • 2,858
  • 2
  • 16
  • 35