col = collections.LineCollection([spiral], offsets=xyo, transOffset=a.transData) # Note: the first argument to the collection initializer # must be a list of sequences of x,y tuples; we have only # one sequence, but we still have to put it in a list. a.add_collection(col, autolim=True) # autolim=True enables autoscaling. For collections with # offsets like this, it is neither efficient nor accurate, # but it is good enough to generate a plot that you can use # as a starting point. If you know beforehand the range of # x and y that you want to show, it is better to set them # explicitly, leave out the autolim kwarg (or set it to False), # and omit the 'a.autoscale_view()' call below. # Make a transform for the line segments such that their size is # given in points: trans = transforms.scale_transform(fig.dpi / transforms.Value(72.0), fig.dpi / transforms.Value(72.0)) col.set_transform(trans) # the points to pixels transform col.set_color(colors) a.autoscale_view() # See comment above, after a.add_collection. a.set_title("LineCollection using offsets") # The same data as above, but fill the curves. a = fig.add_subplot(2, 2, 2) col = collections.PolyCollection([spiral], offsets=xyo, transOffset=a.transData) a.add_collection(col, autolim=True) trans = transforms.scale_transform(fig.dpi / transforms.Value(72.0), fig.dpi / transforms.Value(72.0)) col.set_transform(trans) # the points to pixels transform
def plot_day_summary2(ax, opens, closes, highs, lows, ticksize=4, colorup='k', colordown='r', ): """ Represent the time, open, close, high, low as a vertical line ranging from low to high. The left tick is the open and the right tick is the close. ax : an Axes instance to plot to ticksize : size of open and close ticks in points colorup : the color of the lines where close >= open colordown : the color of the lines where close < open return value is a list of lines added """ # note this code assumes if any value open, close, low, high is # missing they all are missing rangeSegments = [ ((i, low), (i, high)) for i, low, high in zip(xrange(len(lows)), lows, highs) if low != -1 ] # the ticks will be from ticksize to 0 in points at the origin and # we'll translate these to the i, close location openSegments = [ ((-ticksize, 0), (0, 0)) ] # the ticks will be from 0 to ticksize in points at the origin and # we'll translate these to the i, close location closeSegments = [ ((0, 0), (ticksize, 0)) ] offsetsOpen = [ (i, open) for i, open in zip(xrange(len(opens)), opens) if open != -1 ] offsetsClose = [ (i, close) for i, close in zip(xrange(len(closes)), closes) if close != -1 ] scale = ax.figure.dpi * Value(1/72.0) tickTransform = scale_transform( scale, zero()) r,g,b = colorConverter.to_rgb(colorup) colorup = r,g,b,1 r,g,b = colorConverter.to_rgb(colordown) colordown = r,g,b,1 colord = { True : colorup, False : colordown, } colors = [colord[open>=close] for open, close in zip(opens, closes) if open!=-1 and close !=-1] assert(len(rangeSegments)==len(offsetsOpen)) assert(len(offsetsOpen)==len(offsetsClose)) assert(len(offsetsClose)==len(colors)) useAA = 0, # use tuple here lw = 1, # and here rangeCollection = LineCollection(rangeSegments, colors = colors, linewidths = lw, antialiaseds = useAA, ) openCollection = LineCollection(openSegments, colors = colors, antialiaseds = useAA, linewidths = lw, offsets = offsetsOpen, transOffset = ax.transData, ) openCollection.set_transform(tickTransform) closeCollection = LineCollection(closeSegments, colors = colors, antialiaseds = useAA, linewidths = lw, offsets = offsetsClose, transOffset = ax.transData, ) closeCollection.set_transform(tickTransform) minx, maxx = (0, len(rangeSegments)) miny = min([low for low in lows if low !=-1]) maxy = max([high for high in highs if high != -1]) corners = (minx, miny), (maxx, maxy) ax.update_datalim(corners) ax.autoscale_view() # add these last ax.add_collection(rangeCollection) ax.add_collection(openCollection) ax.add_collection(closeCollection) return rangeCollection, openCollection, closeCollection
def draw2(): import pylab as P from matplotlib import collections, axes, transforms from matplotlib.colors import colorConverter import matplotlib.numerix as N nverts = 50 npts = 100 # Make some spirals r = N.array(range(nverts)) theta = N.array(range(nverts)) * (2*N.pi)/(nverts-1) xx = r * N.sin(theta) yy = r * N.cos(theta) spiral = zip(xx,yy) # Make some offsets xo = P.randn(npts) yo = P.randn(npts) xyo = zip(xo, yo) # Make a list of colors cycling through the rgbcmyk series. colors = [colorConverter.to_rgba(c) for c in ('r','g','b','c','y','m','k')] fig = P.figure() fig.canvas.mpl_connect('pick_event', on_canvas_pick) a = fig.add_subplot(2,1,1) col = collections.LineCollection([spiral], offsets=xyo, transOffset=a.transData, picker=True) # Note: the first argument to the collection initializer # must be a list of sequences of x,y tuples; we have only # one sequence, but we still have to put it in a list. a.add_collection(col, autolim=True) # autolim=True enables autoscaling. For collections with # offsets like this, it is neither efficient nor accurate, # but it is good enough to generate a plot that you can use # as a starting point. If you know beforehand the range of # x and y that you want to show, it is better to set them # explicitly, leave out the autolim kwarg (or set it to False), # and omit the 'a.autoscale_view()' call below. # Make a transform for the line segments such that their size is # given in points: trans = transforms.scale_transform(fig.dpi/transforms.Value(72.), fig.dpi/transforms.Value(72.)) col.set_transform(trans) # the points to pixels transform col.set_color(colors) a.autoscale_view() # See comment above, after a.add_collection. a.set_title('LineCollection using offsets') # The same data as above, but fill the curves. a = fig.add_subplot(2,1,2) col = collections.PolyCollection([spiral], offsets=xyo, transOffset=a.transData) col._picker =True a.add_collection(col, autolim=True) trans = transforms.scale_transform(fig.dpi/transforms.Value(72.), fig.dpi/transforms.Value(72.)) col.set_transform(trans) # the points to pixels transform col.set_color(colors) a.autoscale_view() a.set_title('PolyCollection using offsets') P.show()
def plot_day_summary2(ax, opens, closes, highs, lows, ticksize=4, colorup='k', colordown='r', ): """ Represent the time, open, close, high, low as a vertical line ranging from low to high. The left tick is the open and the right tick is the close. ax : an Axes instance to plot to ticksize : size of open and close ticks in points colorup : the color of the lines where close >= open colordown : the color of the lines where close < open return value is a list of lines added """ # note this code assumes if any value open, close, low, high is # missing they all are missing rangeSegments = [ ((i, low), (i, high)) for i, low, high in zip(xrange(len(lows)), lows, highs) if low != -1 ] # the ticks will be from ticksize to 0 in points at the origin and # we'll translate these to the i, close location openSegments = [ ((-ticksize, 0), (0, 0)) ] # the ticks will be from 0 to ticksize in points at the origin and # we'll translate these to the i, close location closeSegments = [ ((0, 0), (ticksize, 0)) ] offsetsOpen = [ (i, open) for i, open in zip(xrange(len(opens)), opens) if open != -1 ] offsetsClose = [ (i, close) for i, close in zip(xrange(len(closes)), closes) if close != -1 ] scale = ax.figure.dpi * Value(1/72.0) tickTransform = scale_transform( scale, zero()) r,g,b = colorConverter.to_rgb(colorup) colorup = r,g,b,1 r,g,b = colorConverter.to_rgb(colordown) colordown = r,g,b,1 colord = { True : colorup, False : colordown, } colors = [colord[open<close] for open, close in zip(opens, closes) if open!=-1 and close !=-1] assert(len(rangeSegments)==len(offsetsOpen)) assert(len(offsetsOpen)==len(offsetsClose)) assert(len(offsetsClose)==len(colors)) useAA = 0, # use tuple here lw = 1, # and here rangeCollection = LineCollection(rangeSegments, colors = colors, linewidths = lw, antialiaseds = useAA, ) openCollection = LineCollection(openSegments, colors = colors, antialiaseds = useAA, linewidths = lw, offsets = offsetsOpen, transOffset = ax.transData, ) openCollection.set_transform(tickTransform) closeCollection = LineCollection(closeSegments, colors = colors, antialiaseds = useAA, linewidths = lw, offsets = offsetsClose, transOffset = ax.transData, ) closeCollection.set_transform(tickTransform) minx, maxx = (0, len(rangeSegments)) miny = min([low for low in lows if low !=-1]) maxy = max([high for high in highs if high != -1]) corners = (minx, miny), (maxx, maxy) ax.update_datalim(corners) ax.autoscale_view() # add these last ax.add_collection(rangeCollection) ax.add_collection(openCollection) ax.add_collection(closeCollection) return rangeCollection, openCollection, closeCollection
transOffset=a.transData) # Note: the first argument to the collection initializer # must be a list of sequences of x,y tuples; we have only # one sequence, but we still have to put it in a list. a.add_collection(col, autolim=True) # autolim=True enables autoscaling. For collections with # offsets like this, it is neither efficient nor accurate, # but it is good enough to generate a plot that you can use # as a starting point. If you know beforehand the range of # x and y that you want to show, it is better to set them # explicitly, leave out the autolim kwarg (or set it to False), # and omit the 'a.autoscale_view()' call below. # Make a transform for the line segments such that their size is # given in points: trans = transforms.scale_transform(fig.dpi / transforms.Value(72.), fig.dpi / transforms.Value(72.)) col.set_transform(trans) # the points to pixels transform col.set_color(colors) a.autoscale_view() # See comment above, after a.add_collection. a.set_title('LineCollection using offsets') # The same data as above, but fill the curves. a = fig.add_subplot(2, 2, 2) col = collections.PolyCollection([spiral], offsets=xyo, transOffset=a.transData) a.add_collection(col, autolim=True) trans = transforms.scale_transform(fig.dpi / transforms.Value(72.),