Random Subset Scatter Plot Generation

  • Share this:

Code introduction


The function randomly selects a subset of data from the given data and uses the scatterplot function of the Seaborn library to draw a scatter plot.


Technology Stack : Seaborn, NumPy, Matplotlib, Pandas

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

def random_distribution_plot(data, x, y):
    """
    This function generates a random plot of two variables from the data using Seaborn.
    """
    # Generate random subset of data
    np.random.seed(0)
    indices = np.random.choice(data.index, size=int(len(data) * 0.7), replace=False)
    subset_data = data.loc[indices]
    
    # Plotting the data
    sns.scatterplot(data=subset_data, x=x, y=y)
    plt.title('Random Scatter Plot')
    plt.xlabel(x)
    plt.ylabel(y)
    plt.show()

# Example usage
data = pd.DataFrame({
    'A': np.random.normal(0, 1, 100),
    'B': np.random.normal(0, 1, 100)
})
random_distribution_plot(data, 'A', 'B')