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:
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 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.