import matplotlib.pyplot as plt import numpy as np # Данные: (первый человек, второй человек, время1, время2) data = [ ("Елисей", "Ника", "01:53.68", "00:46.85"), ("Ника", "Вова", "00:57.28", "01:05.67"), ("Вова", "Елисей","00:38.35", "00:34.74"), ("Дима", "Саша", "00:57.45", "00:52.90"), ("Саша", "Дима", "00:25.69", "00:55.85"), ] def time_to_seconds(t): # поддерживает формат mm:ss.xx mm, rest = t.split(":") ss = float(rest) return int(mm) * 60 + ss def seconds_to_time(sec): mm = int(sec // 60) ss = sec % 60 return f"{mm:02d}:{ss:05.2f}" # Подготовка данных labels = [f"{a} → {b}" for a, b, _, _ in data] t1 = np.array([time_to_seconds(x[2]) for x in data]) t2 = np.array([time_to_seconds(x[3]) for x in data]) y = np.arange(len(data)) # Визуальный стиль plt.style.use("seaborn-v0_8-whitegrid") fig, ax = plt.subplots(figsize=(11, 6)) color1 = "#4C78A8" color2 = "#F58518" bars1 = ax.barh(y, t1, color=color1, edgecolor="none", label="Первое время") bars2 = ax.barh(y, t2, left=t1, color=color2, edgecolor="none", label="Второе время") # Подписи на сегментах for i, (b1, b2, a1, a2) in enumerate(zip(bars1, bars2, t1, t2)): ax.text( b1.get_x() + b1.get_width() / 2, b1.get_y() + b1.get_height() / 2, seconds_to_time(a1), ha="center", va="center", color="white", fontsize=10, fontweight="bold" ) ax.text( b2.get_x() + b2.get_width() / 2, b2.get_y() + b2.get_height() / 2, seconds_to_time(a2), ha="center", va="center", color="white", fontsize=10, fontweight="bold" ) # Подписи осей и оформление ax.set_yticks(y) ax.set_yticklabels(labels, fontsize=11) ax.invert_yaxis() ax.set_xlabel("Время, сек.") ax.set_title("Сравнение суммарного времени выполнения задания", fontsize=15, pad=12) ax.legend(loc="lower right") # Сетка и рамки ax.spines["top"].set_visible(False) ax.spines["right"].set_visible(False) ax.grid(axis="x", linestyle="--", alpha=0.4) plt.tight_layout() fig.savefig("first_test.png") plt.show()