-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathseaborn-charts2.py
More file actions
executable file
·212 lines (184 loc) · 7.19 KB
/
seaborn-charts2.py
File metadata and controls
executable file
·212 lines (184 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python3
"""seaborn-charts.py here.
Create chart for the data.
https://github.com/wilsonmar/python-samples/blob/main/seaborn-charts.py
Before running this:
chmod +x seaborn-charts.py
In Window machine command prompt:
python seaborn-charts.py
In Mac/Linux command line:
./seaborn-charts.py
Requires:
pip install pandas seaborn matplotlib statsmodels
# TODO:
[ ] Remove millisecs legend- Done
Sort Accuracy legend, green on top
Title to LLM Eval: Cost vs Accuracy vs Speed Scatter Plot - Done
Bargain! & Not worth it! overlay? text - Done
[ ] Add MNOVA results to chart - Done
[ ] Add trendline - Done
2nd chart with size by cost, color by accuracy- Done
Can we make the comments "Not Worth It!" and "Bargain!" in italics?- Done
Can we make the font larger for all chars? - Done
Can the legend be on the right on both plots? -Done
"""
__last_change__ = "25-09-29 v011 + 2nd chart legend hyphen fixed :seaborn-charts.py"
# Internal imports (no pip/uv add needed):
from datetime import datetime, timezone
try:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import statsmodels.multivariate.manova as manova
except Exception as e:
print(f"Python module import failed: {e}")
print("Please activate your virtual environment:\n python3 -m venv venv\n source venv/bin/activate")
exit(9)
#Read data from a CSV file into a pandas DataFrame
df = pd.read_csv('seaborn-charts.csv')
# Change figure size
# This must be done BEFORE creating the plot. Control your chart size.
plt.figure(figsize=(10, 6))
# Ensure 'x_column' and 'y_column' exist in your CSV file
sns.set_theme(style='darkgrid')
#Get the current UTC time
current_utc_time = datetime.now(timezone.utc)
formatted_time = current_utc_time.strftime('%Y-%m-%d %H:%M:%S UTC')
# The 'transform=plt.gca().transAxes' places the text relative to the axes
plt.text(
0.95, # x-position (0.0 to 1.0)
0.95, # y-position (0.0 to 1.0)
formatted_time,
horizontalalignment='right',
verticalalignment='bottom',
transform=plt.gca().transAxes,
fontsize=8,
color='grey'
)
plt.text(
0.80, # x-position (0.0 to 1.0)
0.80, # y-position (0.0 to 1.0)
"Not Worth It!",
horizontalalignment='right',
fontstyle='italic',
verticalalignment='bottom',
transform=plt.gca().transAxes,
fontsize=22,
color='#F08080'
)
plt.text(
0.20, # x-position (0.0 to 1.0)
0.20, # y-position (0.0 to 1.0)
'Bargain!',
horizontalalignment='right',
verticalalignment='bottom',
fontstyle='italic',
transform=plt.gca().transAxes,
fontsize=22,
color='#006400'
)
# Create a first chart
#change edge color and size of marker by changing s values
df.sort_values("Accuracy", ascending=False, inplace=True)
ax=sns.scatterplot(data=df, x='MilliSecs', y='USD cents',markers=True,color='cornflowerblue',hue='Accuracy',edgecolor='black',size='CoV',sizes=(50,200),palette='RdYlGn') # RdYlGn_r reverses the order
ax=sns.regplot(data=df, x='MilliSecs', y='USD cents', scatter=False, ci=None, color='grey',line_kws={'linestyle': '--'})
#Add plot titles and labels for clarity
plt.grid(axis='y', linestyle='--', alpha=0.7)
ax.set_title('LLM Eval: Cost vs Accuracy vs Speed Scatter Plot', fontsize=22)
plt.xlabel('Milliseconds response time')
plt.ylabel('USD cents cost')
sns.despine(trim=True, offset=5)
#Customize the Legend
# Place the legend outside the plot area
plt.legend(
bbox_to_anchor=(1.05, 1),
loc="upper left",
borderaxespad=0.
)
# Dynamically label each point with the LLM name
for index, row in df.iterrows():
# Place text to the right of each point
plt.annotate(
text=row['LLM'], # Use the LLM name as the label
xy=(row['MilliSecs'], row['USD cents']),
xytext=(10, 0), # Offset the text by 10 points to the right
textcoords='offset points',
ha='left', # Align the text to the left
fontsize=12
#arrowprops=dict(arrowstyle='', color='gray') # Optional arrow
)
# Calculate MNOVA values
#https://in.mathworks.com/discovery/manova.html for context
#https://online.stat.psu.edu/stat505/book/export/html/762 and https://www.statsmodels.org/dev/_modules/statsmodels/multivariate/manova.html
dfs=df[['Accuracy','USD cents']]# Dependent variables
Accuracy_Group=df[['MilliSecs']]# Independent variables
manova_result = manova.MANOVA.from_formula('dfs ~ Accuracy_Group', data=df)
results=manova_result.mv_test()
f_value = results.results['Accuracy_Group']['stat']['F Value'].iloc[0]# Get the F-value from Wilken's Lambda test
p_value = results.results['Accuracy_Group']['stat']['Pr > F'].iloc[0] # Get the p-value from Wilken's Lambda test
# Add text using ax.text()
ax.text(
0.25, # X-coordinate (5% from the left)
0.50, # Y-coordinate (95% from the bottom)
f"F={f_value:.2f} p={p_value:.2f}",#f"F:{f_value:.2f} p={p_value:.3f}", # Text to display
transform=ax.transAxes, # Use axes coordinates
fontsize=12,
bbox=dict(boxstyle="round,pad=0.3", fc='white', ec='none', alpha=0.7)
)
#Display the plot
plt.tight_layout() # Adjusts plot to fit figure
plt.show()
# Create a second chart
plt.figure(figsize=(10, 6))
# Ensure 'x_column' and 'y_column' exist in your CSV file
sns.set_theme(style='darkgrid')
df.sort_values("Accuracy", ascending=False, inplace=True)
ax=sns.scatterplot(data=df, x='Accuracy', y='USD cents',markers=True,color='cornflowerblue',hue='CoV',edgecolor='black',size='CoV',sizes=(50,200),palette='RdYlGn') # RdYlGn_r reverses the order
ax=sns.regplot(data=df, x='Accuracy', y='USD cents', scatter=False, ci=None, color='grey',line_kws={'linestyle': '--'})
#Add plot titles and labels for clarity
plt.grid(axis='y', linestyle='--', alpha=0.7)
ax.set_title('LLM Eval: Cost vs Accuracy Scatter Plot', fontsize=22)
plt.xlabel('Accuracy %')
plt.ylabel('USD cents cost')
sns.despine(trim=True, offset=5)
#Customize the Legend
# Place the legend outside the plot area
plt.legend(
bbox_to_anchor=(1.05, 1),
loc="upper left",
borderaxespad=0.,
title='Accuracy'
)
# Dynamically label each point with the LLM name
for index, row in df.iterrows():
# Place text to the right of each point
plt.annotate(
text=row['LLM'], # Use the LLM name as the label
xy=(row['Accuracy'], row['USD cents']),
xytext=(10, 0), # Offset the text by 10 points to the right
textcoords='offset points',
ha='left', # Align the text to the left
fontsize=12
#arrowprops=dict(arrowstyle='', color='gray') # Optional arrow
)
ax.text(
0.25, # X-coordinate (5% from the left)
0.50, # Y-coordinate (95% from the bottom)
f"F={f_value:.2f} p={p_value:.2f}",#f"F:{f_value:.2f} p={p_value:.3f}", # Text to display
transform=ax.transAxes, # Use axes coordinates
fontsize=12,
bbox=dict(boxstyle="round,pad=0.3", fc='white', ec='none', alpha=0.7)
)
plt.text(
0.95, # x-position (0.0 to 1.0)
0.95, # y-position (0.0 to 1.0)
formatted_time,
horizontalalignment='right',
verticalalignment='bottom',
transform=plt.gca().transAxes,
fontsize=8,
color='grey'
)
#Display the plot
plt.tight_layout() # Adjusts plot to fit figure
plt.show()