5

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

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.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Thor
  • 158
  • 1
  • 1
  • 8
  • What have you tried? You will get much better responses here if you have show us what you have tried, as you question stands now it reads as 'please do my work for me'. – tacaswell Aug 05 '13 at 21:43
  • I'm a beginner, you are right. But I tried to plot lot of times. Plotting was not the problem, but the gantt design was. And for gantt design, there are no exampels available. – Thor Aug 06 '13 at 16:25

2 Answers2

16

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))

The resulting output

sodd
  • 12,482
  • 3
  • 54
  • 62
  • This looks very much like a Gantt chart already. If you're in for some more pain -- like learning LaTeX --, try [LaTeX pgfgantt](http://www.ctan.org/pkg/pgfgantt). – Sven Aug 06 '13 at 16:54
  • Nice package, but not flexible with big numbers. With data >300 it dosen't fit on one page. After scaling the tex box with \noindent\resizebox{\textwidth}{!} it is not possible to read the axis label. – Thor Aug 06 '13 at 19:15
1

You can read the text file using numpy.loadtxt, for example, and then plot it using matplotlib. For example:

import numpy as np
import matplotlib.pyplot as plt

x, y = np.loadtxt('file.txt', usecols=(2,3), unpack=True)
plt.plot(x,y)    

You should see the matplotlib documentation for more options.

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
  • I already have the Data in Python. Column 2+ 3 are on x axis.like a timeline. Column 0 is the y axis – Thor Aug 05 '13 at 21:02