2026-rff_mp/pogodinda/lab1/plot_results.py
2026-05-21 20:29:55 +03:00

85 lines
3.5 KiB
Python

import matplotlib.pyplot as plt
import csv
import numpy as np
# Читаем результаты из CSV
data = {}
with open('docs/data/results.csv', 'r', encoding='utf-8') as f:
reader = csv.reader(f)
next(reader)
for row in reader:
struct, mode, op, time = row[0], row[1], row[2], float(row[3])
if op not in data:
data[op] = {}
if struct not in data[op]:
data[op][struct] = {}
data[op][struct][mode] = time
# Создаём графики
operations = ['вставка', 'поиск', 'удаление']
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
# Цвета: синий и красный
colors = {'случайный': '#1f77b4', 'отсортированный': '#d62728'} # синий и красный
for idx, op in enumerate(operations):
ax = axes[idx]
structures = ['Связный список', 'Хеш-таблица', 'Двоичное дерево']
data_keys = ['LinkedList', 'HashTable', 'BST']
x = np.arange(len(structures))
width = 0.35
random_times = [data[op][key].get('случайный', 0) for key in data_keys]
sorted_times = [data[op][key].get('отсортированный', 0) for key in data_keys]
# Рисуем столбцы
bars1 = ax.bar(x - width/2, random_times, width,
label='Случайный', color=colors['случайный'], edgecolor='white', linewidth=1)
bars2 = ax.bar(x + width/2, sorted_times, width,
label='Отсортированный', color=colors['отсортированный'], edgecolor='white', linewidth=1)
ax.set_ylabel('Время (секунды)', fontsize=11)
ax.set_title(f'{op.upper()}', fontsize=13, fontweight='bold')
ax.set_xticks(x)
ax.set_xticklabels(structures, fontsize=10)
# Добавляем сетку
ax.grid(True, axis='y', alpha=0.3, linestyle='--')
ax.set_axisbelow(True)
# Убираем верхнюю и правую рамку
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# Добавляем значения на столбцы
for bar in bars1:
height = bar.get_height()
if height > 0:
ax.annotate(f'{height:.4f}',
xy=(bar.get_x() + bar.get_width()/2, height),
xytext=(0, 3), textcoords="offset points",
ha='center', va='bottom', fontsize=8)
for bar in bars2:
height = bar.get_height()
if height > 0:
ax.annotate(f'{height:.4f}',
xy=(bar.get_x() + bar.get_width()/2, height),
xytext=(0, 3), textcoords="offset points",
ha='center', va='bottom', fontsize=8)
# Легенда ПОД графиками, чтобы не накладывалась
fig.legend(labels=['Случайный', 'Отсортированный'],
loc='lower center', bbox_to_anchor=(0.5, -0.05),
ncol=2, fontsize=11, frameon=True, fancybox=True, shadow=True)
plt.suptitle('Сравнение производительности структур данных (10000 записей)',
fontsize=14, fontweight='bold', y=1.02)
plt.tight_layout()
plt.subplots_adjust(bottom=0.12) # Оставляем место для легенды снизу
plt.savefig('docs/data/graph.png', dpi=150, bbox_inches='tight', facecolor='white')
plt.show()
print("График сохранён в docs/data/graph.png")