5 Best Ways to Visualize Multiple Shapes on a Plot in Python Using Bokeh

Rate this post

πŸ’‘ Problem Formulation: Data visualization often requires representing multiple datasets or categories with varied geometries. In Python, the challenge is to display multiple shapes on a single Bokeh plot for clear and interactive data exploration. Consider a dataset containing different types of fruits with varying sizes and colors. Our aim is to develop a coherent plot that visually differentiates each type using distinct shapes and colors.

Method 1: Using Multi-Line Glyphs

The multi-line glyph function in Bokeh allows the plotting of multi-segment lines on the same graph. It is useful for visualizing data that can be represented as a collection of lines, such as time series with multiple entries or trajectories.

Here’s an example:

from bokeh.plotting import figure, show

# Create a new plot
p = figure(title="Multi-Line Glyphs Example")

# Add multi-line glyphs
p.multi_line(xs=[[1, 2, 3], [2, 3, 4]], ys=[[3, 2, 1], [4, 3, 2]],
             color=["firebrick", "navy"], alpha=[0.8, 0.3], line_width=4)

# Show the results
show(p)

The output is a Bokeh plot with two multi-segment lines, one in firebrick red and the other in navy blue.

This code snippet demonstrates how to deploy the multi_line() glyph to render lines of varying properties. Each list within xs and ys corresponds to the points of a single line. Attributes like color and transparency (alpha) can be customized to differentiate the lines.

Method 2: Combining Different Markers

Bokeh provides a variety of marker types that can be combined on the same plot. This diversity in shapes can highlight distinct groups or categories within the data.

Here’s an example:

from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource

# Define the data with a column for each shape type
source = ColumnDataSource(data=dict(
    x=[1, 2, 3, 4, 5],
    y=[6, 7, 2, 4, 5],
    marker=["circle", "square", "triangle", "diamond", "cross"]
))

# Create a new plot
p = figure(title="Combining Different Markers")

# Add shapes using the specified markers
for x, y, marker in zip(source.data['x'], source.data['y'], source.data['marker']):
    p.scatter(x, y, marker=marker, size=15)

# Show the results
show(p)

The output is a Bokeh plot showcasing five distinct marker types.

This snippet uses the scatter() method to add shapes onto the plot based on the marker type specified for each data point. This method is both flexible and visually effective for distinguishing between categories.

Method 3: Using Patches

Bokeh’s patches glyph allows drawing multiple polygonal shapes with different fill colors. This is particularly useful for geographic data or any shapes that can be defined by their vertices.

Here’s an example:

from bokeh.plotting import figure, show

# Create a new plot
p = figure(title="Patches Glyph Example")

# Define data for polygons (patches)
xs = [[1, 3, 2], [3, 4, 6], [2, 8, 7]]
ys = [[2, 1, 4], [4, 7, 8], [5, 3, 6]]

# Add patches
p.patches(xs, ys, fill_color=["red", "blue", "green"], line_color="black")

# Show the results
show(p)

The output is a Bokeh plot containing three colored polygons.

This example creates three distinct polygons (patches) on a plot by passing lists of x and y coordinates to the patches() method. Each patch can have a separate fill color to enhance distinction amongst them.

Method 4: Utilizing Annotations

Annotations such as labels and arrows can improve the readability of a plot by providing additional information. In Bokeh, Label and Arrow annotations can be strategically placed to describe or emphasize specific aspects of the plot.

Here’s an example:

from bokeh.plotting import figure, show
from bokeh.models import Label

# Create a new plot
p = figure(title="Annotation Example")

# Plot shapes
p.triangle([1, 3, 5], [3, 7, 5], size=20, color="orange")

# Define and add a label
label = Label(x=3, y=7, x_offset=12, text="Peak", text_baseline="middle")
p.add_layout(label)

# Show the results
show(p)

The output is a Bokeh plot with triangles and a label naming the peak point.

Here, Label() adds descriptive text next to a prominent data point (the peak) on the plot, enhancing the visualization’s interpretability. Annotations like these can clarify the meaning of various geometric shapes at a glance.

Bonus One-Liner Method 5: Drawing Custom Shapes with bokeh.models.glyphs

Bokeh’s flexible glyph interface enables the drawing of custom shapes. This is particularly powerful when default glyphs don’t suffice for specific visualization needs.

Here’s an example:

from bokeh.plotting import figure, show
from bokeh.models.glyphs import Wedge

# Create a new plot
p = figure(title="Custom Glyphs Example")

# Create a custom wedge glyph
wedge = Wedge(x=1, y=1, radius=0.5, start_angle=0.4, end_angle=4.8, fill_color="purple")

# Add to plot
p.add_glyph(wedge)

# Show the results
show(p)

The output is a Bokeh plot displaying a purple wedge shape.

This code demonstrates how to employ the Wedge glyph class from bokeh.models.glyphs to add a custom-shaped wedge onto the plot. You can define other custom shapes in a similar manner.

Summary/Discussion

  • Method 1: Multi-Line Glyphs. Great for representing paths or trajectories. Not ideal for categorical data with distinct shape requirements.
  • Method 2: Combining Different Markers. Versatile for categorical differentiation but may become cluttered with too many categories.
  • Method 3: Using Patches. Excellently visualizes complex, polygonal areas. Requires manual definition of vertices, which may be cumbersome for intricate shapes.
  • Method 4: Utilizing Annotations. Enhances data comprehension with contextual information. It is secondary to visual plotting and mostly serves as a supportive tool.
  • Bonus Method 5: Custom Shapes with bokeh.models.glyphs. Unlimited customization potential but requires more coding effort compared to using standard glyphs.