From: John H. <jdh...@ac...> - 2004-04-06 19:57:33
|
>>>>> "Peter" == Peter Groszkowski <pgr...@ge...> writes: Peter> Hi everyone: I was wondering whether it is possible to tell Peter> matplotlib how/when to connect data points. Consider this Peter> simple script: Peter> from matplotlib.matlab import * figure(1) t = Peter> [0,1,2,3,4,5,105,106,107] s = [1,4,5,3,9,11,-5,-8,3] Peter> plot(t, s, antialiased=False) grid(True) show() Peter> There are no data points between t=5 and t=105. By default Peter> the points (5,11) and (105,-5) are connected, but I would Peter> like to tell matplotlib NOT to do so. In my case I would Peter> like to pass the plot function a variable telling it what Peter> to do. So for example would have: Peter> plot(t, s, max_delta=40) Peter> This would mean that the points are only to be connected if Peter> the difference between the adjacent t values is less than Peter> 40. In my case this is relevant because sometimes there Peter> are "holes" in my data, and connecting the points makes the Peter> plots look very messy. Peter> Would anyone find something like this useful? Would it be Peter> difficult to implement? Certainly not difficult, and probably useful enough to put in the standard distro. Eg, in a stock market trading example, you would have lots of quotes, minute by minute, punctuated by long intervals overnight where the market is closed. If you set maxdelta appropriately, you could draw connected lines only within trading days. Here is a sample implementation from matplotlib.matlab import * def segplot(x, y, fmt, maxdelta, **kwargs): """ Plot x versus y, breaking the plot at any point where x[i] - x[i-1] > maxdelta. kwargs are passed on to plot """ x = asarray(x) y = asarray(y) d = diff(x) lines = [] ind = nonzero(greater(d, maxdelta)) ind = ind+1 if not len(ind): lines.extend( plot(x,y,fmt,**kwargs) ) else: allind = [0] allind.extend(ind) allind.append(len(x)) for i1,i2 in zip(allind[:-1], allind[1:]): lines.extend( plot(x[i1:i2], y[i1:i2], fmt, **kwargs) ) return lines t = [0,1,2,3,4,5,105,106,107,187, 200, 212, 300, 320] s = [1,4,5,3,9,11,-5,-8,3,12, 15, 12, -1, 3] segplot(t, s, 'b-o', 40, antialiased=False) grid(True) show() I'm inclined not to make this part of plot, since plot processes a variable number of arguments it makes it a little difficult. Certainly doable, but I'm hesitant to put too much on plot because it might become unwieldy. But a new function, like segment plot, would be easy enough to include. Any suggestions for a name, or additional functionality? JDH |