You can download this code by clicking the button below.
This code is now available for download.
This function randomly selects two columns from the provided DataFrame and generates a barplot using the Seaborn library.
Technology Stack : Seaborn, NumPy, Pandas, Matplotlib
Code Type : Function
Code Difficulty : Intermediate
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def generate_random_barplot(data):
"""
Generates a random barplot from the provided DataFrame using Seaborn.
"""
# Select a random column for the x-axis and a random column for the y-axis
x_column = data.columns[np.random.randint(data.shape[1])]
y_column = data.columns[np.random.randint(data.shape[1])]
# Generate the barplot
plt.figure(figsize=(10, 6))
sns.barplot(x=x_column, y=y_column, data=data)
plt.title(f"Random Barplot of {x_column} vs {y_column}")
plt.show()
# Example usage:
# Create a random DataFrame
np.random.seed(0)
data = pd.DataFrame({
'A': np.random.randn(100),
'B': np.random.randn(100),
'C': np.random.randn(100),
'D': np.random.randn(100)
})
# Generate a random barplot
generate_random_barplot(data)