python实现时间轴图,Python中的时间轴

本文介绍了如何在Python中使用matplotlib库创建类似时间轴图表的方法,包括设置不同事件的开始和结束日期,以及如何在图表上添加注释。示例代码展示了如何创建不同周期的任务时间线,如每日、双月和每月任务。
摘要由CSDN通过智能技术生成

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.

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 =

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Python可以使用各种库和框架来实现时间轴,例如: 1. 使用Matplotlib库来绘制时间轴表,可以使用plot()函数来绘制数据点,使用axhline()函数来绘制时间轴线。 2. 使用Bokeh库来创建交互式时间轴,可以使用TimeSeries()函数来绘制时间序列数据,使用DatetimeTickFormatter()函数来格式化时间轴标签。 3. 使用Django框架来创建Web应用程序,可以使用Django的模板语言来渲染时间轴模板,使用Django的视函数来处理时间轴数据。 以上是一些常见的Python实现时间轴的方法,具体实现方式可以根据具体需求和技术水平来选择。 ### 回答2: 在Python实现时间轴可以有不同的方法,以下是其一种较为简单的方式: 1. 首先需要安装pandas和matplotlib库,可以使用pip命令进行安装。 2. 导入所需库 ```python import pandas as pd import matplotlib.pyplot as plt ``` 3. 创建数据框(dataframe)并在其添加时间和事件数据时间可以使用python datetime模块或者pandas.to_datetime()方法来转换为时间戳。 ```python data = {'time_stamp': ['2020-01-01', '2020-01-05', '2020-01-10', '2020-01-15'], 'event': ['Event 1', 'Event 2', 'Event 3', 'Event 4']} df = pd.DataFrame(data) df['time_stamp'] = pd.to_datetime(df['time_stamp']) ``` 4. 准备画布,并设置绘参数 ```python fig, ax = plt.subplots(figsize=(10,5)) ``` 5. 在画布上绘制时间轴 ```python ax.plot(df['time_stamp'], [0]*len(df), '|', markersize=20, color='g') ``` 6. 在时间轴上添加事件标签 ```python for i, txt in enumerate(df['event']): ax.annotate(txt, (df['time_stamp'][i], 0), xytext=(5,-15), textcoords='offset points', rotation=90) ``` 7. 隐藏坐标轴和边框 ```python ax.axis('off') ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['left'].set_visible(False) ax.spines['bottom'].set_visible(False) ``` 8. 显示绘 ```python plt.show() ``` 这样,就可以得到一个简单的时间轴。可以根据需要进行调整和美化,添加更多细节和交互功能,实现更加复杂的效果。 ### 回答3: Python实现timeline时间轴的方法可以有很多种,以下是一种可能的实现: 1. 数据处理 首先,我们需要将timeline的所有事件按照时间顺序排列,这就涉及到数据处理的问题。一种简单的方法是将每个事件存储为一个字典,其包含该事件的时间、标题、描述等信息,并将所有事件存储在一个列表。然后,我们可以使用Python内置的sorted()函数将这个列表按照事件时间排序。 2. 可视化 接下来,我们需要将排好序的事件数据可视化成时间轴Python有很多可用的可视化库,比如Matplotlib、PyQtGraph等。这里我们选用Matplotlib。 在Matplotlib,我们可以使用plot()函数绘制时间轴的基本轮廓,然后使用annotate()函数添加事件的标题和描述等信息。具体绘制代码如下: ```python import matplotlib.pyplot as plt # 绘制时间轴基本轮廓 plt.plot([0, 100], [0, 0], 'k-', linewidth=2) plt.plot([50, 50], [-0.5, 0.5], 'k-', linewidth=2) # 遍历事件数据,逐个添加事件的标题和描述 for event in events: time = event['time'] title = event['title'] desc = event['desc'] plt.annotate(title, xy=(time, 0.2), ha='center', va='bottom', fontsize=8) plt.annotate(desc, xy=(time, -0.2), ha='center', va='top', fontsize=8) # 显示时间轴 plt.axis('off') plt.show() ``` 3. 交互和动画 如果要让时间轴更加交互和生动,我们可以使用Matplotlib的事件处理功能和动画功能。比如,我们可以添加鼠标悬停事件处理函数,使得鼠标悬停在事件上时,显示该事件的详细信息。代码如下: ```python def on_mouse_move(event): if not event.inaxes: return for ann in ax.texts: if ann.contains(event)[0]: ann.set_visible(True) else: ann.set_visible(False) fig, ax = plt.subplots() for event in events: time = event['time'] title = event['title'] desc = event['desc'] ann = ax.annotate(title, xy=(time, 0.2), ha='center', va='bottom', fontsize=8) ax.annotate(desc, xy=(time, -0.2), ha='center', va='top', fontsize=8) ann.set_visible(False) fig.canvas.mpl_connect('motion_notify_event', on_mouse_move) ``` 另外,我们还可以使用动画功能,将时间轴的事件逐个显示出来。这需要用到Matplotlib的animation模块。具体实现步骤可以参考Matplotlib官方文档和示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值