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()
# SampleExample DataFrameusagedfdf1 = pl.DataFrame({
"stuff"x": [None, None, 1, 1,2, 1,3, None,4, None,5],
1, 1,"y": None,[10, 1,20, None,30, 1,40, 1, 1, 1]50]
})
# Create a forward-looking column to compare with the current 'stuff' columndfdf2 = df.with_column(pl.DataFrame({
pl.col("stuff").shift(-1).alias(x": [1, 2, 3, 4, 5],
"next_stuff")y": [15, 25, 35, 45, 55]
})
# Identify rows where a change from 1 to None or None to 1 will occurdfdf3 = df.with_column(pl.DataFrame({
"x": [1, 2, 3, 4, 5],
"y": [5, 15, 25, 35, 45]
})
dataframes_with_titles = [
(pl.col("stuff")Scatter !=Plot pl.col(1", df1),
("next_stuff")).alias(Scatter Plot 2", df2),
("will_change")Scatter Plot 3", df3))]
# Count the number of changes from 1 to None, indicating the end of a group of 1scount_groups = df.filter((pl.col("stuff") == 1) & (pl.col("next_stuff").is_null())).count()
print("Number of contiguous groups of 1s:", count_groups)plot_dataframes_with_titles(dataframes_with_titles)