完整代码如下,怎么将绘制的图形的x轴改为时间呀?现在是将x轴的设置注释掉了如图,一加上,图形就不对了如图
import matplotlib.pyplot as plt
import datetime
import matplotlib.dates as mdates
stations_up = ['北京' ,'北京' ,'天津','天津', '石家庄', '石家庄', '邯郸','邯郸']
stations_down=[ '邯郸','邯郸', '石家庄', '石家庄','天津','天津','北京' ,'北京']
trains_up = [
{'schedule': [(0, '08:00:00'), (1, '08:00:30'), (2, '08:01:10'),(3, '08:01:50'),(4, '08:02:50'),(5, '08:03:10'),(6, '08:04:50'),(7, '08:05:50')]},
{'schedule': [(0, '08:01:00'), (1, '08:01:30'), (2, '08:02:10'),(3, '08:02:50'),(4, '08:03:50'),(5, '08:04:10'),(6, '08:05:50'),(7, '08:06:50')]},
{'schedule': [(0, '08:02:00'), (1, '08:02:30'), (2, '08:03:10'),(3, '08:03:50'),(4, '08:04:50'),(5, '08:05:10'),(6, '08:06:50'),(7, '08:07:50')]}
]
trains_down=[
{'schedule': [(0, '08:02:00'), (1, '08:02:30'), (2, '08:03:10'),(3, '08:03:50'),(4, '08:04:50'),(5, '08:05:10'),(6, '08:06:50'),(7, '08:07:50')]}
]
#对于列车上行
for t in trains_up:
for i in range(len(t['schedule'])):
dt = datetime.datetime.strptime(t['schedule'][i][1], '%H:%M:%S')
t['schedule'][i] = (t['schedule'][i][0], dt.hour + dt.minute/60.0+dt.second/3600.0)
for t in trains_up:
times = [x[1] for x in t['schedule']]
plt.plot(times,stations_up,linewidth=1)
#对于下行
for t in trains_down:
for i in range(len(t['schedule'])):
dt = datetime.datetime.strptime(t['schedule'][i][1], '%H:%M:%S')
t['schedule'][i] = (t['schedule'][i][0], dt.hour + dt.minute/60.0+dt.second/3600.0)
for t in trains_down:
times = [x[1] for x in t['schedule']]
plt.plot(times,stations_down,linewidth=1)
#横纵轴的基本设置
# times = [ '8:00', '08:10', '08:20', '08:30', '08:40', '08:50', '09:00', '09:10', '09:20', '09:30', '09:40']
# plt.xticks(range(len(times)), times)
plt.rcParams['font.sans-serif'] = ['SimSun'] #设置字体,
plt.rcParams['axes.unicode_minus'] = False #plt.rcParams[‘axes.unicode_minus’]是一个配置参数,它决定了坐标轴上负号的显示方式
plt.xlabel('时间')
plt.ylabel('站点')
plt.title('列车运行图')
plt.legend()
plt.show()