2201_75814951 2023-05-26 16:47 采纳率: 12.5%
浏览 94
已结题

python如何将x轴变为时分秒格式的

完整代码如下,怎么将绘制的图形的x轴改为时间呀?现在是将x轴的设置注释掉了如图,一加上,图形就不对了如图

img

img


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()
  • 写回答

5条回答 默认 最新

  • PhoenixRiser 2023-05-26 17:03
    关注
    获得0.75元问题酬金

    TechWhizKid参考GPT回答:

    • 用matplotlib.dates库 ,把时间还原为日期时间对象(datetime),并在绘图时使用mdates.DateFormatter来设置时间的格式。

    img

    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)  # 注意这里修改了时间格式
    
    for t in trains_up:
        times = [mdates.date2num(x[1]) for x in t['schedule']]  # 使用mdates.date2num转换日期时间
        plt.plot(times, stations_up, linewidth=1)  # 使用plt.plot来绘制时间数据
    
    #对于下行
    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)  # 注意这里修改了时间格式
    
    for t in trains_down:
        times = [mdates.date2num(x[1]) for x in t['schedule']]  # 使用mdates.date2num转换日期时间
        plt.plot(times, stations_down, linewidth=1)  # 使用plt.plot来绘制时间数据
    
    #横纵轴的基本设置
    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))  # 使用mdates.DateFormatter设置时间格式
    plt.gcf().autofmt_xdate()  # 自动旋转x轴的日期标签以避免重叠
    
    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()
    
    
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 6月3日
  • 赞助了问题酬金15元 5月26日
  • 创建了问题 5月26日

悬赏问题

  • ¥15 在matlab中Application Compiler后的软件无法打开
  • ¥15 想问一下STM32创建工程模板时遇到得问题
  • ¥15 Fiddler抓包443
  • ¥20 Qt Quick Android 项目报错及显示问题
  • ¥15 而且都没有 OpenCVConfig.cmake文件我是不是需要安装opencv,如何解决?
  • ¥15 oracleBIEE analytics
  • ¥15 H.264选择性加密例程
  • ¥50 windows的SFTP服务器如何能批量同步用户信息?
  • ¥15 centos7.9升级python3.0的问题
  • ¥15 安装CentOS6时卡住