Last modified: Jan 01, 2025 By Alexander Williams

Python Bokeh Patch: Create Filled Polygon Area Charts

The Bokeh library offers powerful tools for creating interactive data visualizations in Python. One of its versatile features is the patch() function, which allows you to create filled polygon plots and area charts.

Understanding Bokeh Patch Function

Before diving into area charts, make sure you have Bokeh installed properly. If you're new to Bokeh, check out our guide on how to install and get started with Python Bokeh.

Basic Area Chart Example

Let's start with a simple example of creating an area chart using patch():


from bokeh.plotting import figure, show
import numpy as np

# Create sample data
x = np.linspace(0, 10, 50)
y = 2 * np.sin(x) + 3

# Create figure
p = figure(title='Basic Area Chart', width=600, height=400)

# Create patch
p.patch(x=x, y=y, fill_color='blue', fill_alpha=0.3, line_color='blue')

# Show the plot
show(p)

Multiple Area Charts

You can create multiple area charts on the same figure to compare different datasets. Here's how to do it:


# Create multiple datasets
x = np.linspace(0, 10, 50)
y1 = 2 * np.sin(x) + 3
y2 = np.cos(x) + 2

p = figure(title='Multiple Area Charts', width=600, height=400)

# Create patches for both datasets
p.patch(x=x, y=y1, fill_color='blue', fill_alpha=0.3, legend_label='Sin')
p.patch(x=x, y=y2, fill_color='red', fill_alpha=0.3, legend_label='Cos')

show(p)

Customizing Area Charts

Bokeh offers various options to customize your area charts. Here's an example with advanced styling:


from bokeh.palettes import Spectral6

# Create styled area chart
p = figure(title='Styled Area Chart',
          width=800,
          height=400,
          background_fill_color='#f5f5f5')

# Add styled patch
p.patch(x=x,
       y=y1,
       fill_color=Spectral6[0],
       fill_alpha=0.6,
       line_color='black',
       line_width=2,
       legend_label='Data Series')

# Customize the plot
p.grid.grid_line_color = 'white'
p.grid.grid_line_width = 2
p.legend.location = 'top_right'
p.legend.click_policy = 'hide'

show(p)

Creating Stacked Area Charts

Stacked area charts are useful for showing cumulative values. Here's how to create them:


# Create sample data for stacking
x = np.linspace(0, 10, 50)
y1 = np.random.rand(50) * 3
y2 = np.random.rand(50) * 2
y3 = np.random.rand(50) * 1.5

# Calculate stacked values
y2_stacked = y1 + y2
y3_stacked = y2_stacked + y3

p = figure(title='Stacked Area Chart', width=700, height=400)

# Create stacked patches
p.patch(x=x, y=y3_stacked, fill_color='green', fill_alpha=0.3, legend_label='Layer 3')
p.patch(x=x, y=y2_stacked, fill_color='blue', fill_alpha=0.3, legend_label='Layer 2')
p.patch(x=x, y=y1, fill_color='red', fill_alpha=0.3, legend_label='Layer 1')

show(p)

Interactive Features

One of Bokeh's strengths is its interactive features. Similar to line plots and scatter plots, we can add hover tools:


from bokeh.models import HoverTool

# Create figure with hover tool
p = figure(title='Interactive Area Chart', width=600, height=400,
          tools='pan,box_zoom,reset,hover')

# Add hover tool
hover = HoverTool(tooltips=[
    ('x', '@x'),
    ('y', '@y')
])
p.add_tools(hover)

# Add interactive patch
p.patch(x=x, y=y, fill_color='purple', fill_alpha=0.3,
        line_color='purple', name='area')

show(p)

Best Practices and Tips

Choose appropriate colors with good contrast and opacity levels to ensure visibility of all elements in your chart.

Use legend labels effectively to identify different data series in your visualizations.

Consider interactive features like hover tools and zoom capabilities to enhance user experience.

Conclusion

Bokeh's patch() function is a powerful tool for creating area charts and filled polygons. It offers extensive customization options and interactive features for creating engaging visualizations.

Whether you're creating simple area charts or complex stacked visualizations, understanding these concepts will help you create effective and interactive data presentations.