Random Bar Plot Generator Using Seaborn

  • Share this:

Code introduction


This code defines a function to generate a random bar plot using Seaborn. The function accepts data, x-axis, y-axis, color grouping, palette, and figure size as parameters.


Technology Stack : Seaborn, NumPy, Matplotlib, Pandas

Code Type : The type of code

Code Difficulty : Advanced


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

def generate_random_bar_plot(data, x, y, hue=None, palette='viridis', figsize=(10, 6)):
    """
    Generate a random bar plot using Seaborn.
    """
    plt.figure(figsize=figsize)
    sns.barplot(data=data, x=x, y=y, hue=hue, palette=palette)
    plt.show()

# Example usage
np.random.seed(0)
data = np.random.choice(['A', 'B', 'C'], size=(100, 3))
data = pd.DataFrame(data, columns=['Category', 'Group', 'Value'])
generate_random_bar_plot(data, 'Category', 'Value', hue='Group')