def plot_nmean(self, fname=None): if fname: from matplotlib.backends.backend_agg import Figure, FigureCanvasAgg f = Figure() c = FigureCanvasAgg(f) else: from matplotlib import pyplot f = pyplot.gcf() ax_vi = f.add_subplot(111) ax_vi.set_xscale('log') ax_n = ax_vi.twinx() l2 = ax_vi.plot(self.gamma, self.VI, color='blue') l3 = ax_vi.plot(self.gamma, self.In, '--', color='red') l = ax_n.plot(self.gamma, self.n_mean, ':', c='black') ax_n.legend((l2, l3, l), ("$VI$", "$I_n$", "$\langle n \\rangle$"), loc=0) ax_n.set_ylabel('$\langle n \\rangle$') ax_n.set_yscale('log') # ax_n.set_ylim(bottom=0) ax_vi.set_ylabel('$VI$ and $I_n$') ax_vi.set_xlabel('$\gamma$') if fname: c.print_figure(fname, bbox_inches='tight')
def plot_nmean(self, fname=None): if fname: from matplotlib.backends.backend_agg import Figure, FigureCanvasAgg f = Figure() c = FigureCanvasAgg(f) else: from matplotlib import pyplot f = pyplot.gcf() ax_vi = f.add_subplot(111) ax_vi.set_xscale("log") ax_n = ax_vi.twinx() l2 = ax_vi.plot(self.gamma, self.VI, color="blue") l3 = ax_vi.plot(self.gamma, self.In, "--", color="red") l = ax_n.plot(self.gamma, self.n_mean, ":", c="black") ax_n.legend((l2, l3, l), ("$VI$", "$I_n$", "$\langle n \\rangle$"), loc=0) ax_n.set_ylabel("$\langle n \\rangle$") ax_n.set_yscale("log") # ax_n.set_ylim(bottom=0) ax_vi.set_ylabel("$VI$ and $I_n$") ax_vi.set_xlabel("$\gamma$") if fname: c.print_figure(fname, bbox_inches="tight")
def nosql_draw2(check_func, match): from matplotlib.backends.backend_agg import FigureCanvas try: from matplotlib.backends.backend_agg import Figure except ImportError: from matplotlib.figure import Figure was_interactive = mpl.is_interactive() if was_interactive: mpl.interactive(False) # fnum = 32 fig = Figure() canvas = FigureCanvas(fig) # NOQA # fig.clf() ax = fig.add_subplot(1, 1, 1) if check_func is not None and check_func(): return ax, xywh1, xywh2 = match.show(ax=ax) if check_func is not None and check_func(): return savekw = { # 'dpi' : 60, 'dpi': 80, } axes_extents = pt.extract_axes_extents(fig) # assert len(axes_extents) == 1, 'more than one axes' extent = axes_extents[0] with io.BytesIO() as stream: # This call takes 23% - 15% of the time depending on settings fig.savefig(stream, bbox_inches=extent, **savekw) stream.seek(0) data = np.fromstring(stream.getvalue(), dtype=np.uint8) if check_func is not None and check_func(): return pt.plt.close(fig) image = cv2.imdecode(data, 1) thumbsize = 221 max_dsize = (thumbsize, thumbsize) dsize, sx, sy = vt.resized_clamped_thumb_dims( vt.get_size(image), max_dsize) if check_func is not None and check_func(): return image = vt.resize(image, dsize) vt.imwrite(fpath, image) if check_func is not None and check_func(): return
def plot_nhists(self, fname): from matplotlib.backends.backend_agg import Figure, FigureCanvasAgg from matplotlib.backends.backend_pdf import PdfPages f = Figure() c = FigureCanvasAgg(f) ax = f.add_subplot(111) pp = PdfPages(fname) for i in range(len(self.n_hist)): histdata = self.n_hist[i] histedges = self.n_hist_edges[i] ax.cla() ax.set_title("$\gamma=%f$" % self.gamma[i]) ax.bar(histedges[:-1], histdata, width=(histedges[1:] - histedges[:-1])) ax.set_xscale("log") ax.set_xlim(0.1, self.Nnodes) ax.set_ylim(0, 1) pp.savefig(f) pp.close()
def plot_nhists(self, fname): from matplotlib.backends.backend_agg import Figure, FigureCanvasAgg from matplotlib.backends.backend_pdf import PdfPages f = Figure() c = FigureCanvasAgg(f) ax = f.add_subplot(111) pp = PdfPages(fname) for i in range(len(self.n_hist)): histdata = self.n_hist[i] histedges = self.n_hist_edges[i] ax.cla() ax.set_title("$\gamma=%f$" % self.gamma[i]) ax.bar(histedges[:-1], histdata, width=(histedges[1:] - histedges[:-1])) ax.set_xscale('log') ax.set_xlim(0.1, self.Nnodes) ax.set_ylim(0, 1) pp.savefig(f) pp.close()
def write_header(self, resolution=72): from matplotlib.backends.backend_agg import RendererAgg, Figure from matplotlib.backend_bases import GraphicsContextBase try: from matplotlib.transforms import Value except ImportError: dpi = resolution else: dpi = Value(resolution) self.renderer = RendererAgg(self.w, self.h, dpi) self.figure = Figure() self.gc = GraphicsContextBase() self.gc.set_linewidth(.2)
def make_plot(ts, dat, ylabel='Data', ylog=False, enums=None, ts2=None, dat2=None, y2label=None, y2log=False, enums2=None, tmin=None, tmax=None, fname=None, ymin=None, ymax=None, y2min=None, y2max=None): fig = Figure(figsize=(8., 5.0), dpi=300) gspec = GridSpec(1, 1) canvas = FigureCanvas(fig) axes = fig.add_subplot(gspec[0], facecolor='#FFFFFF') if ylog: # and False: axes.set_yscale('log', basey=10) pos = np.where(dat>0) ts = ts[pos] dat = dat[pos] tvals = [ts2date(t) for t in ts] if enums is not None: pad = min(0.8, 0.1*len(enums)) axes.set_ylim(-pad, len(enums)-1+pad) axes.set_yticks(range(len(enums))) axes.set_yticklabels(enums) try: axes.get_yaxis().get_major_formatter().set_useOffset(False) except: pass if tmin is not None and tmax is not None: axes.set_xlim((ts2date(tmin), ts2date(tmax))) if ymin is not None or ymax is not None: if ymin is not None: ymin = float(ymin) else: ymin = min(dat) if ymax is not None: ymax = float(ymax) else: ymax = max(dat) axes.set_ylim((ymin, ymax)) axes.set_xlabel('Date', fontproperties=mplfont) axes.set_ylabel(ylabel, color='b', fontproperties=mplfont) axes.xaxis.set_major_formatter(DateFormatter("%H:%M\n%b-%d")) axes.plot(tvals, dat, color='b', **plotopts) for x in axes.get_xticklabels(): x.set_rotation(0) x.set_ha('center') axes.grid(True, zorder=-5) if ts2 is not None: if len(fig.get_axes()) < 2: ax = axes.twinx() axes = fig.get_axes()[1] axes0 = fig.get_axes()[0] axes0.set_zorder(axes.get_zorder()+10) axes0.patch.set_visible(False) axes0.grid(axes.get_zorder()-5) axes.grid(False) try: axes.get_yaxis().get_major_formatter().set_useOffset(False) except: pass if y2log: axes.set_yscale('log', basey=10) pos = np.where(dat2>0) ts2 = ts2[pos] dat2 = dat2[pos] t2vals = [ts2date(t) for t in ts2] if enums2 is not None: pad = min(0.8, 0.1*len(enums2)) axes.set_ylim(-pad, len(enums2)-1+pad) axes.set_yticks(range(len(enums2))) axes.set_yticklabels(enums2) plotopts['zorder'] = 20 axes.plot(t2vals, dat2, color='r', **plotopts) axes.set_ylabel(y2label, color='r', fontproperties=mplfont) if tmin is not None and tmax is not None: axes.set_xlim((ts2date(tmin), ts2date(tmax))) if y2min is not None or y2max is not None: if y2min is not None: y2min = float(y2min) else: y2min = min(dat2) if y2max is not None: y2max = float(y2max) else: y2max = max(dat2) axes.set_ylim((y2min, y2max)) auto_margins(fig, canvas, axes, gspec) if fname is None: figdata = io.BytesIO() fig.savefig(figdata, format='png', facecolor='#FDFDFA') figdata.seek(0) return base64.b64encode(figdata.getvalue()) else: fig.savefig(fname) return fname
def plot( self, fname=None, ax1items=["VI", "In", "ov_N"], ax2items=["q"], xaxis="gamma", xlim=None, y1lim=None, y2lim=None, plotstyles={}, ): """Plot things from a multiresolution plot. ax1items: List of items to plot on axis 1 (left) ax1items: List of items to plot on axis 2 (right) Each item should be a name of something on the MultiResolutionCorrelation object. """ # Recalculate all the stuff table = self.table() # remove NmiO if we don't have that attribute if ax1items == ["VI", "In", "ov_N"] and "ov_N" in ax1items: if "ov_N" not in table: ax1items = ax1items[:] ax1items.remove("ov_N") # Silently remove any axis items that don't exist. ax1items = [x for x in ax1items if x in table.keys()] ax2items = [x for x in ax2items if x in table.keys()] # Prepare plot figure if fname: from matplotlib.backends.backend_agg import Figure, FigureCanvasAgg f = Figure() c = FigureCanvasAgg(f) else: from matplotlib import pyplot f = pyplot.gcf() # Add axes ax1 = f.add_subplot(111) if xaxis in ("gamma",): ax1.set_xscale("log") ax2 = ax1.twinx() # Defaults (move outside of the function eventually) legenditems = [] defaultplotstyles = { "gamma": dict(label="$\gamma$", color="blue", linestyle="-"), "VI": dict(label="$VI$", color="blue", linestyle="-"), "In": dict(label="$I_n$", color="red", linestyle="--"), "N": dict(label="$N$", color="green", linestyle="-."), "ov_N": dict(label="$N_{ov}$", color="green", linestyle="-."), "q": dict(label="$q$", color="black", linestyle=":"), "n_mean": dict(label="$<n>$", color="blue", linestyle=":"), "ov_n_mean": dict(label="$<n_{ov}>$", color="green", linestyle=":"), "entropy": dict(label="$H$", color="blue", linestyle="--"), "F1": dict(label="$F_1$", color="red", linestyle="-"), "ov_F1": dict(label="$F_{1,ov}$", color="green", linestyle="-"), "s_F1": dict(label="$_sF_1$", color="cyan", linestyle="-"), "s_F1_ov": dict(label="$F1_{s,ov}$", color="magenta"), None: dict(), # defaults } plotstyles = plotstyles.copy() plotstyles = recursive_dict_update(defaultplotstyles, plotstyles) # Fill axes def _processaxis(items, axN): # One copy now, for modification of standard values. for item in items: plotstyle = plotstyles[item] = plotstyles.get(item, {}).copy() x = table[xaxis] y = table[item] scale = plotstyle.get("scale", 1) if "scale1" in plotstyle or item == "VI": scale = int(numpy.ceil(numpy.max(y))) plotstyle["scale"] = scale if scale != 1: label = plotstyle.get("label", item) label = label + r"$/%s$" % scale plotstyle["label"] = label plotstyle["scale"] = scale # Make another copy we'll use for kwargs to plot: plotstyle = plotstyles.get(item, {}).copy() scale = plotstyle.pop("scale", 1) l = axN.plot(x, y / scale, **plotstyle)[0] legenditems.append((l, plotstyle.get("label", item))) _processaxis(ax1items, ax1) _processaxis(ax2items, ax2) # Legends and labels ax2.legend(*zip(*legenditems), loc=0) ax2.set_ylim(bottom=0) def axLabels(axItems): """Look up axis labels for a list of items.""" for item in axItems: plotstyle = plotstyles.get(item, {}) label = plotstyle.get("label", item) # if 'scale' in plotstyle: # label = (r'$%s\times$'%plotstyle['scale'])+label yield label ax1labels = ", " ax1.set_ylabel(", ".join(axLabels(ax1items))) ax2.set_ylabel(", ".join(axLabels(ax2items))) ax1.set_xlabel(defaultplotstyles.get(xaxis, dict(label=xaxis))["label"]) if xlim: ax1.set_xlim(*xlim) if y1lim: ax1.set_ylim(*y1lim) if y2lim: ax2.set_ylim(*y2lim) if fname: c.print_figure(fname, bbox_inches="tight") return f
def plot(self, fname=None, ax1items=['VI', 'In', 'ov_N'], ax2items=['q'], xaxis='gamma', xlim=None, y1lim=None, y2lim=None, plotstyles={}): """Plot things from a multiresolution plot. ax1items: List of items to plot on axis 1 (left) ax1items: List of items to plot on axis 2 (right) Each item should be a name of something on the MultiResolutionCorrelation object. """ # Recalculate all the stuff table = self.table() # remove NmiO if we don't have that attribute if ax1items == ['VI', 'In', 'ov_N'] and 'ov_N' in ax1items: if 'ov_N' not in table: ax1items = ax1items[:] ax1items.remove('ov_N') # Silently remove any axis items that don't exist. ax1items = [x for x in ax1items if x in table.keys()] ax2items = [x for x in ax2items if x in table.keys()] # Prepare plot figure if fname: from matplotlib.backends.backend_agg import Figure, FigureCanvasAgg f = Figure() c = FigureCanvasAgg(f) else: from matplotlib import pyplot f = pyplot.gcf() # Add axes ax1 = f.add_subplot(111) if xaxis in ('gamma', ): ax1.set_xscale('log') ax2 = ax1.twinx() # Defaults (move outside of the function eventually) legenditems = [] defaultplotstyles = { 'gamma': dict(label='$\gamma$', color='blue', linestyle='-'), 'VI': dict(label='$VI$', color='blue', linestyle='-'), 'In': dict(label='$I_n$', color='red', linestyle='--'), 'N': dict(label='$N$', color='green', linestyle='-.'), 'ov_N': dict(label='$N_{ov}$', color='green', linestyle='-.'), 'q': dict(label='$q$', color='black', linestyle=':'), 'n_mean': dict(label='$<n>$', color='blue', linestyle=':'), 'ov_n_mean': dict(label='$<n_{ov}>$', color='green', linestyle=':'), 'entropy': dict(label='$H$', color='blue', linestyle='--'), 'F1': dict(label='$F_1$', color='red', linestyle='-'), 'ov_F1': dict(label='$F_{1,ov}$', color='green', linestyle='-'), 's_F1': dict(label='$_sF_1$', color='cyan', linestyle='-'), 's_F1_ov': dict(label='$F1_{s,ov}$', color='magenta'), None: dict(), # defaults } plotstyles = plotstyles.copy() plotstyles = recursive_dict_update(defaultplotstyles, plotstyles) # Fill axes def _processaxis(items, axN): # One copy now, for modification of standard values. for item in items: plotstyle = plotstyles[item] = plotstyles.get(item, {}).copy() x = table[xaxis] y = table[item] scale = plotstyle.get('scale', 1) if 'scale1' in plotstyle or item == 'VI': scale = int(numpy.ceil(numpy.max(y))) plotstyle['scale'] = scale if scale != 1: label = plotstyle.get('label', item) label = label + r'$/%s$' % scale plotstyle['label'] = label plotstyle['scale'] = scale # Make another copy we'll use for kwargs to plot: plotstyle = plotstyles.get(item, {}).copy() scale = plotstyle.pop('scale', 1) l = axN.plot(x, y / scale, **plotstyle)[0] legenditems.append((l, plotstyle.get('label', item))) _processaxis(ax1items, ax1) _processaxis(ax2items, ax2) # Legends and labels ax2.legend(*zip(*legenditems), loc=0) ax2.set_ylim(bottom=0) def axLabels(axItems): """Look up axis labels for a list of items.""" for item in axItems: plotstyle = plotstyles.get(item, {}) label = plotstyle.get('label', item) #if 'scale' in plotstyle: # label = (r'$%s\times$'%plotstyle['scale'])+label yield label ax1labels = ', ' ax1.set_ylabel(', '.join(axLabels(ax1items))) ax2.set_ylabel(', '.join(axLabels(ax2items))) ax1.set_xlabel( defaultplotstyles.get(xaxis, dict(label=xaxis))['label']) if xlim: ax1.set_xlim(*xlim) if y1lim: ax1.set_ylim(*y1lim) if y2lim: ax2.set_ylim(*y2lim) if fname: c.print_figure(fname, bbox_inches='tight') return f
def nosql_draw(check_func, rchip1_fpath, rchip2_fpath, kpts1, kpts2): # This gets executed in the child thread and does drawing async style #from matplotlib.backends.backend_pdf import FigureCanvasPdf as FigureCanvas #from matplotlib.backends.backend_pdf import Figure #from matplotlib.backends.backend_svg import FigureCanvas #from matplotlib.backends.backend_svg import Figure from matplotlib.backends.backend_agg import FigureCanvas try: from matplotlib.backends.backend_agg import Figure except ImportError: from matplotlib.figure import Figure kpts1_ = vt.offset_kpts(kpts1, (0, 0), (resize_factor, resize_factor)) kpts2_ = vt.offset_kpts(kpts2, (0, 0), (resize_factor, resize_factor)) #from matplotlib.figure import Figure if check_func is not None and check_func(): return rchip1 = vt.imread(rchip1_fpath) rchip1 = vt.resize_image_by_scale(rchip1, resize_factor) if check_func is not None and check_func(): return rchip2 = vt.imread(rchip2_fpath) rchip2 = vt.resize_image_by_scale(rchip2, resize_factor) if check_func is not None and check_func(): return try: idx = cm.daid2_idx[daid] fm = cm.fm_list[idx] fsv = None if cm.fsv_list is None else cm.fsv_list[idx] fs = None if fsv is None else fsv.prod(axis=1) except KeyError: fm = [] fs = None fsv = None maxnum = 200 if fs is not None and len(fs) > maxnum: # HACK TO ONLY SHOW TOP MATCHES sortx = fs.argsort()[::-1] fm = fm.take(sortx[:maxnum], axis=0) fs = fs.take(sortx[:maxnum], axis=0) was_interactive = mpl.is_interactive() if was_interactive: mpl.interactive(False) #fnum = 32 fig = Figure() canvas = FigureCanvas(fig) # NOQA #fig.clf() ax = fig.add_subplot(1, 1, 1) if check_func is not None and check_func(): return #fig = pt.plt.figure(fnum) #H1 = np.eye(3) #H2 = np.eye(3) #H1[0, 0] = .5 #H1[1, 1] = .5 #H2[0, 0] = .5 #H2[1, 1] = .5 ax, xywh1, xywh2 = pt.show_chipmatch2(rchip1, rchip2, kpts1_, kpts2_, fm, fs=fs, colorbar_=False, ax=ax) if check_func is not None and check_func(): return savekw = { # 'dpi' : 60, 'dpi' : 80, } axes_extents = pt.extract_axes_extents(fig) #assert len(axes_extents) == 1, 'more than one axes' extent = axes_extents[0] with io.BytesIO() as stream: # This call takes 23% - 15% of the time depending on settings fig.savefig(stream, bbox_inches=extent, **savekw) stream.seek(0) data = np.fromstring(stream.getvalue(), dtype=np.uint8) if check_func is not None and check_func(): return pt.plt.close(fig) image = cv2.imdecode(data, 1) thumbsize = 221 max_dsize = (thumbsize, thumbsize) dsize, sx, sy = vt.resized_clamped_thumb_dims(vt.get_size(image), max_dsize) if check_func is not None and check_func(): return image = vt.resize(image, dsize) vt.imwrite(fpath, image) if check_func is not None and check_func(): return
def time_histogram(request): form = TimePersonForm(request.GET) if not form.is_valid(): pass else: q = form.cleaned_data["q"] use_regex = form.cleaned_data.get("regex", False) if use_regex: regex_pattern = re.compile(q, re.I) matches = get_time_slots(**form.cleaned_data) H = cava.util.Histogram() for slot in matches: intervals = cava.util.parseslot(slot.shift(), slot.name) for name, start, end in intervals: if not ((not use_regex and q.lower() in name.lower()) or (use_regex and regex_pattern.search(name))): continue start_mins = _time_since_weekstart(start) end_mins = _time_since_weekstart(end) # if start_mins == 970 or end_mins == 970: # print start_mins, end_mins if end_mins > start_mins: H.add(start_mins, end_mins) else: H.add(start_mins, 10080) H.add(0, end_mins) plot_rows = [(0, 0)] rows = [] for time, count in H.rows(): plot_rows.append((time / 1440.0, plot_rows[-1][1])) rows.append(dict(count=count, time=time, day=time // 1440, hour=time % 1440 // 60, minute=time % 60)) plot_rows.append((time / 1440.0, count)) img_path = request.get_full_path() + "&img=1" # print 'w' # for row in rows: # pass # print 'x' # for t, c in plot_rows: # if .5 < t < .8: # print t, c # print 'y' # for t, c in H.rows(): # if 800 < t < 1200: # print t, c if "img" in request.REQUEST: import tempfile tmpdir = tempfile.mkdtemp() os.environ["MPLCONFIGDIR"] = tmpdir from matplotlib.backends.backend_agg import Figure, FigureCanvasAgg f = Figure() c = FigureCanvasAgg(f) ax = f.add_subplot(111) ax.set_xlabel("Days (0=Monday, 1=Tuesday, etc)") ax.set_ylabel("Number of times on call") for dayx, day in zip((0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5), "MTWTFSS"): ax.text(x=dayx, y=0.5, s=day) ts, cs = zip(*plot_rows) ax.plot(ts, cs) ax.set_xlim(0, 7) for i in range(1, 7): ax.axvline(x=i, color="gray", linestyle="--") import cStringIO p = cStringIO.StringIO() c.print_figure(p, bbox_inches="tight") import shutil shutil.rmtree(tmpdir) response = HttpResponse(p.getvalue(), mimetype="image/png") return response template = django.template.loader.get_template("cava/time_histogram.html") body = template.render(RequestContext(request, locals())) return HttpResponse(body)