I have a dataframe file_df with cols: path, param0, param1, param2, param3, param4 What…
- User
I have a dataframe
file_dfwith cols: path, param0, param1, param2, param3, param4What I usually do is:
bins = np.linspace(0, 1, 23) def x_y_from_file(file_path): # example processing: data = np.load(file_path) y, x = np.histogram(data, bins) return x, y file_content_df = file_df.copy() all_x_and_y_values = [x_y_from_file(p) for p in file_content_df["path"]] file_content_df["x_vals"] = [t[0] for t in all_x_and_y_values] file_content_df["y_vals"] = [t[1] for t in all_x_and_y_values] file_content_df = file_content_df.explode(["x_vals", "y_vals"]).reset_index(drop=True)and then
sns.relplot(data=file_content_df, kind="line", x="x_vals", y="y_vals", hue="param1", row="param2", col="param3")to get a plot of lines where the values are automatically averaged (estimator parameter of
sns.relplot) over "param4" and "param0". However, this time I have way too many and large files so this is not possible because the df would get too large.The best I have figured out to circumvent this is
from utils.mymath import online_mean_and_ci def load_and_average(data, color, **kwargs): bin_edges = np.linspace(0, 1, 23) bin_centers = (bin_edges[1:] + bin_edges[:-1]) / 2 mean_density, ci_low, ci_high = online_mean_and_ci( data_iterator=(x_y_from_file(p)[1] for p in data['path']), alpha=0.1 ) ax = plt.gca() ax.plot(bin_centers, mean_density, color=color, **kwargs) ax.fill_between(bin_centers, ci_low, ci_high, color=color, alpha=0.2) g = sns.FacetGrid(agents_crosslink_df, hue="param1", row="param2", col="param3", margin_titles=True) g.map_dataframe(load_and_average) g.add_legend() plt.show()What I still don't like about this approach:
- not generalizable to the different kinds of plots (
kindparameter in sns.relplot (like "line", "scatter")) - I have to do the averaging and estimator part by myself and not use the one in seaborn
REMINDER: Your solution should NOT use
online_mean_and_cibut seaborns! - not generalizable to the different kinds of plots (
- Assistant