
PYTHON — Multiple Glyphs and Adding Legend in Python
In theory, there is no difference between theory and practice. But, in practice, there is. — Jan L.A. van de Snepscheut

PYTHON — Introduction to Python Lambda Functions
# Multiple Glyphs and Adding Legend in Python
In this tutorial, we will learn how to create a visualization with multiple glyphs and add a legend using Python with the Bokeh library.
First, let’s import the necessary libraries and set up the data:
import numpy as np
from bokeh.io import output_file
from bokeh.plotting import figure, show
# My word count data
day_num = np.linspace(1, 10, 10)
daily_words = [450, 628, 488, 210, 287, 791, 508, 639, 397, 943]
cumulative_words = np.cumsum(daily_words)
# Output the visualization to a static HTML page my_tutorial_progress.html
output_file('my_tutorial_progress.html', title='My Tutorial Progress')
# Create a figure with a datetime type x-axis
fig = figure(title='My Tutorial Progress',
plot_height=400,
plot_width=700,
x_axis_label='Day Number',
y_axis_label='Words Written',
x_minor_ticks=2,
y_range=(0, 6000),
toolbar_location=None)Next, we will create the glyphs for the visualization:
# Represent daily words as vertical bars (columns)
fig.vbar(x=day_num, bottom=0, top=daily_words,
color='blue', width=0.75,
legend='Daily')
# The cumulative sum will be a trend line
fig.line(x=day_num, y=cumulative_words,
color='gray', line_width=1,
legend='Cumulative')
# Put the legend in the upper left corner
fig.legend.location = 'top_left'
# Show the visualization
show(fig)The vbar() and line() functions create the vertical bars and the trend line, respectively. They also specify the color, width, and legend for each glyph. Finally, we position the legend in the upper left corner and display the visualization using show().
By running the script, a visualization will be generated with vertical bars representing daily words and a trend line for the cumulative sum, along with a legend.
This tutorial demonstrates how to visualize data with multiple glyphs and add a legend using the Bokeh library in Python. For more information on styling legends, you can refer to the Bokeh user guide.







