Generating Random Scatter Plot with Plotly Express

  • Share this:

Code introduction


This function takes a DataFrame and a color value as input, and generates a scatter plot using Plotly Express. The color of the scatter plot can be customized through the 'color' parameter.


Technology Stack : Plotly Express, pandas, numpy

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import pandas as pd
import numpy as np
import plotly.express as px

def generate_random_plot(dataframe, color='blue'):
    """
    This function generates a random scatter plot using Plotly Express based on the provided DataFrame.
    """
    fig = px.scatter(dataframe, x='arg1', y='arg2', color=color)
    fig.show()

# Example DataFrame for testing
data = {
    'arg1': np.random.rand(50),
    'arg2': np.random.rand(50)
}
df = pd.DataFrame(data)

# Call the function with the example DataFrame
generate_random_plot(df)