Skip to content

Bars in a subplot of Plotly express bar chart are thin and not centered when plotting with color #4249

@HHest

Description

@HHest

When I combine two plotly express bar charts together as subplots, and use "color" in any of the chart, then the bars of that chart isn't centered as expected; they are thinner and shifted over to one side (unless it happens to be the middle one). The more bars there are, the thinner the bars become. Thanks!

import pandas as pd
from plotly import express as px, graph_objects as go, subplots as sp


def f_add_trace(o, f, c):
    # Is this the correct way of creating a subplot from a plot express object?
    for d in f['data']:
        o.add_trace(d, row=1, col=c)


df_t = pd.DataFrame({'x': ['a', 'b', 'c', 'd'], 'y': [-10, 11, -9, -16]})
df_p = pd.DataFrame({'x': ['sss', 'ttt', 'uuu'], 'y': [-15, 10, -7]})

# Create some charts, to be combined in a single plot
f_t = px.bar(df_t, x='x', y='y')
f_p1 = px.bar(df_p, x='x', y='y', color='x')  # this code causes the problem with width and centering
f_p2 = px.bar(df_p, x='x', y='y')  # this code has no problem

# First plot, with "color" bar graph
out1 = sp.make_subplots(rows=1, cols=2, shared_yaxes=True, subplot_titles=['t', 'p'], column_widths=[4, 2])
f_add_trace(out1, f_t, 1)
f_add_trace(out1, f_p1, 2)
out1.show()  # Bad. the bars of the 2nd subplot is thin, and are shifted, except the middle one.

# Second plot, without color
out2 = sp.make_subplots(rows=1, cols=2, shared_yaxes=True, subplot_titles=['t', 'p'], column_widths=[4, 2])
f_add_trace(out2, f_t, 1)
f_add_trace(out2, f_p2, 2)
out2.show()  # normal chart

f_p1.show()  # this looks fine

Plot with problems

image

Normal looking plot when "color" is not used

image

The "bad" chart looks fine, when by itself

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions