Generating Random Scatter Plot with Plotly

  • Share this:

Code introduction


This function creates a scatter plot using Plotly, where the x-axis and y-axis data are passed as arguments, and the y-axis data is randomly generated. Each point in the chart is accompanied by a text label.


Technology Stack : Plotly (plotly.graph_objects)

Code Type : Plotly chart

Code Difficulty : Intermediate


                
                    
import random
import plotly.graph_objects as go

def generate_random_plot(arg1, arg2):
    # Create a trace
    trace = go.Scatter(
        x=[arg1, arg2],  # Sample data
        y=[random.randint(1, 100), random.randint(1, 100)],  # Random data
        mode='markers+text',
        text=['A', 'B'],
        name='Random Data'
    )

    # Define layout
    layout = go.Layout(
        title='Random Data Scatter Plot',
        xaxis={'title': 'X Axis Title'},
        yaxis={'title': 'Y Axis Title'}
    )

    # Create the figure
    fig = go.Figure(data=[trace], layout=layout)

    # Show the figure
    fig.show()