def plotBox(self, name, data, data_range=None): box = Frame(self, border=2, relief=SUNKEN) box.pack(side=TOP, fill=BOTH, expand=YES) frame = Frame(box, background='grey') frame.pack(side=TOP, fill=X, expand=NO) Label(frame, text=string.capitalize(string.join( string.split(name , '_'), ' ')), background='grey').pack(side=LEFT) if data_range is None: min = Numeric.minimum.reduce(data[:,1]) max = Numeric.maximum.reduce(data[:,1]) min, max = plotRange(min, max) else: min, max = data_range plot_objects = [] plot_data = data time = plot_data[:,0] jumps = Numeric.repeat(Numeric.arange(len(time)-1), Numeric.less(time[1:], time[:-1]))+1 for i in self.master.restarts: plot_objects.append(PolyLine([(self.time[i], min), (self.time[i], max)], color='black', stipple='gray25')) plot_objects.insert(0, PolyLine(plot_data, color = 'red')) plot = PlotCanvas(box, 400, 100, zoom=1, select=self.master._selectRange) plot.pack(side=LEFT, fill=BOTH, expand=YES) plot.draw(PlotGraphics(plot_objects), 'automatic', (min, max)) plot.bind('<Double-Button-1>', lambda event, d=plot_data: externalPlot(d)) self.registerPlot(plot) self.setSelection(plot)
class ModeViewer2D(Toplevel): def __init__(self, master, mode_projector): Toplevel.__init__(self, master) self.title("2D Mode Projection") self.mode_projector = mode_projector frame = Frame(self) frame.pack(side=TOP, fill=BOTH, expand=YES) self.xnum = IntEntry(frame, "X mode number: ", 6, 6, mode_projector.numberOfModes()-1) self.xnum.pack(side=LEFT) self.xnum.bind('<Return>', self.draw) self.ynum = IntEntry(frame, " Y mode number: ", 7, 6, mode_projector.numberOfModes()-1) self.ynum.pack(side=LEFT) self.ynum.bind('<Return>', self.draw) self.plot = PlotCanvas(self, 400, 400) self.plot.pack(side=TOP, fill=BOTH, expand=YES) self.draw() def draw(self, event=None): xnum = self.xnum.get() ynum = self.ynum.get() self.mode_projector.calculateProjections([xnum, ynum]) data = self.mode_projector[ynum] data[:, 0] = self.mode_projector[xnum][:, 1] self.plot.clear() self.plot.draw(PolyLine(data, color = 'red'))
class ModeViewer2D(Toplevel): def __init__(self, master, mode_projector): Toplevel.__init__(self, master) self.title("2D Mode Projection") self.mode_projector = mode_projector frame = Frame(self) frame.pack(side=TOP, fill=BOTH, expand=YES) self.xnum = IntEntry(frame, "X mode number: ", 6, 6, mode_projector.numberOfModes() - 1) self.xnum.pack(side=LEFT) self.xnum.bind('<Return>', self.draw) self.ynum = IntEntry(frame, " Y mode number: ", 7, 6, mode_projector.numberOfModes() - 1) self.ynum.pack(side=LEFT) self.ynum.bind('<Return>', self.draw) self.plot = PlotCanvas(self, 400, 400) self.plot.pack(side=TOP, fill=BOTH, expand=YES) self.draw() def draw(self, event=None): xnum = self.xnum.get() ynum = self.ynum.get() self.mode_projector.calculateProjections([xnum, ynum]) data = self.mode_projector[ynum] data[:, 0] = self.mode_projector[xnum][:, 1] self.plot.clear() self.plot.draw(PolyLine(data, color='red'))
def __init__(self, master, rb_projector): PlotWindow.__init__(self, master, "Rigid-body motion projections") plot_range = 0. for i in range(6): series = rb_projector[i][:, 1] lower = Numeric.minimum.reduce(series) upper = Numeric.maximum.reduce(series) lower, upper = plotRange(lower, upper) plot_range = max(plot_range, upper - lower) for column, offset, type in [(0, 0, "Translation "), (1, 3, "Rotation ")]: for i in range(3): data = rb_projector[i + offset] box = Frame(self, border=2, relief=SUNKEN) box.grid(row=i, column=column, sticky=N + W + E + S) frame = Frame(box, background='grey') frame.pack(side=TOP, fill=X, expand=NO) Label(frame, text=type + ["X", "Y", "Z"][i], background='grey').pack(side=LEFT) minv = Numeric.minimum.reduce(data[:, 1]) maxv = Numeric.maximum.reduce(data[:, 1]) minv, maxv = plotRange(minv, maxv) middle = 0.5 * (maxv + minv) if maxv - minv < plot_range: minv = middle - 0.5 * plot_range maxv = middle + 0.5 * plot_range plot_objects = [PolyLine(data, color='red')] plot = PlotCanvas(box, 400, 100, zoom=1, select=self.master._selectRange) plot.pack(side=LEFT, fill=BOTH, expand=YES) plot.draw(PlotGraphics(plot_objects), 'automatic', (minv, maxv)) plot.bind('<Double-Button-1>', lambda event, d=data: externalPlot(d)) self.registerPlot(plot) self.setSelection(plot)
def __init__(self, master, rb_projector): PlotWindow.__init__(self, master, "Rigid-body motion projections") plot_range = 0. for i in range(6): series = rb_projector[i][:, 1] lower = Numeric.minimum.reduce(series) upper = Numeric.maximum.reduce(series) lower, upper = plotRange(lower, upper) plot_range = max(plot_range, upper-lower) for column, offset, type in [(0, 0, "Translation "), (1, 3, "Rotation ")]: for i in range(3): data = rb_projector[i+offset] box = Frame(self, border=2, relief=SUNKEN) box.grid(row=i, column=column, sticky=N+W+E+S) frame = Frame(box, background='grey') frame.pack(side=TOP, fill=X, expand=NO) Label(frame, text=type + ["X", "Y", "Z"][i], background='grey').pack(side=LEFT) minv = Numeric.minimum.reduce(data[:,1]) maxv = Numeric.maximum.reduce(data[:,1]) minv, maxv = plotRange(minv, maxv) middle = 0.5*(maxv+minv) if maxv-minv < plot_range: minv = middle-0.5*plot_range maxv = middle+0.5*plot_range plot_objects = [PolyLine(data, color = 'red')] plot = PlotCanvas(box, 400, 100, zoom=1, select=self.master._selectRange) plot.pack(side=LEFT, fill=BOTH, expand=YES) plot.draw(PlotGraphics(plot_objects), 'automatic', (minv, maxv)) plot.bind('<Double-Button-1>', lambda event, d=data: externalPlot(d)) self.registerPlot(plot) self.setSelection(plot)
def plotBox(self, name, data, data_range=None): box = Frame(self, border=2, relief=SUNKEN) box.pack(side=TOP, fill=BOTH, expand=YES) frame = Frame(box, background='grey') frame.pack(side=TOP, fill=X, expand=NO) Label(frame, text=string.capitalize(string.join(string.split(name, '_'), ' ')), background='grey').pack(side=LEFT) if data_range is None: min = Numeric.minimum.reduce(data[:, 1]) max = Numeric.maximum.reduce(data[:, 1]) min, max = plotRange(min, max) else: min, max = data_range plot_objects = [] plot_data = data time = plot_data[:, 0] jumps = Numeric.repeat(Numeric.arange(len(time) - 1), Numeric.less(time[1:], time[:-1])) + 1 for i in self.master.restarts: plot_objects.append( PolyLine([(self.time[i], min), (self.time[i], max)], color='black', stipple='gray25')) plot_objects.insert(0, PolyLine(plot_data, color='red')) plot = PlotCanvas(box, 400, 100, zoom=1, select=self.master._selectRange) plot.pack(side=LEFT, fill=BOTH, expand=YES) plot.draw(PlotGraphics(plot_objects), 'automatic', (min, max)) plot.bind('<Double-Button-1>', lambda event, d=plot_data: externalPlot(d)) self.registerPlot(plot) self.setSelection(plot)
class TkPlotCanvasDemo1: def __init__(self, parent): self.master = parent self.ncurves = 3 # number of curves self.npoints = 20 # number of points on each curve # here we store data in plain NumPy arrays self.vector_x = zeros(self.npoints) # use a list of y vectors(0:self.ncurves-1) self.vector_y = [zeros(self.npoints) \ for y in range(self.ncurves)] self.fill_vectors() # fill the vectors with data for testing # make graph widget: self.g = PlotCanvas(self.master, 500, 300, zoom=True, relief='sunken', border=2) self.g.pack(expand=True, fill='both') # define a list of colors for the various curves: colors = ['red', 'yellow', 'blue', 'green', 'black', 'grey'] # plot each curve: # the x coordinates are in self.vector_x # the y coordinates are in self.vector_y[i] self.curves = [] for i in range(self.ncurves): xy_pairs = array([self.vector_x, self.vector_y[i]]).transpose() c = PolyLine(xy_pairs, width=1 + i, color=colors[i]) self.curves.append(c) object = PlotGraphics(self.curves) self.g.draw(object, xaxis='automatic', yaxis='automatic') self.buttons = Pmw.ButtonBox(self.master, labelpos='n', label_text='Options:') self.buttons.pack(fill='both', expand=True, padx=10, pady=10) # add buttons: self.buttons.add('Move points', command=self.animate) self.buttons.add('Postscript', command=self.postscript) self.buttons.add('Symbols', command=self.symbols) self.buttons.add('Quit', command=self.master.quit) self.buttons.alignbuttons() # nice loook... def symbols(self): """Turn on symbols (triangles) at all points.""" curves_wsymbol = [PolyMarker(curve.points, color='blue', marker='triangle') \ for curve in self.curves] self.g.draw(PlotGraphics(curves_wsymbol)) # plot collection def fill_vectors(self): """Fill NumPy vectors with (random) values.""" # use random numbers for generating plot data: random.seed(9) # fix the seed for testing for index in range(self.npoints): self.vector_x[index] = index # x coordinates for y in range(self.ncurves): self.vector_y[y][index] = random.uniform(0, 8) def animate(self, delay=100): """Adjust curves randomly, in an animated fashion.""" self.g.clear() # erase all plot data for curve in self.curves: p = curve.points # [[x1,y1],[x,2,y2],...] (NumPy) for index in range(p.shape[0]): p[index][1] = random.uniform(0, 8) self.g.draw(curve, xaxis='automatic', yaxis=(0, 8)) # plot PolyLine self.master.after(delay) self.master.update() def postscript(self): """Generate a hardcopy of the plot in PostScript.""" self.g.canvas.postscript(file='tmp2.ps')
class TkPlotCanvasDemo1: def __init__(self, parent): self.master = parent self.ncurves = 3 # number of curves self.npoints = 20 # number of points on each curve # here we store data in plain NumPy arrays self.vector_x = zeros(self.npoints) # use a list of y vectors(0:self.ncurves-1) self.vector_y = [zeros(self.npoints) \ for y in range(self.ncurves)] self.fill_vectors() # fill the vectors with data for testing # make graph widget: self.g = PlotCanvas(self.master, 500, 300, zoom=True, relief='sunken', border=2) self.g.pack(expand=True, fill='both') # define a list of colors for the various curves: colors = ['red','yellow','blue','green','black','grey'] # plot each curve: # the x coordinates are in self.vector_x # the y coordinates are in self.vector_y[i] self.curves = [] for i in range(self.ncurves): xy_pairs = array([self.vector_x,self.vector_y[i]]).transpose() c = PolyLine(xy_pairs, width=1+i, color=colors[i]) self.curves.append(c) object = PlotGraphics(self.curves) self.g.draw(object, xaxis='automatic', yaxis='automatic') self.buttons = Pmw.ButtonBox(self.master, labelpos='n', label_text='Options:') self.buttons.pack(fill='both', expand=True, padx=10, pady=10) # add buttons: self.buttons.add('Move points',command=self.animate) self.buttons.add('Postscript', command=self.postscript) self.buttons.add('Symbols', command=self.symbols) self.buttons.add('Quit', command=self.master.quit) self.buttons.alignbuttons() # nice loook... def symbols(self): """Turn on symbols (triangles) at all points.""" curves_wsymbol = [PolyMarker(curve.points, color='blue', marker='triangle') \ for curve in self.curves] self.g.draw(PlotGraphics(curves_wsymbol)) # plot collection def fill_vectors(self): """Fill NumPy vectors with (random) values.""" # use random numbers for generating plot data: random.seed(9) # fix the seed for testing for index in range(self.npoints): self.vector_x[index] = index # x coordinates for y in range(self.ncurves): self.vector_y[y][index] = random.uniform(0,8) def animate(self,delay=100): """Adjust curves randomly, in an animated fashion.""" self.g.clear() # erase all plot data for curve in self.curves: p = curve.points # [[x1,y1],[x,2,y2],...] (NumPy) for index in range(p.shape[0]): p[index][1] = random.uniform(0,8) self.g.draw(curve,xaxis='automatic',yaxis=(0,8)) # plot PolyLine self.master.after(delay) self.master.update() def postscript(self): """Generate a hardcopy of the plot in PostScript.""" self.g.canvas.postscript(file='tmp2.ps')
def __init__(self, master, mode_projector, indices): from Scientific.Statistics import mean, standardDeviation title = "Normal mode projections (Temperature: %5.1f K)" \ % mode_projector.temperature PlotWindow.__init__(self, master, title) self.mode_projector = mode_projector plot_range = 0. for i in indices: series = mode_projector[i][:, 1] average = mean(series) deviation = standardDeviation(series) lower = Numeric.minimum.reduce(series) lower = min(lower, average-deviation) upper = Numeric.maximum.reduce(series) upper = max(upper, average+deviation) lower, upper = plotRange(lower, upper) plot_range = max(plot_range, upper-lower) nmodes = mode_projector.numberOfModes() for i in range(len(indices)): if indices[i] < 0: indices[i] = nmodes+indices[i] next_indices = [] step = indices[1] - indices[0] i = indices[-1] while len(next_indices) < 4: i = i + step if i >= nmodes: break next_indices.append(i) previous_indices = [] i = indices[0] while len(previous_indices) < 4: i = i - step if i < 6: break previous_indices.insert(0, i) if next_indices or previous_indices: frame = Frame(self) frame.pack(side=BOTTOM, fill=X, expand=YES) if previous_indices: Button(frame, text="Previous", command=lambda s=self, pi=previous_indices: s.modeProjection(pi)).pack(side=LEFT) if next_indices: Button(frame, text="Next", command=lambda s=self, ni=next_indices: s.modeProjection(ni)).pack(side=RIGHT) for i in range(len(indices)): number = indices[i] data = mode_projector[number] fluctuation = self.mode_projector.amplitude(number) average = mean(data[:, 1]) deviation = standardDeviation(data[:, 1]) box = Frame(self, border=2, relief=SUNKEN) box.pack(side=TOP, fill=BOTH, expand=YES) frame = Frame(box, background='grey') frame.pack(side=TOP, fill=X, expand=NO) Label(frame, text="Mode %d" % number, background='grey').pack(side=LEFT) Button(frame, text="Animation", background='grey', command=lambda s=self, n=number: s.animateMode(n, 1.)).pack(side=RIGHT) Button(frame, text="View", background='grey', command=lambda s=self, n=number: s.viewMode(n)).pack(side=RIGHT) minv = Numeric.minimum.reduce(data[:,1]) maxv = Numeric.maximum.reduce(data[:,1]) minv, maxv = plotRange(minv, maxv) middle = 0.5*(maxv+minv) if maxv-minv < plot_range: minv = middle-0.5*plot_range maxv = middle+0.5*plot_range plot_objects = [PolyLine([(data[1, 0], average-fluctuation), (data[1, 0], average+fluctuation)], color='blue', width=3), PolyLine([(data[-1, 0], average-deviation), (data[-1, 0], average+deviation)], color='green', width=3)] plot_objects.insert(0, PolyLine(data, color = 'red')) plot = PlotCanvas(box, 400, 100, zoom=1, select=self.master._selectRange) plot.pack(side=LEFT, fill=BOTH, expand=YES) plot.draw(PlotGraphics(plot_objects), 'automatic', (minv, maxv)) plot.bind('<Double-Button-1>', lambda event, d=data: externalPlot(d)) self.registerPlot(plot) self.setSelection(plot)
def __init__(self, master, mode_projector, indices): from Scientific.Statistics import mean, standardDeviation title = "Normal mode projections (Temperature: %5.1f K)" \ % mode_projector.temperature PlotWindow.__init__(self, master, title) self.mode_projector = mode_projector plot_range = 0. for i in indices: series = mode_projector[i][:, 1] average = mean(series) deviation = standardDeviation(series) lower = Numeric.minimum.reduce(series) lower = min(lower, average - deviation) upper = Numeric.maximum.reduce(series) upper = max(upper, average + deviation) lower, upper = plotRange(lower, upper) plot_range = max(plot_range, upper - lower) nmodes = mode_projector.numberOfModes() for i in range(len(indices)): if indices[i] < 0: indices[i] = nmodes + indices[i] next_indices = [] step = indices[1] - indices[0] i = indices[-1] while len(next_indices) < 4: i = i + step if i >= nmodes: break next_indices.append(i) previous_indices = [] i = indices[0] while len(previous_indices) < 4: i = i - step if i < 6: break previous_indices.insert(0, i) if next_indices or previous_indices: frame = Frame(self) frame.pack(side=BOTTOM, fill=X, expand=YES) if previous_indices: Button(frame, text="Previous", command=lambda s=self, pi=previous_indices: s. modeProjection(pi)).pack(side=LEFT) if next_indices: Button(frame, text="Next", command=lambda s=self, ni=next_indices: s. modeProjection(ni)).pack(side=RIGHT) for i in range(len(indices)): number = indices[i] data = mode_projector[number] fluctuation = self.mode_projector.amplitude(number) average = mean(data[:, 1]) deviation = standardDeviation(data[:, 1]) box = Frame(self, border=2, relief=SUNKEN) box.pack(side=TOP, fill=BOTH, expand=YES) frame = Frame(box, background='grey') frame.pack(side=TOP, fill=X, expand=NO) Label(frame, text="Mode %d" % number, background='grey').pack(side=LEFT) Button(frame, text="Animation", background='grey', command=lambda s=self, n=number: s.animateMode(n, 1.)).pack( side=RIGHT) Button(frame, text="View", background='grey', command=lambda s=self, n=number: s.viewMode(n)).pack( side=RIGHT) minv = Numeric.minimum.reduce(data[:, 1]) maxv = Numeric.maximum.reduce(data[:, 1]) minv, maxv = plotRange(minv, maxv) middle = 0.5 * (maxv + minv) if maxv - minv < plot_range: minv = middle - 0.5 * plot_range maxv = middle + 0.5 * plot_range plot_objects = [ PolyLine([(data[1, 0], average - fluctuation), (data[1, 0], average + fluctuation)], color='blue', width=3), PolyLine([(data[-1, 0], average - deviation), (data[-1, 0], average + deviation)], color='green', width=3) ] plot_objects.insert(0, PolyLine(data, color='red')) plot = PlotCanvas(box, 400, 100, zoom=1, select=self.master._selectRange) plot.pack(side=LEFT, fill=BOTH, expand=YES) plot.draw(PlotGraphics(plot_objects), 'automatic', (minv, maxv)) plot.bind('<Double-Button-1>', lambda event, d=data: externalPlot(d)) self.registerPlot(plot) self.setSelection(plot)
class ScatteringFunctionPlot(Frame): def __init__(self, master, filename): Frame.__init__(self, master) file = NetCDFFile(filename) self.q = file.variables['q'][1:] self.t = file.variables['time'][:] self.fn = file.variables['sf'][1:, :] Label(self, text='S(t) for various q').pack(side=TOP, fill=X) self.plot1 = PlotCanvas(self, 600, 250, zoom=1) self.plot1.pack(side=TOP, fill=BOTH, expand=YES) Label(self, text='S(q) for various t').pack(side=TOP, fill=X) self.plot2 = PlotCanvas(self, 600, 250, zoom=1) self.plot2.pack(side=TOP, fill=BOTH, expand=YES) frame = Frame(self) frame.pack(side=TOP, fill=X) self.first_q = IntEntry(frame, "q range: from ", 0, 0, len(self.q)) self.first_q.grid(row=0, column=0) self.first_q.bind('<Return>', self.draw) self.last_q = IntEntry(frame, " to ", len(self.q), 0, len(self.q)) self.last_q.grid(row=0, column=1) self.skip_q = IntEntry(frame, " skip ", (len(self.q) + 10) / 10, 1, len(self.q)) self.skip_q.grid(row=0, column=2) self.first_t = IntEntry(frame, "t range: from ", 0, 0, len(self.t)) self.first_t.grid(row=1, column=0) self.last_t = IntEntry(frame, " to ", len(self.t), 0, len(self.t)) self.last_t.grid(row=1, column=1) self.skip_t = IntEntry(frame, " skip ", (len(self.t) + 10) / 10, 1, len(self.t)) self.skip_t.grid(row=1, column=2) self.first_q.bind('<Return>', self.draw) self.last_q.bind('<Return>', self.draw) self.skip_q.bind('<Return>', self.draw) self.first_t.bind('<Return>', self.draw) self.last_t.bind('<Return>', self.draw) self.skip_t.bind('<Return>', self.draw) self.draw() def draw(self, event=None): try: first_q = self.first_q.get() last_q = self.last_q.get() skip_q = self.skip_q.get() first_t = self.first_t.get() last_t = self.last_t.get() skip_t = self.skip_t.get() except ValueError: return graphics1, graphics2 = self.plot_objects(first_q, last_q, skip_q, first_t, last_t, skip_t) self.plot1.clear() self.plot1.draw(PlotGraphics(graphics1), 'automatic', 'automatic') self.plot2.clear() self.plot2.draw(PlotGraphics(graphics2), 'automatic', 'automatic') def plot_objects(self, first_q, last_q, skip_q, first_t, last_t, skip_t): plot_objects1 = [] markers1 = [] color_index = 0 dt = (last_t - first_t + 150) / 150 for i in range(first_q, last_q, skip_q): data = self.fn[i, first_t:last_t:dt] data_t = self.t[first_t:last_t:dt] points = N.transpose(N.array([data_t, data])) plot_objects1.append( PolyLine(points, color=self.colors[color_index])) markers1.append(self.q[i], color_index) color_index = (color_index + 1) % len(self.colors) plot_objects2 = [] markers2 = [] color_index = 0 dq = (last_q - first_q + 150) / 150 for i in range(first_t, last_t, skip_t): data = self.fn[first_q:last_q:dq, i] data_q = self.q[first_q:last_q:dq] points = N.transpose(N.array([data_q, data])) plot_objects2.append( PolyLine(points, color=self.colors[color_index])) markers2.append(self.t[i], color_index) color_index = (color_index + 1) % len(self.colors) for q, color_index in markers1: plot_objects2.append( VerticalLine(q, color=self.colors[color_index], stipple='gray50')) for t, color_index in markers2: plot_objects1.append( VerticalLine(t, color=self.colors[color_index], stipple='gray50')) return plot_objects1, plot_objects2 colors = [ 'red', 'green', 'blue', 'orange', 'brown', 'yellow', 'violet', 'pink', 'cyan', 'grey' ]