class SinCos(object): funcNames = ('sin', 'cos') filePath = "sc.png" def __init__(self): self.X = np.linspace(0, 4 * np.pi, 200) self.pt = Plotter(1, 2, width=700, height=500, useAgg=True) self.pt.set_xlabel("X") self.pt.use_grid() self.pt.add_annotation(0, "First") self.pt.add_annotation(199, "Last") def __call__(self, frequency): self.pt.set_title("Sin and Cosine: Frequency = {:.2f}x", frequency) with self.pt as p: for funcName in self.funcNames: Y = getattr(np, funcName)(frequency * self.X) p.set_ylabel("{}(X)".format(funcName)) p(self.X, Y) with open(self.filePath, "wb") as fh: self.pt.show(fh=fh)
# Construct a Plotter object for a 700x500 pixel (100 DPI) figure with # two subplots. pt = Plotter(1, 2, width=7.0, height=5.0) # The plots, one in each subplot, will be of a sine and cosine with # 200 points from 0 to 4*pi. funcNames = ('sin', 'cos') X = np.linspace(0, 4 * np.pi, 200) pt.set_title("Sine and Cosine") # Each subplot will have an x-axis label of "X" and a grid. pt.set_xlabel("X") pt.use_grid() # Each plot will have an annotation labeled "Last" at its last # point. Note that we can use negative indices referenced to the last # element, just as with Python sequences. pt.add_annotation(-1, "Last") # Make a subplotting context and work with the two subplots via the # subplot tool sp. It's actually just a reference to pt, but set up # for subplotting, with a context for a new subplot each time it's # called. with pt as sp: # Do each plot, sin and then cos. for k, funcName in enumerate(funcNames): if k == 0: # The major ticks are at pi/2 intervals, but only for the # top subplot to demonstrate that it can be different pt.set_tickSpacing('x', np.pi / 2) # Generate the 1-D Numpy array for this plot's y-axis. Y = getattr(np, funcName)(X) # The sin plot will have a dashed line instead of the default