使用 python matplotlib 创建甘特图 [英] Create Gantt Plot with python matplotlib

查看:44
本文介绍了使用 python matplotlib 创建甘特图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 matplotlib 绘制具有该数据的图形.问题在于可视化从第2列到第3列的距离.最后,它看起来应该像甘特图.

How is it possible with matplotlib to plot a graph with that data. The problem is to visualize the distance from column 2 to column 3. At the end it should look like a Gantt chart.

0   0    0.016   19.833
1   0   19.834   52.805
2   0   52.806   84.005
5   0   84.012  107.305
8   0  107.315  128.998
10  0  129.005  138.956
11  0  138.961  145.587
13  0  145.594  163.863
15  0  163.872  192.118
16  0  192.127  193.787
17  0  193.796  197.106
20  0  236.099  246.223
25  1   31.096   56.180
27  1   58.097   64.857
28  1   64.858   66.494
29  1   66.496   89.908
31  1   89.918  111.606
34  1  129.007  137.371
35  1  137.372  145.727
39  1  176.097  209.461
42  1  209.476  226.207
44  1  226.217  259.317
46  1  259.329  282.488
47  1  282.493  298.905

第1列需要2种颜色.对于y轴,选择了0列,对于x轴,选择了第2列和第3列很重要.对于每一行,应绘制一条线.第二列是开始时间,第三列是停止时间.

I need 2 colors for column 1. And for the y-axis the column 0 is selected, for the x-axis the column 2 and 3 are important. For each row a line should be plotted. Column 2 is the start time, and column 3 is the stop time.

推荐答案

如果我没理解错的话,您想在第 3 列和第 4 列的 x 值之间绘制一条水平线,y 值等于列0.要在两个x值之间的给定y值处绘制水平线,可以使用 hlines .我相信下面的代码是一个可能的解决方案.

If I have understood you correctly, you want to plot a horizontal line between the x-values of the 3rd and 4th column, with y-value equal that in column 0. To plot a horizontal line at a given y-value between two x-values, you could use hlines. I believe the code below is a possible solution.

import numpy as np
import matplotlib.pyplot as plt

# Read data from file into variables
y, c, x1, x2 = np.loadtxt('data.txt', unpack=True)

# Map value to color
color_mapper = np.vectorize(lambda x: {0: 'red', 1: 'blue'}.get(x))

# Plot a line for every line of data in your file
plt.hlines(y, x1, x2, colors=color_mapper(c))

这篇关于使用 python matplotlib 创建甘特图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆