Skip to main content

chat1

import polars as pl
import matplotlib.pyplot as plt
import math

def plot_dataframes_with_titles(dataframes_with_titles):
    """
    Plots scatter plots for a list of (title, DataFrame) tuples.
    
    Args:
    dataframes_with_titles (List[Tuple[str, pl.DataFrame]]): List of tuples where each tuple contains a title and a polars DataFrame.
    """
    num_plots = len(dataframes_with_titles)
    cols = math.ceil(math.sqrt(num_plots))
    rows = math.ceil(num_plots / cols)
    
    plt.figure(figsize=(5 * cols, 5 * rows))
    
    for i, (title, df) in enumerate(dataframes_with_titles):
        plt.subplot(rows, cols, i + 1)
        plt.scatter(df[:, 0], df[:, 1])
        plt.title(title)
        plt.xlabel(df.columns[0])
        plt.ylabel(df.columns[1])
    
    plt.tight_layout()
    plt.show()

# Example usage
df1 = pl.DataFrame({
    "x": [1, 2, 3, 4, 5],
    "y": [10, 20, 30, 40, 50]
})

df2 = pl.DataFrame({
    "x": [1, 2, 3, 4, 5],
    "y": [15, 25, 35, 45, 55]
})

df3 = pl.DataFrame({
    "x": [1, 2, 3, 4, 5],
    "y": [5, 15, 25, 35, 45]
})

dataframes_with_titles = [
    ("Scatter Plot 1", df1),
    ("Scatter Plot 2", df2),
    ("Scatter Plot 3", df3)
]

plot_dataframes_with_titles(dataframes_with_titles)