chat1
from bokeh.plotting import figure, show, output_notebook
from bokeh.layouts import gridplot
import polars as pl
# Enable Bokeh to display plots in the notebookimport matplotlib.pyplot as pltimport mathoutput_notebook()
def plot_dataframes_with_titles(plot_dataframes_with_titles_bokeh(dataframes_with_titles):
"""
Plots scatter plots for a list of (title, DataFrame) tuples.tuples using Bokeh.
Args:
dataframes_with_titles (List[Tuple[str, pl.DataFrame]]): List of tuples where each tuple contains a title and a polars DataFrame.
"""
num_plotsplots = 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)df in enumerate(dataframes_with_titles):dataframes_with_titles:
plt.subplot(rows,p cols,= ifigure(title=title, +x_axis_label=df.columns[0], 1)y_axis_label=df.columns[1])
plt.p.scatter(df[:, 0], df[:, 1])
plt.title(title) plt.xlabel(df.columns[0]) plt.ylabel(df.columns[1])plots.append(p)
plt.tight_layout()# Create a grid plot
plt.grid = gridplot([plots], ncols=2)
show()grid)
# 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(plot_dataframes_with_titles_bokeh(dataframes_with_titles)