Skip to content

Use vbar_stack when adding 2 vbars to same plot #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 9, 2025

Conversation

gabalafou
Copy link
Contributor

Fixes #54.

@pavithraes
Copy link
Member

pavithraes commented Jun 9, 2025

Thanks for the PR!

Since we're creating a stacked bar chart, I think it'll be best to use vbar_stack:

from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource
from bokeh.palettes import HighContrast3

# create ColumnDataSource based on DataFrame from the demo data set
source = ColumnDataSource(monthly_values_df)

# set up the figure
p = figure(
    x_range=source.data["month_name"],  # use the sequence of strings from the "month_name" column as categories
)

# plot values from these columns
columns = ["freight", "passengers", "mail"] 

# use vbar_stack that stacks multiple vbar renderers
p.vbar_stack( 
    columns,
    x="month_name",
    width=0.9,
    source=source,
    color=HighContrast3, # use the HighContrast color palettee
    legend_label=columns,
)

# move legend to left, to avoid overalap with bars
p.legend.location = "top_left"

show(p)

It additionally avoids using bokeh.transform, which is introduced in the following section (after this example).

Alternatively, we can revert this example back to using the line glyph: https://github.com/bokeh/tutorial/blob/01b37f297bd762d28ae0c035dbbe73f8c5b82f93/notebooks/06_data_sources.ipynb

I personally like the stacked bar example -- it presents more useful information, and includes a color palette & legend location change, which were introduced in the previous notebook.

@gabalafou
Copy link
Contributor Author

That makes sense to me, updated!

Copy link
Member

@pavithraes pavithraes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@pavithraes pavithraes changed the title Use stack when adding 2 vbars to same plot Use var_stack when adding 2 vbars to same plot Jun 9, 2025
@pavithraes pavithraes changed the title Use var_stack when adding 2 vbars to same plot Use vbar_stack when adding 2 vbars to same plot Jun 9, 2025
@pavithraes pavithraes merged commit a9e6ea3 into main Jun 9, 2025
@pavithraes pavithraes deleted the correct-bar-chart branch June 9, 2025 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect stacked bar example in 06 - Data Source
2 participants