Catarina Nogueira
Catarina Nogueira

Reputation: 1138

Plotly python: Fix one of the bars on the plot as reference for the others

I have a bar plot whose baselines I got from an article:

from plotly import graph_objects as go

data = {
    "original":[15, 23, 32, 10, 23],
    "model_1": [4,   8, 18,  6,  0],
    "model_2": [11, 18, 18,  0,  20],
    "labels": [
        "feature",
        "question",
        "bug",
        "documentation",
        "maintenance"
    ]
}

fig = go.Figure(
    data=[
        go.Bar(
            name="Original",
            x=data["labels"],
            y=data["original"],
            offsetgroup=0,
        ),
        go.Bar(
            name="Model 1",
            x=data["labels"],
            y=data["model_1"],
            offsetgroup=1,
        ),
        go.Bar(
            name="Model 2",
            x=data["labels"],
            y=data["model_2"],
            offsetgroup=2
        )
    ],
    layout=go.Layout(
        title="Issue Types - Original and Models",
        yaxis_title="Number of Issues"
    )
)

fig.show()

enter image description here

And what I want is to also show in all different labels (Question, Bug, Documentation, Maintenance) a bar with the 'Original' (blue bar) from the Feature label. So every label will be with the two bars it already have + the bar of the 'Original' from Feature label.

The problem is that I really could not find on how to do this on documentation or anywhere.

Upvotes: 2

Views: 243

Answers (1)

Derek O
Derek O

Reputation: 19545

If I understand you correctly, what you can do is hard code the values of the 'Original' bar from the Feature label, and add them to the other bars. To avoid adding the 'Original' bar from the Feature label back to itself, set the value to 0. To keep the color scheme the same, you can manually set the colors of the bars.

from plotly import graph_objects as go


## add new_bars with the values set to 15 except the first
data = {
    "new_bars":[0, 15, 15, 15, 15],
    "original":[15, 23, 32, 10, 23],
    "model_1": [4,   8, 18,  6,  0],
    "model_2": [11, 18, 18,  0,  20],
    "labels": [
        "feature",
        "question",
        "bug",
        "documentation",
        "maintenance"
    ]
}

#modify the first bar
fig = go.Figure(
    data=[
        go.Bar(
            name="Original (feature)",
            x=data["labels"],
            y=data["new_bars"],
            marker_color="LightBlue",
            offsetgroup=0,
        ),
        go.Bar(
            name="Original",
            x=data["labels"],
            y=data["original"],
            marker_color="CornflowerBlue",
            offsetgroup=1,
        ),
        go.Bar(
            name="Model 1",
            x=data["labels"],
            y=data["model_1"],
            marker_color="Tomato",
            offsetgroup=2,
        ),
        go.Bar(
            name="Model 2",
            x=data["labels"],
            y=data["model_2"],
            offsetgroup=2,
            marker_color="rgb(0,212,149)",
            base=data["model_1"],
        ),
    ],
    layout=go.Layout(
        title="Issue Types - Original and Models",
        yaxis_title="Number of Issues"
    )
)

fig.show()

enter image description here

Upvotes: 1

Related Questions