55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
import pickle
|
|
|
|
with open('./results.pkl', 'rb') as fin:
|
|
drift,results = pickle.load(fin)
|
|
|
|
x_axis = np.asarray(drift)
|
|
results = {k:np.asarray(v) for k, v in results.items()}
|
|
import pandas as pd
|
|
|
|
# Crear los bins y asignar cada x a un bin
|
|
num_bins = 5
|
|
bins = np.linspace(x_axis.min(), x_axis.max(), num_bins + 1)
|
|
bin_labels = np.digitize(x_axis, bins) - 1 # Asignar cada x al bin correspondiente (restamos 1 para indexar desde 0)
|
|
|
|
# Crear la figura
|
|
plt.figure(figsize=(10, 6))
|
|
bin_positions = np.arange(num_bins) # Posiciones centrales de los bins
|
|
offset = 0.2 # Desplazamiento entre métodos
|
|
|
|
# Recorrer los métodos y construir boxplots para cada bin
|
|
for i, (method, y_values) in enumerate(results.items()):
|
|
binned_data = [[] for _ in range(num_bins)]
|
|
|
|
# Agrupar valores de y por los bins
|
|
for bin_idx in range(num_bins):
|
|
binned_data[bin_idx] = y_values[bin_labels == bin_idx]
|
|
|
|
# Crear un DataFrame para boxplot
|
|
binned_df = pd.DataFrame({f"Bin {j + 1}": pd.Series(data) for j, data in enumerate(binned_data)})
|
|
|
|
# Dibujar los boxplots con un desplazamiento en el eje x
|
|
positions = bin_positions + i * offset # Desplazar las posiciones
|
|
box = plt.boxplot(
|
|
binned_df.values,
|
|
positions=positions,
|
|
widths=0.15,
|
|
patch_artist=True,
|
|
showfliers=False,
|
|
boxprops=dict(facecolor=plt.cm.Set1(i / len(results)), alpha=0.7),
|
|
medianprops=dict(color="black"),
|
|
)
|
|
|
|
# Añadir el método a la leyenda con un marcador
|
|
plt.plot([], [], label=method, color=plt.cm.Set1(i / len(results)))
|
|
|
|
# Configurar la gráfica
|
|
plt.xticks(ticks=bin_positions, labels=[f"Bin {i + 1}" for i in range(num_bins)])
|
|
plt.xlabel("Bins")
|
|
plt.ylabel("Values")
|
|
plt.legend(title="Methods")
|
|
plt.title("Boxplot by Bins for Different Methods")
|
|
plt.tight_layout()
|
|
plt.show() |