avatarLaxfed Paulacy

Summary

This web content provides a tutorial on creating a data visualization with multiple glyphs and a legend in Python using the Bokeh library.

Abstract

The article is a step-by-step guide on how to create a data visualization in Python that includes multiple glyphs, such as vertical bars and a trend line, and how to add a legend to the plot. It begins by importing necessary libraries, setting up the data, and configuring the output to a static HTML page. The tutorial then demonstrates how to create the visualization elements using Bokeh's vbar() and line() functions, specifying properties like color, width, and legend labels. The legend is positioned in the upper left corner of the plot, and the final visualization is displayed using Bokeh's show() function. The article concludes by noting that the generated visualization will show daily words as vertical bars and a cumulative sum as a trend line, with a legend included for clarity. For further customization of legends, readers are directed to the Bokeh user guide.

Opinions

  • The author emphasizes the practicality of Bokeh for creating interactive visualizations in Python.
  • The use of multiple glyphs is presented as an effective way to represent different aspects of data within a single plot.
  • Positioning the legend in the upper left corner is suggested as a good practice for visualization readability.
  • The article implies that the Bokeh library is user-friendly and suitable for those looking to create visualizations with Python.
  • By providing a link to the Bokeh user guide, the author encourages readers to explore further styling options for their visualizations.

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.

PYTHON — Functions, Iterables, and Iterators in Python

Legend
ChatGPT
Python
Multiple
Glyphs
Recommended from ReadMedium