JOC / result /Figure7.py
BTBUzhy's picture
Upload 2 files
cdd939e verified
Raw History Blame Contribute Delete
2.43 kB
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from scipy.interpolate import make_interp_spline
# Load the CSV file
file_path = '/Users/admin/Downloads/satisfaction_df_3.csv'
data = pd.read_csv(file_path)
# Group the data by 'param' and get the 'selected_num' values
grouped_data = data.groupby('param')['selected_num'].apply(list)
# Create a DataFrame for boxplot
boxplot_data = pd.DataFrame(dict([(k, pd.Series(v)) for k, v in grouped_data.items()]))
# Calculate means for each group
means = boxplot_data.mean()
# Prepare data for the smooth curve
x = np.arange(len(means))
x_smooth = np.linspace(x.min(), x.max(), 200)
spl = make_interp_spline(x, means, k=3) # k=3 for cubic spline
means_smooth = spl(x_smooth)
# Plot the boxplot
plt.figure(figsize=(12, 8))
sns.set(style="whitegrid")
# Define custom colors
box_colors = [(219/255, 237/255, 243/255) for _ in range(len(boxplot_data.columns))]
# Create the boxplot with adjusted width and custom colors
ax = sns.boxplot(data=boxplot_data, linewidth=2.5, palette=box_colors, width=0.2, medianprops=dict(color='black', linewidth=2.5))
# Add smooth mean line
plt.plot(x_smooth, means_smooth, color='red', label='Mean', linewidth=2.5)
# Add mean points
plt.scatter(x, means, color='red')
# Customize the plot
ax.set_ylabel('Number of parameterized parking spots', fontsize=27)
ax.set_xlabel('Maximum allowed number of parameterized parking spots', fontsize=27)
ax.set_xticklabels([10, 25, 40, 55], fontsize=30)
ax.tick_params(axis='x', labelsize=30) # Increase x-axis tick label size
ax.tick_params(axis='y', labelsize=30) # Increase y-axis tick label size
ax.grid(True, axis='y', linestyle='--', linewidth=0.7)
# Add legend for median line and mean line
handles, labels = ax.get_legend_handles_labels()
# Create custom legend entries
median_handle = plt.Line2D([], [], color='black', linewidth=2.5, linestyle='-', label='Median')
mean_handle = plt.Line2D([], [], color='red', linewidth=2.5, linestyle='-', label='Mean')
# Add the custom legend entries manually
handles.extend([median_handle, mean_handle])
labels.extend(['Median', 'Mean'])
# Remove duplicates while maintaining order
unique_labels_handles = dict(zip(labels, handles))
ax.legend(unique_labels_handles.values(), unique_labels_handles.keys(), fontsize=30, loc='upper left')
plt.tight_layout()
plt.savefig('boxplot.pdf')
# Display the plot
plt.show()