如何使用matplotlib绘制酒吧时间线? [英] How to draw a bar timeline with matplotlib?
问题描述
我有以下数据类型:
开始结束事件2003 2007年赛事11991 2016 活动二2008 2016年赛事31986 2015 事件 42013 2013赛事51994 1999 事件 62002 2002年赛事7
我的目标是制定这些事件的时间表,即绘制从日期1到日期2的一系列不同的直线和水平条,上面带有事件的名称.
我目前正在使用matplotlib中的 barh
函数来试试运气,但是我想知道python中是否已经有一个现成的模块,可以与matplotlib和pandas很好地集成吗?
我认为这里不需要特殊功能.使用 plt.barh
直接为您提供所需的情节.
将matplotlib.pyplot导入为plt将numpy导入为npbegin = np.array([2003,1991,2008,1986,2013,1994,2002])end = np.array([2007,2016,2016,2015,2013,1999,2002])event = ["Event {}".format(i)for i in range(len(begin))]plt.barh(range(len(begin)),end-begin,left = begin)plt.yticks(range(len(begin)),event)plt.show()
请注意,事件 4 和 6 似乎丢失了,因为开始和结束是相同的.如果你想把end
解释为年末,你可以加1,
plt.barh(range(len(begin)),end-begin + 1,left = begin)
I have the type of following data:
Begin End Event
2003 2007 Event 1
1991 2016 Event 2
2008 2016 Event 3
1986 2015 Event 4
2013 2013 Event 5
1994 1999 Event 6
2002 2002 Event 7
My goal is to make a timeline of these events, ie to draw a series of distinct straight and horizontal bars from date 1 to date 2 with the names of the events on them.
I am currently trying my luck with the barh
function from matplotlib, but I wonder if there is already a ready-made module in python that would integrate nicely with matplotlib and pandas to do so ?
I don't think there is the need to for a special function here. Using plt.barh
is directly giving you the desired plot.
import matplotlib.pyplot as plt
import numpy as np
begin = np.array([2003,1991,2008,1986,2013,1994,2002])
end = np.array([2007,2016,2016,2015,2013,1999,2002])
event = ["Event {}".format(i) for i in range(len(begin))]
plt.barh(range(len(begin)), end-begin, left=begin)
plt.yticks(range(len(begin)), event)
plt.show()
Note that Event 4 and 6 seem missing, because start and end are identical. If you want to interprete end
as being the end of the year, you may add 1 to it,
plt.barh(range(len(begin)), end-begin+1, left=begin)
这篇关于如何使用matplotlib绘制酒吧时间线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!