Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting labeled intervals in matplotlib/gnuplot

I have a data sample which looks like this:

a 10:15:22 10:15:30 OK b 10:15:23 10:15:28 OK c 10:16:00 10:17:10 FAILED b 10:16:30 10:16:50 OK 

What I want is to plot the above data in the following way:

captions ^   | c |         *------* b |   *---*    *--* a | *--*   |___________________                      time > 

With the color of lines depending on the OK/FAILED status of the data point. Labels (a/b/c/...) may or may not repeat.

As I've gathered from documentation for gnuplot and matplotlib, this type of a plot should be easier to do in the latter as it's not a standard plot and would require some preprocessing.

The question is:

  1. Is there a standard way to do plots like this in any of the tools?
  2. If not, how should I go about plotting this data (pointers to relevant tools/documentation/functions/examples which do something-kinda-like the thing described here)?
like image 793
dm3 Avatar asked Oct 07 '11 07:10

dm3


People also ask

How to change the grid interval of a Matplotlib plot?

We must use the matplotlib.pyplot.grid () function to show the grids. It displays a plot with grids in which grids are spaced as per the spacing of the ticks. We can change the grid interval by changing the spacing of ticks.

How to plot with data that contains dates in Matplotlib?

And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course The plot_date () function in pyplot module of matplotlib library is used to plot with data that contains dates.

How to plot a line with an interval at each data point?

To plot a line in Matplotlib with an interval at each data point, we can take the following steps − Set the figure size and adjust the padding between and around the subplots. Make an array of means and standard deviations. Plot means using plot () method. Fill the area between means+stds and means-stds, alpha=0.7 and color='yellow'.

How do you plot a data set with labels?

An object with labelled data. If given, provide the label names to plot in x and y. Technically there's a slight ambiguity in calls where the second label is a valid fmt. plot ('n', 'o', data=obj) could be plt (x, y) or plt (y, fmt).


1 Answers

Updated: Now includes handling the data sample and uses mpl dates functionality.

import matplotlib.pyplot as plt from matplotlib.dates import DateFormatter, MinuteLocator, SecondLocator import numpy as np from StringIO import StringIO import datetime as dt  ### The example data a=StringIO("""a 10:15:22 10:15:30 OK b 10:15:23 10:15:28 OK c 10:16:00 10:17:10 FAILED b 10:16:30 10:16:50 OK """)  #Converts str into a datetime object. conv = lambda s: dt.datetime.strptime(s, '%H:%M:%S')  #Use numpy to read the data in.  data = np.genfromtxt(a, converters={1: conv, 2: conv},                      names=['caption', 'start', 'stop', 'state'], dtype=None) cap, start, stop = data['caption'], data['start'], data['stop']  #Check the status, because we paint all lines with the same color  #together is_ok = (data['state'] == 'OK') not_ok = np.logical_not(is_ok)  #Get unique captions and there indices and the inverse mapping captions, unique_idx, caption_inv = np.unique(cap, 1, 1)  #Build y values from the number of unique captions. y = (caption_inv + 1) / float(len(captions) + 1)  #Plot function def timelines(y, xstart, xstop, color='b'):     """Plot timelines at y from xstart to xstop with given color."""        plt.hlines(y, xstart, xstop, color, lw=4)     plt.vlines(xstart, y+0.03, y-0.03, color, lw=2)     plt.vlines(xstop, y+0.03, y-0.03, color, lw=2)  #Plot ok tl black     timelines(y[is_ok], start[is_ok], stop[is_ok], 'k') #Plot fail tl red timelines(y[not_ok], start[not_ok], stop[not_ok], 'r')  #Setup the plot ax = plt.gca() ax.xaxis_date() myFmt = DateFormatter('%H:%M:%S') ax.xaxis.set_major_formatter(myFmt) ax.xaxis.set_major_locator(SecondLocator(interval=20)) # used to be SecondLocator(0, interval=20)  #To adjust the xlimits a timedelta is needed. delta = (stop.max() - start.min())/10  plt.yticks(y[unique_idx], captions) plt.ylim(0,1) plt.xlim(start.min()-delta, stop.max()+delta) plt.xlabel('Time') plt.show() 

Resulting image

like image 91
tillsten Avatar answered Sep 28 '22 22:09

tillsten