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.
Step 1: Import the necessary libraries to plot Stacked Bar Chart from DataFrame.
Python3
# Importing library's from bokeh.plotting import figure, show, output_notebook import pandas as pd |
Step 2: Creating DataFrame to be used for visualization purposes.
Python3
# Create Students,Subjects list and Colours. Students = [ 'Ankur' , 'Yash' , 'Aditya' , 'Harshit' ] Subjects = [ 'Operating System' , 'Data Structure' ,\ 'Java Programming' ] cols = [ '#00ff00' , '#009900' , '#00cc99' ] |
Step 3: Create and initialise data to the dictionary.
Python3
# Initialize data to lists. data = { 'Students' : Students, 'Operating System' : [ 17 , 20 , 19 , 18 ], 'Data Structure' : [ 19 , 18 , 20 , 17 ], 'Java Programming' : [ 20 , 19 , 20 , 18 ]} |
Step 4: Create dataFrame and verify data.
Python3
# Creates DataFrame. df = pd.DataFrame(data) df.head() |
Output:
Step 5: Plot data In a vertically stacked manner.
Python3
# plot data in Stack manner of bar type fig = figure(x_range = df.Students, height = 500 , title = "Marks counts of \ every students according to subjects") fig.vbar_stack(Subjects, x = 'Students' , source = df, color = cols, width = 0.5 ) |
Step 6: Display Stack Graph.
Python3
# Display Stack Graph show(fig) |
Output:
Adding Labels to Bokeh Stacked Bar Chart
We can use the attribute legend_label to show legends on our graph.
Python3
# plot data in Stack manner of bar type fig = figure(x_range = df.Students, height = 500 , title = "Marks counts of \ every students according to subjects") fig.vbar_stack(Subjects, x = 'Students' , source = df, color = cols, width = 0.5 , legend_label = Subjects) # Display Stack Graph 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.