# -*- coding: utf-8 -*-
"""
@File  : 处理列车运行时刻表.py
@Author: Liu Tao
@Date  : 2022-11-08 21:03
@Desc  :
"""

import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator
import numpy as np
import xlrd



plt.figure(figsize=(8, 10), dpi=700)
ax = plt.gca()
ax.invert_yaxis()
train_id_list = []
station_id_list = []
time_id_list = []
ax.grid(True, linewidth=0.3, color='darkgray')



x = []
y = []

#book = xlrd.open_workbook("output_train_results.xlsx")
book = xlrd.open_workbook(r"t_d_7.xlsx")
sheet = book.sheet_by_index(0)
for l in range(1,sheet.nrows):
    train_id = int(sheet.cell_value(l,0))
    train_id_list.append(train_id)
    station_id = int(sheet.cell_value(l, 1))
    station_id_list.append(station_id)
    time_id = int(sheet.cell_value(l, 2))
    time_id_list.append(time_id)

train_id_matrix = []
station_id_matrix = []
time_id_matrix = []

# 计算共有多少个点
def countX(lst, x):
    count = 0
    for ele in lst:
        if (ele == x):
            count = count + 1
    return count
lst = train_id_list
num = countX(lst, 1)


for i in range(0,len(train_id_list),num):
    train_id_pieces = train_id_list[i: i+num]
    train_id_matrix.append(train_id_pieces)
    station_id_pieces = station_id_list[i: i + num]
    station_id_matrix.append(station_id_pieces)
    time_id_pieces = time_id_list[i: i + num]
    time_id_matrix.append(time_id_pieces)


#y_ticks = [1,10,20,52,61,67,69,71,74,77,84,109,113,117]
#plt.yticks(y_ticks)




print(train_id_matrix)
print(station_id_matrix)
print(time_id_matrix)

i = 0
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "blue", linewidth=0.3, linestyle='-')
plt.xticks(time_id_matrix[i][:])

plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

y_major_locator = MultipleLocator(1)  # 以每10显示
ax.yaxis.set_major_locator(y_major_locator)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 1
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "green", linewidth=0.3, linestyle='-')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 2
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "cyan", linewidth=0.3, linestyle='-')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 3
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "yellow", linewidth=0.3, linestyle='-')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 4
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "black", linewidth=0.3, linestyle='-')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 5
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "purple", linewidth=0.3, linestyle='-')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 6
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "chocolate", linewidth=0.3, linestyle='-')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 7
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "brown", linewidth=0.3, linestyle='-')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 8
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "darkviolet", linewidth=0.3, linestyle='-')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 9
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "orange", linewidth=0.3, linestyle='-')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 10
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "darkblue", linewidth=0.3, linestyle='-')
plt.xticks(time_id_matrix[i][:])

plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 11
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "olive", linewidth=0.3, linestyle='-')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 12
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "blue", linewidth=0.3, linestyle='-.')
plt.xticks(time_id_matrix[i][:])

plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 13
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "green", linewidth=0.3, linestyle='-.')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 14
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "cyan", linewidth=0.3, linestyle='-.')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 15
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "yellow", linewidth=0.3, linestyle='-.')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 16
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "black", linewidth=0.3, linestyle='-.')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 17
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "purple", linewidth=0.3, linestyle='-.')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 18
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "chocolate", linewidth=0.3, linestyle='-.')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 19
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "brown", linewidth=0.3, linestyle='-.')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 20
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "darkviolet", linewidth=0.3, linestyle='-.')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 21
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "orange", linewidth=0.3, linestyle='-.')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 22
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "darkblue", linewidth=0.3, linestyle='-.')
plt.xticks(time_id_matrix[i][:])

plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 23
plt.plot(time_id_matrix[i][:], station_id_matrix[i][:], "olive", linewidth=0.3, linestyle='-.')
plt.xticks(time_id_matrix[i][:])
plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)


i = 24
x_values = time_id_matrix[i][:]
y_values = station_id_matrix[i][:]
num_points = len(x_values)
for j in range(0, num_points, 5):
    if j + 5 <= num_points:
        plt.plot(x_values[j:j+5], y_values[j:j+5], "red", linewidth=0.3, linestyle='-')
    else:
        # 绘制剩余的点
        plt.plot(x_values[j:], y_values[j:], "red", linewidth=0.3, linestyle='-')

plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 25
x_values = time_id_matrix[i][:]
y_values = station_id_matrix[i][:]
num_points = len(x_values)
for j in range(0, num_points, 5):
    if j + 5 <= num_points:
        plt.plot(x_values[j:j+5], y_values[j:j+5], "red", linewidth=0.3, linestyle='-')
    else:
        # 绘制剩余的点
        plt.plot(x_values[j:], y_values[j:], "red", linewidth=0.3, linestyle='-')

plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)

i = 26
x_values = time_id_matrix[i][:]
y_values = station_id_matrix[i][:]
num_points = len(x_values)
for j in range(0, num_points, 5):
    if j + 5 <= num_points:
        plt.plot(x_values[j:j+5], y_values[j:j+5], "red", linewidth=0.3, linestyle='-')
    else:
        # 绘制剩余的点
        plt.plot(x_values[j:], y_values[j:], "red", linewidth=0.3, linestyle='-')

plt.xlabel('Time',fontproperties='Times New Roman',fontsize=15)
plt.ylabel('Station',fontproperties='Times New Roman',fontsize=15)
plt.yticks(fontproperties='Times New Roman', size=5)
plt.xticks(fontproperties='Times New Roman', size=10)

x_ticks = [0, 120, 240, 360, 390, 420, 720, 780, 900, 960]
plt.xticks(x_ticks)


plt.savefig("time-space graph2.png")
plt.show()