# Here is a list of categorical values (or factors)
names = ['Jane', 'David', 'Julian', 'Jonas', 'Macro', 'Barbara']
scores = [8, 9, 8, 5, 7, 6]
# sorting the bars means sorting the range factors
sorted_names = sorted(names, key=lambda x: scores[names.index(x)])
p = figure(x_range=sorted_names, plot_height=300, title="Sorted Student Scores")
# Categorical values can also be used as coordinates
p.vbar(x=names, top=scores, width=0.75)
# Set some properties to make the plot look better
p.xgrid.grid_line_color = None
p.y_range.start = 0
show(p)
2. Filled Bar
Often times we may want to have bars that are shaded some color. This can be accomplished in different ways. One way is to supply all the colors upfront. This can be done by putting all the data, including the colors for each bar, in a ColumnDataSource.
Another common operation or bar chart is to stack bars on top of one another. Bokeh makes this easy to do with the specialized hbar_stack() and vbar_stack() .
When creating bar charts, it is often desirable to visually display the data according to sub-groups.
from bokeh.models import ColumnDataSource, FactorRange
# this creates [ ("Apples", "2015"), ("Apples", "2016"), ("Apples", "2017"), ("Pears", "2015), ... ]
x = [ (name, year) for name in names for year in years ]
counts = sum(zip(data['2017'], data['2018'], data['2019']), ()) # like an hstack
source = ColumnDataSource(data=dict(x=x, counts=counts))
p = figure(x_range=FactorRange(*x), plot_height=400, title="Student Score by Year")
#toolbar_location=None, tools="")
p.vbar(x='x', top='counts', width=0.75,color ='dodgerblue', source=source)
p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xaxis.major_label_orientation = 1
p.xgrid.grid_line_color = None
show(p)
Color Mapping
We can also apply a color mapping, similar to the earlier example.
p.vbar(x='x', top='counts', width=0.75, source=source, line_color="white",
# use the palette to colormap based on the the x[1:2] values
fill_color=factor_cmap('x', palette=Spectral6, factors=years, start=1, end=2))
Factor Mapping
Another way we can set the color of the bars is to use a transform. Here we use a new one factor_cmap that accepts the name of a column to use for color mapping, as well as the palette and factors that define the color mapping.
Additionally, we can configure it to map just the sub-factors if desired. we want to only shade based on the year. So we pass start=1 and end=2 to specify the slice range of each factor to use when color mapping.
from bokeh.transform import factor_cmap
p = figure(x_range=FactorRange(*x), plot_height=400, title="Student Score by Year")
p.vbar(x='x', top='counts', width=0.75, source=source, line_color="white",
# use the palette to colormap based on the the x[1:2] values
fill_color=factor_cmap('x', palette=['salmon', 'forestgreen', 'dodgerblue'], factors=years, start=1, end=2))
p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xaxis.major_label_orientation = 1
p.xgrid.grid_line_color = None
show(p)
5. Stacked and Grouped Bar
We've already discussed stacked bar and grouped bar, respectively. In fact, it is also possible to combine these two together, if you have a piece of complicated information to show. Here is an example, let's add a new factor called "city" for the dataset. It will show you Berlin's and Munich's performance in the 4 quarters and each month.
If you have created a range with nested categories as above, it is possible to plot glyphs using only the "outer" categories. Additionally, if you want to address other information, for example, the average performance of each quarter, you can overlay a line directly.