You can download this code by clicking the button below.
This code is now available for download.
This function generates a random color palette that can be used for coloring Seaborn plots.
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 random_color_palette():
"""
This function generates a random color palette for seaborn plots.
"""
# Create a DataFrame with random RGB values
random_colors = pd.DataFrame({
'R': np.random.randint(0, 256, size=8),
'G': np.random.randint(0, 256, size=8),
'B': np.random.randint(0, 256, size=8)
})
# Convert RGB values to hexadecimal
random_colors['Color'] = random_colors.apply(lambda x: '#' + ''.join([f'{x[i]:02x}' for i in [0, 2, 4]]), axis=1)
return random_colors['Color'].tolist()
# Example usage
color_palette = random_color_palette()
sns.set_palette(color_palette)
plt.figure(figsize=(10, 6))
sns.scatterplot(x=np.random.rand(50), y=np.random.rand(50))
plt.show()