Scatter Plot Generation with Plotly

  • Share this:

Code introduction


This function uses Plotly's GraphObjects module to create a scatter plot and displays it on the screen. The user needs to pass two lists as data for the x and y axes.


Technology Stack : Plotly, GraphObjects, Numpy

Code Type : Graphics drawing

Code Difficulty : Intermediate


                
                    
import plotly.graph_objects as go
import numpy as np

def generate_scatter_plot(x, y):
    # Create a trace
    trace = go.Scatter(x=x, y=y, mode='markers')

    # Create data array with the trace
    data = [trace]

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

    # Create figure
    fig = go.Figure(data=data, layout=layout)

    # Show plot
    fig.show()