π‘ Problem Formulation: Creating informative and appealing visual representations of data is a cornerstone of data analysis and interpretation in Python. A patch plot is particularly useful for displaying areas of interest within a dataset – such as territories on a map or boundaries of regions. Bokeh simplifies this process by providing interactive and scalable visualizations. Suppose we have a set of coordinates that define the vertices of polygons, we want to convert these into a colorful and interactive patch plot.
Method 1: Using the patches()
Function from Bokeh
Bokehβs patches()
function is specifically tailored for plotting multiple polygonal patches. It allows you to pass the coordinates of the vertices of the patches along with colors and other properties to produce a visualization. This method provides an accessible way to generate complex patch plots.
Here’s an example:
from bokeh.plotting import figure, show from bokeh.models import ColumnDataSource # Define the data data = dict( xs=[[1, 3, 2], [3, 4, 6, 6]], ys=[[2, 1, 4], [2, 3, 5, 2]], colors=["firebrick", "navy"] ) # Create a figure p = figure(title="Patch plot example") p.patches('xs', 'ys', color='colors', source=ColumnDataSource(data)) # Display the plot show(p)
The output is a plot with two polygonal patches: one in firebrick red and the other in navy blue.
This code creates a figure and uses the patches()
function to plot polygons based on the x and y coordinates provided. It also allows you to specify the color for each patch, resulting in a vibrant graphic. The use of ColumnDataSource
facilitates the binding of data source and the figure for interactive visualizations.
Method 2: Utilizing Glyph Methods with Bokeh
Glyph methods provide fine-grained control over the visual properties of a patch plot. By assigning specific βglyphsβ to data properties, bokeh allows for detailed customization for styling and interactivity of the patch plot.
Here’s an example:
from bokeh.plotting import figure, show # Sample data xs = [[1, 3, 2], [3, 4, 6, 6]] ys = [[2, 1, 4], [2, 3, 5, 2]] colors = ["green", "purple"] # Create a figure p = figure() p.patch(xs[0], ys[0], color=colors[0], alpha=0.8, line_width=2) p.patch(xs[1], ys[1], color=colors[1], alpha=0.8, line_width=2) # Display plot show(p)
The output is a patch plot with differently colored glyphs, each representing a patch with its own style and properties.
In this snippet, we create individual glyphs for each patch using the patch()
method. This allows for detailed styling such as alpha transparency, and line width of the patch boundaries. Each patch can have its own style without creating a separate data source for each.
Method 3: Patch Plots with Hover Tools
Bokeh’s interactive tools, like the HoverTool, can be used in conjunction with the patches to provide additional context or data on mouseover. This immensely enhances the usability and informativeness of a patch plot.
Here’s an example:
from bokeh.plotting import figure, show from bokeh.models import ColumnDataSource, HoverTool # Defining data with additional fields for the hover tool data = ColumnDataSource({ 'xs': [[1, 3, 2], [3, 4, 6, 6]], 'ys': [[2, 1, 4], [2, 3, 5, 2]], 'color': ["orange", "cyan"], 'label': ['A', 'B'] }) # Creating a figure and patches p = figure(tools="hover") p.patches('xs', 'ys', color='color', source=data) # Customizing the hover tool hover = p.select(dict(type=HoverTool)) hover.tooltips = [("Label", "@label")] # Display plot show(p)
By hovering over the patches, labels ‘A’ or ‘B’ will be displayed.
This method enhances interactivity by adding a HoverTool
that shows the ‘label’ when you hover over each patch. It demonstrates how additional data can be associated with each patch for informative visualizations.
Method 4: Creating Multi-Patch Plots for Comparative Analysis
Bokeh can also be used to compare multiple sets of data by plotting several patches on the same figure, possibly overlaying them to allow for comparative analysis or to highlight differences.
Here’s an example:
from bokeh.plotting import figure, show # Sample data xs1, ys1 = [[1, 3, 2]], [[2, 1, 4]] xs2, ys2 = [[1.5, 3.5, 2.5]], [[2.5, 1.5, 3.5]] # Create a figure p = figure() p.patches(xs1, ys1, color="blue", alpha=0.7) p.patches(xs2, ys2, color="red", alpha=0.7) # Display the plot show(p)
The output is a multi-patch plot, useful for observing the overlay and interaction between different datasets or categories.
This code snippet demonstrates how to plot multiple layers of patches for comparative purposes. Each patches()
method call overlays a new set of patches onto the figure, allowing for a detailed comparison of spatial or categorical data.
Bonus One-Liner Method 5: Using patch()
for a Single Patch
For quickly producing a single distinct, non-interactive patch, the patch()
method can be used as a one-liner to generate the desired plot with minimal effort.
Here’s an example:
show(figure().patch([1, 3, 2], [2, 1, 4], color="black"))
The output is a simple black patch plot.
This one-liner is highly convenient when the need arises to quickly visualize a single patch without the need for interactivity or multiple parameters. It’s straightforward and efficient for simple visuals.
Summary/Discussion
- Method 1: Using the
patches()
Function from Bokeh. Suitable for plotting multiple patches with minimal code. May not offer as much customization for individual patches. - Method 2: Utilizing Glyph Methods. Offers excellent control over individual patches’ properties. Can become verbose with an increasing number of patches.
- Method 3: Patch Plots with Hover Tools. Great for interactivity and providing additional context in visualizations. Requires more setup and understanding of Bokeh tools.
- Method 4: Creating Multi-Patch Plots. Ideal for comparative analyses. Overlapping patches may require careful management of color and transparency for clarity.
- Method 5: Using
patch()
for a Single Patch. Quick and easy one-liner for simple patch. Not suitable for complex or multi-patch plots.