Randomized Color Scatter Plot Generator with Plotly

  • Share this:

Code introduction


This code defines a function that takes X and Y values as input and creates a scatter plot with lines and markers using the Plotly library. The plot has a randomly chosen color and includes a title and axis labels.


Technology Stack : Plotly library, Numpy, Python

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import random
import plotly.graph_objects as go
import numpy as np

def random_plotly_chart(x_values, y_values):
    # Generate a random color for the plot
    color = random.choice(['blue', 'green', 'red', 'purple', 'orange'])
    
    # Create a scatter plot
    fig = go.Figure(data=[go.Scatter(x=x_values, y=y_values, mode='lines+markers', marker_color=color)])
    
    # Update layout
    fig.update_layout(title='Random Plotly Scatter Plot', xaxis_title='X Values', yaxis_title='Y Values')
    
    # Show plot
    fig.show()

# Example usage
x = np.linspace(0, 10, 100)
y = np.sin(x)
random_plotly_chart(x, y)