You can download this code by clicking the button below.
This code is now available for download.
This code defines a function that generates a scatter plot using the Plotnine library based on an input DataFrame, randomly selecting a theme and color palette.
Technology Stack : Plotnine, Plotly Graph Objects, Pandas
Code Type : Function
Code Difficulty : Advanced
import random
import plotly.graph_objects as go
import pandas as pd
def random_plotnine_function(df, x, y):
"""
This function generates a random plot using Plotnine based on the input DataFrame.
"""
# Randomly select a theme from the available Plotnine themes
theme = random.choice([theme for theme in go.Figure().update_layout(theme).layout['theme']['name']])
# Randomly select a color palette from the available Plotnine palettes
palette = random.choice([palette for palette in go.Figure().update_layout(palette).layout['layout']['paper']['backgroundcolor']])
# Create a scatter plot
fig = go.Figure(data=[go.Scatter(x=df[x], y=df[y], mode='markers', marker=dict(color=palette))])
# Apply the selected theme
fig.update_layout(theme=theme)
# Show the plot
fig.show()
# Example usage
data = {'x': [1, 2, 3, 4, 5], 'y': [2, 3, 5, 7, 11]}
df = pd.DataFrame(data)
random_plotnine_function(df, 'x', 'y')