Files
minimal_tetris_py/graph.py
T
2026-07-20 20:51:09 +02:00

91 lines
3.5 KiB
Python

import matplotlib.pyplot as plt
import matplotlib.patheffects as path_effects
import matplotlib.ticker as ticker
# ==========================================
# 0. HELPER FUNCTION
# ==========================================
def format_val(x, pos=None):
"""Formats numbers to k/M format for axes and labels"""
if x >= 1_000_000:
return f"{x/1_000_000:.1f}".replace('.0', '') + 'M'
elif x >= 1000:
return f"{x/1000:.1f}".replace('.0', '') + 'k'
return str(int(x))
# ==========================================
# 1. EDIT YOUR DATA HERE
# ==========================================
labels = ['Tetris AI test CPU', 'tet_cpu_per_row', '4 tet_cpu_per_row']
values = [41700, 909000, 909000*4] # e.g., Speed, FPS, Transactions per second (Higher is better)
# Define your unit here (add a space at the start so it looks clean)
unit_label = "Moves per Second [Moves/sec]"
brand_color = '#00E5FF'
competitor_color = '#888888'
multiplier = values[1] / values[0]
max_val = max(values)
black_outline = [path_effects.withStroke(linewidth=3, foreground='black')]
# ==========================================
# 2. SETUP FIGURE
# ==========================================
fig, ax = plt.subplots(figsize=(10, 4.5))
fig.patch.set_alpha(0.0)
ax.patch.set_alpha(0.0)
bars = ax.barh(labels, values, color=[competitor_color, brand_color], height=0.45, zorder=3)
# ==========================================
# 3. CONFIGURE AXES
# ==========================================
# X-Axis Tick Formatting (The requested change)
ax.xaxis.set_major_formatter(ticker.FuncFormatter(format_val))
for spine in ax.spines.values():
spine.set_visible(False)
ax.spines['bottom'].set_visible(True)
ax.spines['bottom'].set_color('white')
ax.spines['bottom'].set_path_effects(black_outline)
ax.tick_params(axis='both', colors='white', labelsize=12)
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_path_effects(black_outline)
ax.xaxis.grid(True, linestyle='--', color='white', alpha=0.3, zorder=0)
xlabel = ax.set_xlabel(unit_label, color='white', fontsize=12, labelpad=12, fontfamily='monospace')
xlabel.set_path_effects(black_outline)
# ==========================================
# 4. ADD BAR LABELS & BRACKET
# ==========================================
for bar, val in zip(bars, values):
width = bar.get_width()
txt = ax.text(width - (max_val * 0.02) if width >= (max_val*0.15) else width + (max_val*0.02),
bar.get_y() + bar.get_height()/2,
format_val(val),
ha='right' if width >= (max_val*0.15) else 'left',
va='center', color='white', fontsize=14, fontweight='bold')
txt.set_path_effects(black_outline)
# Draw Bracket
y0, y1 = [b.get_y() + b.get_height()/2 for b in bars]
x_line, x_tick = max_val * 1.05, max_val * 1.02
for x, y in zip([x_tick, x_tick, x_line], [y1, y0, [y0, y1]]):
l, = ax.plot([x_tick, x_line] if isinstance(y, (float, int)) else [x_line, x_line],
[y, y] if isinstance(y, (float, int)) else y, color=brand_color, lw=2)
l.set_path_effects(black_outline)
multiplier_txt = ax.text(x_line + (max_val * 0.03), (y0+y1)/2, f'{multiplier:.1f}x\nFASTER',
ha='left', va='center', color=brand_color, fontsize=22, fontweight='heavy')
multiplier_txt.set_path_effects(black_outline)
ax.set_xlim(0, max_val * 1.45)
plt.tight_layout()
plt.subplots_adjust(bottom=0.2)
plt.savefig('final_graph.png', dpi=300, bbox_inches='tight', transparent=True)
plt.show()