Horizontal Stacked Bar Chart using Bokeh

Now let’s create the exact visualization but this time the bars will be stacked horizontally and the function that will be used will be hbar_stack(). Also, there are options to, align the legends at different positions in the plot as you can see in the last lines of the below code block.

Python3




from bokeh.models import ColumnDataSource
# plot data in Stack manner of bar type
fig = figure(y_range=df.Students,
             height=500,
             title="Marks counts of \
             every students according to subjects")
fig.hbar_stack(Subjects,
               y='Students',
               source=ColumnDataSource(df),
               color=cols,
               height=0.5,
               legend_label=Subjects)
  
# Display Stack Graph
fig.legend.orientation = "horizontal"
fig.legend.location = "bottom"
show(fig)


Output:

Horizontally stacked bar chart

Adding Tooltips to Bokeh Stacked Bar Chart

Tooltips are a convenient tool and one can get the numbers for a particular bar just by hovering on it else one will have to check the axes and then conclude some numbers which can lead to errors generally. So, to resolve this issue in bokeh charts one can add tooltips here as well by adding the same in the figure() function.

Python3




from bokeh.models import ColumnDataSource
# plot data in Stack manner of bar type
fig = figure(y_range=df.Students,
             height=500,
             title="Marks counts of \
             every students according to subjects",
             tools="hover",
             tooltips="$name @Students: @$name")
  
fig.hbar_stack(Subjects,
               y='Students',
               source=ColumnDataSource(df),
               color=cols,
               height=0.5,
               legend_label=Subjects)
  
# Display Stack Graph
fig.legend.orientation = "horizontal"
fig.legend.location = "bottom"
show(fig)


Output:

Bokeh Stacked bar chart with tooltips



Bokeh – Stacked Bar Chart from DataFrame

Bokeh is a data visualization library that can be used to create beautiful graphics, from simple plots to complex dashboards with streaming datasets. Bokeh provides interactive plots in modern web browsers like Google Chrome etc, using HTML and JavaScript to present clean and highly interactive charts and plots. Bokeh allows us to create JavaScript-based visualizations without writing JavaScript code ourselves.

Similar Reads

What is a Stacked Bar Chart?

A stacked chart is a type of bar chart that helps us in comparing and plot different columns of data at the same time. In simple terms, a stacked bar chart is a bar chart in which each bar is divided into sub-bars to represent multiple columns of data simultaneously. It is also important to note that stacked bar charts are not suitable for datasets that have both positive and negative values. In this article, we will plot a Stacked Bar Chart from DataFrame....

Vertical Stacked Bar Chart using Bokeh

In the bokeh library, there are two types of function which is used to build a stacked bar chart which is vbar_stack() and hbar_stack() which is used to create vertical and horizontally stacked bar chart. In this part, we will try to create a vertically stacked bar chart using the vbar_stack() function....

Horizontal Stacked Bar Chart using Bokeh

...