Random Bar Plot Generation Using Seaborn

  • Share this:

Code introduction


This function uses the Seaborn library to generate a bar plot, where you can specify the x-axis and y-axis data, as well as an optional color for grouping. The function first converts the data into a DataFrame and then plots a bar plot based on these parameters.


Technology Stack : Seaborn, Numpy, Pandas, Matplotlib

Code Type : The type of code

Code Difficulty :


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

def generate_random_bar_plot(data, x, y, hue=None):
    """
    Generate a random bar plot using Seaborn.
    """
    # Create a DataFrame from the provided data
    df = pd.DataFrame(data)
    
    # Generate a random hue if specified
    if hue:
        df['Hue'] = np.random.choice(['A', 'B', 'C'], size=len(df))
    
    # Plotting the bar plot
    sns.barplot(x=x, y=y, hue=hue, data=df)
    
    plt.title("Random Bar Plot")
    plt.show()

# Example usage
data = {
    'Category': ['A', 'B', 'C', 'D', 'E'],
    'Values': [10, 20, 30, 40, 50]
}

generate_random_bar_plot(data, 'Category', 'Values')