Creating 3D Circle Scatter Plots with Bokeh: A Custom Extension Approach

Creating 3D Circle Scatter Plots with Bokeh: A Custom Extension Approach

===========================================================

In this article, we will explore the process of creating a 3D circle scatter plot using Bokeh, a popular data visualization library. While Bokeh does not have any built-in 3D plotting capabilities, we can achieve this by writing a custom extension for the library.

Introduction to Bokeh


Bokeh is an interactive visualization library that provides a high-level interface for creating web-based visualizations. It supports various types of plots, including scatter plots, bar charts, histograms, and more. However, its core library does not include support for 3D plotting.

The Need for Custom Extensions


To extend the capabilities of Bokeh to include 3D plotting, we need to write a custom extension. This involves creating a new module that will contain the code for our 3D plotting functionality. In this example, we will create a simple 3D scatter plot using PCA1, PCA2, and PCA3 as input coordinates.

Prerequisites


Before we begin, make sure you have Bokeh installed in your Python environment. You can install it using pip:

pip install bokeh

Additionally, you will need to have the numpy library installed:

pip install numpy

Creating the Custom Extension


To create a custom extension for Bokeh, we need to create a new module that contains our 3D plotting functionality. Let’s call this module bokeh_3d_plot.py.

In this module, we will define a function called create_3d_plot that takes in the input data and returns a 3D scatter plot object.

# bokeh_3d_plot.py

import numpy as np
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.transform import linear_cmap

def create_3d_plot(data):
    # Create the input data sources
    pca1_source = ColumnDataSource(data=dict(x=data['PCA1'], y=data['PCA2'], z=data['PCA3']))
    
    # Create the 3D scatter plot
    plot = figure(title="3D Scatter Plot",
                  x_axis_label='PCA1',
                  y_axis_label='PCA2',
                  z_axis_label='PCA3')
    
    # Add the 3D scatter plot to the figure
    plot.scatter(pca1_source['x'], pca1_source['y'],
                 size=9,
                 color='color',
                 alpha=0.3,
                 source=pca1_source)
    
    return plot

Integrating the Custom Extension with Bokeh


To integrate our custom extension with Bokeh, we need to create a new module that will contain the code for our 3D plotting functionality.

# bokeh_3d_plot.py (new)

from bokeh.models import ToolPanel
from bokeh.plotting import figure
from bokeh.transform import linear_cmap

def create_3d_plot(data):
    # Create the input data sources
    pca1_source = ColumnDataSource(data=dict(x=data['PCA1'], y=data['PCA2'], z=data['PCA3']))
    
    # Create the 3D scatter plot
    plot = figure(title="3D Scatter Plot",
                  x_axis_label='PCA1',
                  y_axis_label='PCA2',
                  z_axis_label='PCA3')
    
    # Add the 3D scatter plot to the figure
    plot.scatter(pca1_source['x'], pca1_source['y'],
                 size=9,
                 color='color',
                 alpha=0.3,
                 source=pca1_source)
    
    # Create a tool panel for interactive zooming
    plot Tools = [ToolPanel(tool="hover", point_policy="closest")]
    
    return plot

Using the Custom Extension with Bokeh


Now that we have created our custom extension, we can use it with Bokeh to create a 3D scatter plot.

# main.py

from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
from bokeh.transform import linear_cmap
import numpy as np

def main():
    # Create some sample data
    data = dict(x=np.random.rand(100), y=np.random.rand(100), z=np.random.rand(100))
    
    # Create the input data sources
    pca1_source = ColumnDataSource(data=dict(x=data['x'], y=data['y'], z=data['z']))
    
    # Create the 3D scatter plot using our custom extension
    plot = create_3d_plot(pca1_source.data)
    
    # Show the plot
    show(plot)

if __name__ == "__main__":
    main()

Conclusion


In this article, we explored the process of creating a 3D circle scatter plot using Bokeh by writing a custom extension for the library. We created a new module that contained our 3D plotting functionality and integrated it with Bokeh to create an interactive 3D scatter plot.

Note: The above code is just a basic example and may require modifications to work with your specific data and requirements.

Troubleshooting


If you encounter any issues while running the code, make sure that:

  • You have Bokeh installed in your Python environment.
  • You have the numpy library installed.
  • Your data is in the correct format for our custom extension.
  • You are using the correct version of Bokeh.

References


By following this article, you should be able to create a 3D circle scatter plot using Bokeh by writing a custom extension. If you have any questions or need further clarification on any of the concepts discussed in this article, feel free to ask!


Last modified on 2025-01-04