Bokeh-Based Random Color Generator

  • Share this:

Code introduction


This function uses the Bokeh library to create a random color generator that is displayed on a graphical interface.


Technology Stack : Bokeh, NumPy

Code Type : Custom function

Code Difficulty : Intermediate


                
                    
def generate_random_color():
    from bokeh.plotting import figure, show
    from bokeh.models import Circle, ColorBar, LinearColorAxis
    from numpy.random import uniform

    # Create a figure with a title and axis labels
    p = figure(title="Random Color Generator", tools="pan,wheel_zoom,box_zoom,reset", width=400, height=400)
    
    # Generate random color values
    x = uniform(0, 1, 100)
    y = uniform(0, 1, 100)
    colors = [(uniform(0, 1), uniform(0, 1), uniform(0, 1)) for _ in range(100)]
    
    # Create circles with random positions and colors
    source = Circle(x=x, y=y, fill_color=colors, size=10, alpha=0.5)
    p.add_source(source)
    
    # Add a color bar to the figure
    color_bar = ColorBar(color_axis=LinearColorAxis())
    p.add_layout(color_bar, 'right')
    
    # Display the figure
    show(p)                
              
Tags: