Scatter Plot with Color Scale Representation

  • Share this:

Code introduction


This function creates a scatter plot and uses a color bar to represent the color values of the data. It accepts a dataset containing x, y, and color values.


Technology Stack : Bokeh, ColumnDataSource, ColorBar, linear_cmap

Code Type : Bokeh visualization

Code Difficulty : Intermediate


                
                    
def plot_scatter_with_colorbar(data):
    from bokeh.plotting import figure, show
    from bokeh.models import ColumnDataSource, ColorBar
    from bokeh.transform import linear_cmap

    # Create a ColumnDataSource for the data
    source = ColumnDataSource(data)

    # Create a figure
    p = figure(title="Scatter Plot with Colorbar", x_axis_label='X-axis', y_axis_label='Y-axis')

    # Add a scatter plot
    p.scatter('x', 'y', source=source, color='color', cmap=linear_cmap('color', low=min(data['color']), high=max(data['color'])))

    # Add a color bar
    color_bar = ColorBar(title="Color Scale")
    p.add_layout(color_bar, 'right')

    # Display the plot
    show(p)