Skip main navigation

Plotting with glyphs

Learn more about plotting with glyphs.

How do I plot with glyphs?

We’ve seen how to plot a single square glyph: by passing in a scalar x and y argument. Most glyph functions can also accept a sequence of x- and y-positions, which will then be translated to multiple glyphs on the plot. These coordinate sequences can be lists, Pandas DataFrames, or a Bokeh class called a ColumnDataSource.

Creating a scatter plot

Follow the instructions below to get started on your Notebook:

Step 1

First, we’ll make a scatter plot by plotting a positively correlated line of squares, and a parabola of circles.

Code:

p.square([-4, -3, -2, -1, 0, 1, 2, 3, 4], [16, 9, 4, 1, 0, 1, 4, 9, 16])
p.circle([-4, -3, -2, -1, 0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5, 6, 7, 8], color="red")
show(p) 

 

Output:

 

Screenshot from Jupyter Notebook that shows a line of red dots appearing diagonally on the plot. These are surrounded by a smattering of blue dots. On the top right of the chart there is an edit section where there is a 4-way arrow that is highlighted. X-axis from left to right reads: -4,-2,0,2,4. Y-axis from bottom to top reads: 0,5,10,15. Click to enlarge

 

Step 2

 

We can also draw lines with the ‘Figure.line’ method. We’ll do this on a new plot.

 

Code:

 

p.line([1, 2, 3, 4], [4, 3, 2, 1])
p.line([1, 2, 3, 4], [1, 2, 3, 4], color="red")
show(p) 

 

Output:

 

Screenshot from Jupyter Notebook that shows a thin red line and thin blue line crossing each other on the plot. X-axis from left to right reads: 1, 1.5, 2, 2.5, 3, 3.5, 4. Y-axis from bottom to top reads: 1, 1.5, 2, 2.5, 3, 3.5, 4. The thin red diagonal line starts from 1(y),1(x) and ends at 4(y), 4(x). The thin blue diagonal line starts from 4(y), 1(x) and ends at 1(y), 4(x). The two lines intersect at 2.5(y), 2.5(x). On the top right of the chart there is an edit section where there is a 4-way arrow that is highlighted.Click to enlarge

 

Plotting categorical data

 

Let’s have a look at plotting some categorical data. We can draw bar charts with the ‘Figure.vbar’ (for vertical) or ‘Figure.hbar’ (for horizontal) methods.

 

Step 1

 

We’ll look at vertical bar charts here. We need to provide the x-positions and the top (heights) of the bars:

 

Code:

 

p.vbar(x=[1, 2, 3], top=[1, 2, 3])

 

Output:

 

Screenshot from Jupyter Notebook that shows a blue filled-conjoined barchart which looks like a staircase. On the top right of the chart there is an edit section where there is a 4-way arrow that is highlighted. X-axis from left to right reads: 0.5, 1, 1.5, 2, 2.5, 3, 3.5. Y-axis from bottom to top reads: 0, 0.5, 1, 1.5, 2, 2.5, 3.Click to enlarge

 

Notice that the output of this looks a bit strange because the bars are simply lines of some sort. Let’s fix this.

 

Step 2

 

We can fix it by setting a width on the bar when plotting.

 

Code:

 

p.vbar(x=[1, 2, 3], top=[1, 2, 3], width=0.5)
show(p) 

 

Output:

 

Screenshot from Jupyter Notebook that shows a barchart with three blue bars with spaces between them. On the top right of the chart there is an edit section where there is a 4-way arrow that is highlighted. X-axis from left to right reads: 1, 1.5, 2, 2.5, 3. Y-axis from bottom to top reads: 0, 0.5, 1, 1.5, 2, 2.5, 3. The first bar goes up to 1(y). The second bar goes up to 2(y). The third bar goes up to 3(y).Click to enlarge

 

Ah! Doesn’t that look better now?

 

Step 3

 

What about the labels, though? The plot cannot make sense without it. To show category labels, we need to provide them as range data for the x-axis – sort of like setting the tick labels in Matplotlib. These are provided as the ‘x_range’ keyword argument when creating the plot:

 

Code:

 

x_range = ["Provider A", "Provider B", "Provider C"]
p = figure(x_range=x_range, plot_width=400, plot_height=400)

 

Step 4

 

We can then reuse the same list when drawing the bar.

 

Code:

 

p.vbar(x=x_range, top=[1, 2, 3], width=0.5)

 

Output:

 

Screenshot from Jupyter Notebook that shows a barchart with three blue bars with spaces between them and provider details listed at the bottom of the x-axis. On the top right of the chart there is an edit section where there is a 4-way arrow that is highlighted. X-axis from left to right reads: Provider A, Provider B, Provider C. Y-axis from bottom to top reads: 0, 0.5, 1, 1.5, 2, 2.5, 3. Provider A bar goes up to 1(y). Provider B bar goes up to 2(y). Provider C bar goes up to 3(y).Click to enlarge

 

Step 5

 

When drawing horizontal bars with the Figure.hbar method, you will use y instead of x, right instead of top, and height instead of width.

 

Code:

 

y_range = ["Provider A", "Provider B", "Provider C"]
p = figure(y_range=y_range, plot_width=400, plot_height=400)
p.hbar(y=y_range, right=[1, 2, 3], height=0.5)

 

Output:

 

Screenshot from Jupyter Notebook that shows a barchart with three horizontal blue bars with spaces between them and provider details listed at the left of the y-axis. On the top right of the chart there is an edit section where there is a 4-way arrow that is highlighted. X-axis from left to right reads: 0, 0.5, 1, 1.5, 2, 2.5, 3. Y-axis from bottom to top reads: Provider A, Provider B, Provider C. Provider A bar goes to 1(x). Provider B bar goes to 2(x). Provider C bar goes to 3(x).Click to enlarge

 

There are many more ways of drawing categorical data. If you would like to learn more and explore different customisations available, you can read the user guide here.

 

Read: Bokeh documentation [1]

 

Additional glyphs

 

So far, we plotted squares on bar and scatter plots. However, there are many shapes that can just be drawn at arbitrary positions and sizes. Let’s take a look at drawing rectangles.

 

quad method

 

With the ‘Figure.quad’ method, rectangles can be drawn by giving the positions of their top, bottom, left, and right edges:

 

Code:

 

p.quad(top=[0, 0], bottom=[1, 4], left=[1, 5], right=[3, 6])

 

Notice the output in your Jupyter Notebook.

 

rect method

 

Another way of drawing rectangles is with the ‘Figure.rect’ method, which uses a different format to specify the rectangle(s). This method takes the x and y arguments to set the coordinate of the centre, and the width and height keyword arguments to set the size.

 

Since we can also draw ellipses using the same arguments (with the ‘Figure.ellipse’ method), we’ll look at these two methods together:

 

Code:

 

p.rect(x=[0], y=[0], width=[0.5], height=[0.8])
p.ellipse(x=[0], y=[0], width=[0.1], height=[0.2], color="red")

 

We’ve provided the arguments as lists with single elements, but multiple glyphs can be drawn with different positions and sizes by using multi-element lists, as exhibited by the output below:

 

Notice the output in your Jupyter Notebook.

 

patch method

 

The last glyph method we’ll look at is patch, which allows drawing of arbitrary shapes by providing a list of x- and y-coordinates. For example, here’s how to draw a triangle:

 

Code:

 

p.patch(x=[1, 3, 2], y=[1, 1, 3])

Notice the output in your Jupyter Notebook.

There’s much more you can do, such as draw polygons with holes, or draw arcs and even colormaps. If you’d like to find out more, read the document to learn more.

Read: Plotting with Basic Glyphs [2].

References

  1. Handling Categorical Data [Document]. Bokeh; [date unknown]. Available from: https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html
  2. Plotting with Basic Glyphs [Document]. Bokeh; [date unknown]. Available from: https://docs.bokeh.org/en/latest/docs/user_guide/plotting.html
This article is from the free online

Data Visualisation with Python: Bokeh and Advanced Layouts

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now