Python中的时间轴

编程入门 行业动态 更新时间:2024-10-27 21:25:24
本文介绍了Python中的时间轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以使用类似于这篇文章的方法在Python中创建时间轴仅使用1个vizualiation程序包,而没有其他设置?我尝试使用plotnine包在Python中使用ggplot2,但这要使其工作起来非常麻烦.此外,我尝试了 labella 软件包,但这需要安装Latex发行版.使用matplotlib,我找不到在事件栏旁边添加评论的方法.

Is there any way to create a timeline in Python similar to this post using only 1 vizualiation package and no other setup? I have tried to use the plotnine package to use ggplot2 within Python but this is quite cumbersome to get it to work. Furthermore, I have tried the labella package but this requires installation of a Latex distribution. With matplotlib I can't find a way to include the comments next to the event bars.

推荐答案

我也遇到了这个问题……这是我的解决方案.请忽略丑陋的颜色生成" ...这更多的是正在进行的工作.

I also had this issue ... and this is my solution. Please ignore the Ugly Color Generation ... this is more of a work in progress.

这是经过Python 3.6测试的....

It is Python 3.6 tested ....

import numpy as np import matplotlib.pylab as plt import pandas as pd from cycler import cycler from datetime import datetime, timedelta idx = pd.date_range('2018-1-1', '2018-9-10', freq='1D') df = pd.DataFrame({'Offset': 20,'val': 2}, index=idx) df['Offset']=[n for n in range(0,len(df))] sched=[{'name':'tim', 'jobs':{ 1:(datetime(2018,1,1,),datetime(2018,2,1)), 2:(datetime(2018,4,1) ,datetime(2018,5,1)), 3:(datetime(2018,6,1) ,datetime(2018,7,1))} }, {'name':'BiMonthly', 'jobs':{ 1:(datetime(2018,2,1,),datetime(2018,3,1)), 2:(datetime(2018,5,1) ,datetime(2018,6,1)), 3:(datetime(2018,7,1) ,datetime(2018,8,1))} } , {'name':'Monthly', 'jobs':{ 1:(datetime(2018,2,1),datetime(2018,2,10)), 2:(datetime(2018,3,1),datetime(2018,3,10)), 3:(datetime(2018,4,1),datetime(2018,4,10)), 4:(datetime(2018,5,1),datetime(2018,5,10)), 5:(datetime(2018,6,1),datetime(2018,6,10)) }}, {'name':'LongTerm', 'jobs':{ 1:(datetime(2018,2,1),datetime(2018,5,1)) } }] color_cycle = cycler(c=['r', 'g', 'b']) ls_cycle = cycler('ls', ['-', '--']) sty_cycle = ls_cycle * ( color_cycle) def get_offset(when): global df if type(when)==str: when=pd.to_datetime(when) try: return df.loc[when]['Offset'] except KeyError: print("{} Not Found".format(when)) return -1 thickness=0.3 timelines=[] start_period = idx[0].to_period('D').ordinal for a_job_group in sched: timeline=[] print("-----") for keys in a_job_group['jobs']: #print("Dates {} {}".format(a_job_group['jobs'][keys][0], # a_job_group['jobs'][keys][1])) offset_start = get_offset(a_job_group['jobs'][keys][0]) offset_end = get_offset(a_job_group['jobs'][keys][1]) print("offset {} {} TimeSpan {}".format(offset_start, offset_end, offset_end - offset_start)) timeline_data=(start_period + offset_start,offset_end-offset_start) timeline.append(timeline_data) timelines.append(timeline) pos= 0 df.drop(['Offset'],axis=1,inplace=True,) ax = df.plot(color='w') col_schema=[s for s in sty_cycle] for t in timelines: ax.broken_barh(t, [pos, thickness], color=col_schema[pos]['c'], linestyle=col_schema[pos]['ls']) pos+= 1 plt.show()

它输出什么?

我必须添加一个索引-并检查我是否可以更改TimeSteps(小时数等),但到目前为止,这是我能找到的最佳解决方案.

I have to add an Index - and to check that I can change the TimeSteps (hours weeks etc) but so far this it the best solution I can find.

我计划向其中添加 mpld3 -然后通过Flask运行它....因此,我还有一些路要走.

I plan to add mpld3 to it - and then to run it via Flask.... So I have a little ways to go.

更多推荐

Python中的时间轴

本文发布于:2023-11-22 09:35:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616910.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!