2

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Catarina Nogueira
  • 1,024
  • 2
  • 12
  • 28
  • 1
    I assume you for the last go.Bar() you meant for the parameter offsetgroup=1? Otherwise you will not produce the plot shown in the article – Derek O Aug 18 '20 at 13:47
  • The `fig3` that you are referring to in your question is in its final form, so just put it between `import` and `fig3.show()` and it will appear. – r-beginners Aug 18 '20 at 13:49
  • 1
    Note that we prefer a technical style of writing here. We gently discourage greetings, hope-you-can-helps, thanks, advance thanks, notes of appreciation, regards, kind regards, signatures, please-can-you-helps, chatty material and abbreviated txtspk, pleading, how long you've been stuck, voting advice, meta commentary, etc. Just explain your problem, and show what you've tried, what you expected, and what actually happened. – halfer Aug 19 '20 at 12:14

1 Answers1

1

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

Derek O
  • 16,770
  • 4
  • 24
  • 43