From 82d2b61e6b8acd4f4036d2148c5dba1f7993de39 Mon Sep 17 00:00:00 2001 From: Niko Feith Date: Sun, 10 Sep 2023 11:53:29 +0000 Subject: [PATCH] =?UTF-8?q?Dateien=20hochladen=20nach=20=E2=80=9Eplotter?= =?UTF-8?q?=E2=80=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plotter/reward_plotter_final.py | 66 +++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 plotter/reward_plotter_final.py diff --git a/plotter/reward_plotter_final.py b/plotter/reward_plotter_final.py new file mode 100644 index 0000000..cf2b938 --- /dev/null +++ b/plotter/reward_plotter_final.py @@ -0,0 +1,66 @@ +import numpy as np +import matplotlib.pyplot as plt +import os + + +def plot_csv_with_titles(paths_dict, x_axis, y_axis, subplot_titles, y_limits=(0, 200)): + # Adjustments for dynamic subplot creation based on the dictionary size + num_subplots = len(paths_dict) + fig, axs = plt.subplots(1, num_subplots, figsize=(4*num_subplots, 4)) + + # If only one subplot, axs is not an array, so we need to convert it + if num_subplots == 1: + axs = [axs] + + for idx, (_, file_list) in enumerate(paths_dict.items()): + for path_, color in file_list: + data = np.genfromtxt(path_, delimiter=',', skip_header=0, dtype=float) + + mean = np.mean(data, axis=1) + std = np.std(data, axis=1) + x = np.linspace(0, mean.shape[0], mean.shape[0]) + + axs[idx].plot(x, mean, color=color) + axs[idx].fill_between( + x, + mean - 1.96 * std, + mean + 1.96 * std, + color=color, + alpha=0.5 + ) + axs[idx].set_title(subplot_titles[idx]) + axs[idx].set_xlabel(x_axis) + axs[idx].set_xlim([0, mean.shape[0]]) + axs[idx].grid(True) + axs[idx].set_ylim(y_limits) + + # Only label the y-axis for the leftmost plot + if idx == 0: + axs[idx].set_ylabel(y_axis) + else: + axs[idx].set_ylabel('') + + plt.tight_layout() + plt.show() + + +if __name__ == '__main__': + + filepaths = [ + "/mnt/data/cp-ei-regular-20_0-15-1686582970_1112866.csv", + "/mnt/data/cp-ei-random-0_95-15-1686579274_2881138.csv", + "/mnt/data/cp-cb-random-1_0-15-1686575989_8880587.csv", + "/mnt/data/cp-pi-random-1_0-15-1686575712_588163.csv" + ] + + # Demonstrating the adjusted function with subplot titles + titles = ["Plot 1", "Plot 2", "Plot 3", "Plot 4"] + + data_dict_colored = { + 'subplot1': [(filepaths[0], 'blue'), (filepaths[1], 'green')], + 'subplot2': [(filepaths[2], 'red')], + 'subplot3': [(filepaths[3], 'purple')], + 'subplot4': [] + } + + plot_csv_with_titles(data_dict_colored, 'Episodes', 'Reward', titles)