def setUvFormat(self): self.subplot.xaxis.set_major_formatter(PL.FuncFormatter(self.uv_format)) self.subplot.xaxis.set_major_locator(PL.MultipleLocator(self.parent.M/6)) self.subplot.yaxis.set_major_formatter(PL.FuncFormatter(self.uv_format)) self.subplot.yaxis.set_major_locator(PL.MultipleLocator(self.parent.M/6)) self.subplot.set_xlim(100., 500.) self.subplot.set_ylim(100., 500.)
def Pace_vs_Time(data): fig = pl.figure() ax4 = fig.add_subplot(111) ax4.plot(data['MovingTime'], data['Pace'], color='b') ax4 = pl.gca() ax4.xaxis.set_major_formatter(pl.FuncFormatter(HMS)) ax4.yaxis.set_major_formatter(pl.FuncFormatter(HMS)) pl.xlabel("Time") pl.ylabel("Pace") pl.xlim([0, data['MovingTime'][-1]]) pl.ylim([240, 720]) ### GRAPH ORIGINAL/SMOOTHED DATA ### # fig = pl.figure() # pl.title("Moving Triangle Smoothing") # ax5 = fig.add_subplot(111) pl.plot(data['MovingTime'], smoothTriangle(data['Pace'], 10), label="smoothed d=10", color='r') # ax5 = pl.gca() # ax5.xaxis.set_major_formatter(pl.FuncFormatter(HMS)) # ax5.yaxis.set_major_formatter(pl.FuncFormatter(HMS)) # pl.xlabel("Time") # pl.ylabel("Pace") # pl.xlim([0,data['ElapsedTime'][-1]]) # pl.ylim([240,720]) pl.legend()
def setArcFormat(self): self.subplot.xaxis.set_major_formatter(PL.FuncFormatter(self.arcsec_format)) self.subplot.xaxis.set_major_locator(PL.MultipleLocator(self.parent.M/10)) self.subplot.yaxis.set_major_formatter(PL.FuncFormatter(self.arcsec_format)) self.subplot.yaxis.set_major_locator(PL.MultipleLocator(self.parent.M/10)) self.subplot.set_xlim(200., 400.) self.subplot.set_ylim(200., 400.)
def set_minor_ticks(ax, x=True, y=True): """ Labels first minor tick in the case that there is only a single major tick label visible. """ def even(x, pos): if int(str(int(x * 10**8))[0]) % 2: return "" elif pylab.rcParams["text.usetex"]: return "$%s$" % float_to_latex(x) else: return str(int(x)) # xticks if x: ticks = list(ax.get_xticks()) xlim = ax.get_xlim() for i, tick in enumerate(ticks[::-1]): if not xlim[0] <= tick <= xlim[1]: ticks.pop(-1) if len(ticks) <= 1: ax.xaxis.set_minor_formatter(pylab.FuncFormatter(even)) # yticks if y: ticks = list(ax.get_yticks()) ylim = ax.get_ylim() for i, tick in enumerate(ticks[::-1]): if not ylim[0] <= tick <= ylim[1]: ticks.pop(-1) if len(ticks) <= 1: ax.yaxis.set_minor_formatter(pylab.FuncFormatter(even)) return
def drawhist(self): from matplotlib import pyplot as plt import pandas as pd df = pd.read_csv(r'fishMorganMACCS.csv') log = df['logTox'] val = df['toxValue'] plt.rcParams['font.family'] = 'Times New Roman' plt.rcParams['axes.xmargin'] = 0 plt.rcParams['axes.ymargin'] = 0 #inch settings dpi = 300 w = 1063 / dpi h = 532 / dpi fig = plt.figure(figsize=(w, h), dpi=dpi) #fig = plt.figure(figsize=(5, 2.5)) #ax = fig.add_subplot(111) ax1 = fig.add_subplot(1, 2, 1) ax2 = fig.add_subplot(1, 2, 2) ax1.xaxis.set_major_formatter( plt.FuncFormatter(lambda x, loc: "{:,}".format(int(x)))) ax1.yaxis.set_major_formatter( plt.FuncFormatter(lambda x, loc: "{:,}".format(int(x)))) ax2.xaxis.set_major_formatter( plt.FuncFormatter(lambda x, loc: "{:,}".format(int(x)))) #normal ax1.hist(val, bins=200, range=(-20, 120)) ax1.set_ylim([0, 500]) ax1.set_xlabel("Ecotoxicity [mg/L]", fontsize=5) #log ax2.hist(log, bins=200, range=(-6, 6)) ax2.set_ylim([0, 60]) ax2.set_xlabel("Log Ecotoxicity [mg/L]", fontsize=5) #common label ax1.set_ylabel("Count", fontsize=5) ax1.tick_params(labelsize=4, width=0.3) ax2.tick_params(labelsize=4, width=0.3) #axis width axis = ['top', 'bottom', 'left', 'right'] line_width = [0.3, 0.3, 0.3, 0.3] for a, w in zip(axis, line_width): # change axis width ax1.spines[a].set_linewidth(w) ax2.spines[a].set_linewidth(w) #plt.subplots_adjust(left=0.045, bottom=0.03, right=0.75, top=0.55, wspace=0.1, hspace=0.15) plt.savefig('log.jpg', bbox_inches='tight', dpi=dpi) #plt.savefig('log.jpg', bbox_inches='tight',dpi=dpi) plt.show()
def tempo_analyzer(data, wanted, pace): fig = pl.figure() ax = fig.add_subplot(111) ax.xaxis.set_major_formatter(pl.FuncFormatter(HMS)) ax.yaxis.set_major_formatter(pl.FuncFormatter(HMS)) pl.title("Tempo Analyzer") pl.xlabel("Time") pl.ylabel("Pace") indices = np.where(data['Lap'] == int(wanted)) x = data['MovingTime'][indices] y = data['Pace'][indices] pl.xlim([x[0], x[-1]]) ax.plot(x, y, color='r') pl.axhline(y=pace, linewidth=2, color='k')
def barGraph(data, **kw): """Draws a bar graph for the given data""" from pylab import bar kw.setdefault('barw', 0.5) kw.setdefault('log', 0) kw.setdefault('color', 'blue') xs = [i + 1 for i, row in enumerate(data)] names, ys = zip(*data) names = [n.replace('_', '\n') for n in names] #print 'got xs %s, ys %s, names %s' % (xs, ys, names) bar(xs, ys, width=kw['barw'], color=kw['color'], align='center', log=kw['log']) ax = pylab.gca() def f(x, pos=None): n = int(x) - 1 if n + 1 != x: return '' if n < 0: return '' try: return names[n] except IndexError: return '' ax.xaxis.set_major_formatter(pylab.FuncFormatter(f)) ax.xaxis.set_major_locator(pylab.MultipleLocator(1)) ax.set_xlim(0.5, len(names) + 0.5) for l in ax.get_xticklabels(): pylab.setp(l, rotation=90) start = 0.08, 0.18 pos = (start[0], start[1], 0.99 - start[0], 0.95 - start[1]) ax.set_position(pos)
def Pace_Dist(data): fig = pl.figure() ax9 = fig.add_subplot(111) num_bins = 50 ax9.hist(data['Pace'], num_bins) ax9.xaxis.set_major_formatter(pl.FuncFormatter(HMS)) pl.xlabel("Pace")
def draw_heat_graph(getgraph, opts): # from pyevolve_graph script stage_points = getgraph() fg = pl.figure() ax = fg.add_subplot(111) pl.imshow(stage_points, aspect="auto", interpolation="gaussian", cmap=matplotlib.cm.__dict__["jet"]) pl.title("Population scores along the generations") def labelfmt(x, pos=0): # there is surely a better way to do that return (float(x) == int(x)) and '%d' % (x) or '' ax.xaxis.set_major_formatter(pl.FuncFormatter(labelfmt)) pl.xlabel('Generations -->') pl.ylabel('Sorted Population Results') pl.grid(True) pl.colorbar() if opts.outfile: fg.savefig(opts.outfile) if opts.show: pl.show()
def draw_correl_graph(getgraph, opts): # http://matplotlib.sourceforge.net/index.html fg = pl.figure() ax = fg.add_subplot(111) bars = getgraph() for (values, attrs) in bars: indexes, width = pl.arange(len(values)), 1.0 / len(bars) yvalues = [x.speedup for x in values] xoffset = width * bars.index((values, attrs)) ax.bar(indexes + xoffset, yvalues, width, picker=4000, **attrs) ax.legend(loc='lower left') fg.canvas.draw() # dynamic annotations def on_pick(event): ind = int(event.mouseevent.xdata) point = bars[0][0][ind] tooltip.set_position((event.mouseevent.xdata, event.mouseevent.ydata)) tooltip.set_text(point_descr(point)) tooltip.set_visible(True) fg.canvas.draw() tooltip = ax.text(0, 0, "undef", bbox=dict(facecolor='white', alpha=0.8), verticalalignment='bottom', visible=False) # graph title try: title = 'Correlation Graph for %s' % (opts.id or opts.targets or bars[0][0][0].target) except: title = 'Correlation Graph' # redraw axis, set labels, legend, grid, ... def labelfmt(x, pos=0): return '%.2f%%' % (100.0 * x) ax.yaxis.set_major_formatter(pl.FuncFormatter(labelfmt)) pl.ylabel('speedup (higher is better) -->') pl.xlabel('Configurations (ordered by decreasing speedup of ' + bars[0][1]['label'] + ') -->') pl.title(title) pl.axhspan(0.0, 0.0) pl.axvspan(0.0, 0.0) pl.grid(True) if opts.outfile: fg.savefig(opts.outfile) if opts.show: fg.canvas.mpl_connect('pick_event', on_pick) pl.show()
def draw_search_graph(plots): import pylab as pl fg = pl.figure() ax = fg.add_subplot(111) ax.yaxis.set_major_formatter(pl.FuncFormatter(labelfmt)) for (values, attrs) in plots: indexes, width = pl.arange(len(values)), 1.0 / len(plots) yvalues = [x.result for x in values] xoffset = width * plots.index((values, attrs)) ax.plot(indexes + xoffset, yvalues, **attrs) legend = ax.legend(loc='best') legend.get_frame().set_alpha(0.6) fg.canvas.draw() pl.ylabel('tradeoff improvement -->') pl.xlabel('number of tested configurations -->') pl.title('Search Graph') pl.axhspan(0.0, 0.0) pl.axvspan(0.0, 0.0) pl.grid(True) pl.show()
def Distance_vs_Time(data): fig = pl.figure() ax3 = fig.add_subplot(111) ax3.plot(data['MovingTime'], data['Distance'], color='r') ax3 = pl.gca() ax3.xaxis.set_major_formatter(pl.FuncFormatter(HMS)) pl.xlabel("Time") pl.ylabel("Distance (mi)") pl.xlim([0, data['MovingTime'][-1]])
def Elevation_vs_Time(data): fig = pl.figure() ax2 = fig.add_subplot(111) ax2.plot(data['MovingTime'], data['Altitude'], color='r') ax2 = pl.gca() ax2.xaxis.set_major_formatter(pl.FuncFormatter(HMS)) pl.xlabel("Time") pl.ylabel("Elevation (ft.)") pl.xlim([0, data['MovingTime'][-1]]) pl.ylim([0, 1500])
def HR_vs_Time(data): fig = pl.figure() ax6 = fig.add_subplot(111) ax6.plot(data['MovingTime'], data['Pulse'], color='r') ax6 = pl.gca() ax6.xaxis.set_major_formatter(pl.FuncFormatter(HMS)) pl.xlabel("Time") pl.ylabel("Heart Rate") pl.xlim([0, data['MovingTime'][-1]]) pl.ylim([40, 190])
def HR_Pace(data): fig = pl.figure() ax7 = fig.add_subplot(111) ax7.scatter(data['Pace'], data['Pulse'], color='blue', s=5, edgecolor='none') ax7.xaxis.set_major_formatter(pl.FuncFormatter(HMS)) #ax7.set_aspect(1./ax7.get_data_ratio()) # make axes square pl.xlabel("Pace") pl.ylabel("Heart Rate") pl.ylim([40, 190])
def set_y_axis(self, plot): import pylab def arcmin_formatter(x, pos): return "{0}'".format(int(x / 60)) ymin, ymax = plot.get_ylim() numticks = len(plot.get_yticks()) mtscale = max(60, 60 * int(abs(ymax - ymin) / numticks / 60)) plot.set_yticks(np.arange(ymin - ymin % 60, ymax - ymax % 60, mtscale)) plot.set_yticks(np.arange(ymin - ymin % 60, ymax - ymax % 60, mtscale / 6.0), minor=True) plot.yaxis.set_major_formatter(pylab.FuncFormatter(arcmin_formatter)) return plot
def plotScatter(distDict, walkType, numHops, N): def log_10_product(x, pos): return '%1i' % (x) ax = pylab.subplot(111) ax.set_xscale('log') ax.set_yscale('log') formatter = pylab.FuncFormatter(log_10_product) ax.xaxis.set_major_formatter(formatter) ax.yaxis.set_major_formatter(formatter) xVals = distDict.keys() yVals = distDict.values() ax.scatter(xVals, yVals, s=40, c='b', marker='s', faceted=False) ax.set_xlim(1e-1, 1e4) ax.set_ylim(1e-1, 1e4) pylab.grid(True) pylab.xlabel(r"Degree", fontsize=12) pylab.ylabel(r"Frequency", fontsize=12)
def set_axis(axis, linlog, ax=None): """Axis setting to linear or logarithmic. Seems not to work any more (python 3 problem?). See also make_log_ticks. """ if ax is None: ax = plt.subplot(111, axes='equal') formatter = plt.FuncFormatter(lin_product) if axis == 'x': print(ax) if linlog == 'log': ax.set_xscale(linlog, nonposx='clip') ax.xaxis.set_major_formatter(formatter) if axis == 'y': if linlog == 'log': ax.set_yscale(linlog, nonposy='clip') ax.yaxis.set_major_formatter(formatter) return ax
def plot_classifier(classifier, dataset, target, first_feat, second_feat): """ Visualizes the result for two features. Added feature that stores the image :param classifier: which classifier you wish to use :param dataset: the complete dataset :param target: target values. (in our case its the diabetes column) :param first_feat: the first feature :param second_feat: the second feature :return: no return value """ cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA']) cmap_bold = ListedColormap(['#FF0000', '#00FF00']) X = dataset[[first_feat, second_feat]] y = target.replace(['pos', 'neg'], [1, 0]) lable = ["neg", "pos"] classifier.fit(X, y) x_min, x_max = X.iloc[:, 0].min() - .1, X.iloc[:, 0].max() + .1 y_min, y_max = X.iloc[:, 1].min() - .1, X.iloc[:, 1].max() + .1 xx, yy = np.meshgrid(np.linspace(x_min, x_max, 100), np.linspace(y_min, y_max, 100)) Z = classifier.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) pl.figure() pl.pcolormesh(xx, yy, Z, cmap=cmap_light) pl.scatter(X.iloc[:, 0], X.iloc[:, 1], c=y, cmap=cmap_bold) formatter = pl.FuncFormatter(lambda i, *args: lable[int(i)]) pl.colorbar(ticks=[0, 1], format=formatter) pl.xlabel(first_feat) pl.ylabel(second_feat) pl.axis('tight') fig = pl.gcf() pl.show() fig.savefig( f"../visualization_through_web_app/static/{first_feat}X{second_feat}.png" )
def plot_dmags(gi, dmags, sedcolorkey, sedtype, plotfilterlist=filterlist, titletext=None, ylims=None, xlims=None, newfig=True, figname=None): # ylims = dictionary (ylim['u'] = [0, 0]) sedtypes = ['kurucz', 'mlt', 'quasar', 'white_dwarf', 'sn'] # make figure of change in magnitude if newfig: pylab.figure() yplots = 3 xplots = 2 if len(plotfilterlist) == 1: yplots = 1 xplots = 1 pylab.subplots_adjust(top=0.93, wspace=0.32, hspace=0.32, bottom=0.09, left=0.12, right=0.96) # For Kurucz models if sedtype == 'kurucz': print "# looking at kurucz" # set colors of data points based on their metallicity metallicity = numpy.array(sedcolorkey[0]) logg = numpy.array(sedcolorkey[1]) metcolors = ['c', 'c', 'b', 'g', 'y', 'r', 'm'] metbinsize = abs(metallicity.min() - metallicity.max()) / 6.0 metbins = numpy.arange(metallicity.min(), metallicity.max() + metbinsize, metbinsize) print metbinsize, metbins i = 1 # for each filter, use a different subplot plot_logg2 = False for f in plotfilterlist: ax = pylab.subplot(yplots, xplots, i) for metidx in range(len(metbins)): condition =((metallicity>=metbins[metidx]) & (metallicity<=metbins[metidx]+metbinsize) \ & (logg>3.5)) mcolor = metcolors[metidx] pylab.plot(gi[condition], dmags[f][condition], mcolor + '.') if plot_logg2: condition =((metallicity>=metbins[metidx]) & (metallicity<=metbins[metidx]+metbinsize) \ & (logg<2.5)) mcolor = metcolors[metidx] pylab.plot(gi[condition], dmags[f][condition], mcolor + 'x') i = i + 1 elif sedtype == 'quasar': print "# looking at quasars" # set the colors of data points based on their redshift redshift = sedcolorkey redcolors = ['b', 'b', 'g', 'g', 'r', 'r', 'm', 'm'] redbinsize = 0.5 redbins = numpy.arange(0.0, 3.0 + redbinsize, redbinsize) print redbinsize, redbins, redcolors i = 1 # for each filter, use a different subplot for f in plotfilterlist: ax = pylab.subplot(yplots, xplots, i) for redidx in range(len(redbins)): condition = ((redshift >= redbins[redidx]) & (redshift <= redbins[redidx] + redbinsize)) rcolor = redcolors[redidx] pylab.plot(gi[condition], dmags[f][condition], rcolor + 'o') i = i + 1 elif sedtype == 'galaxy': print "# looking at galaxies" # set the colors of data points based on their redshift gallist = sedcolorkey redcolors = ['b', 'b', 'g', 'g', 'r', 'r', 'm', 'm'] redbinsize = 0.3 redbins = numpy.arange(0.0, 1.7 + redbinsize, redbinsize) print redbinsize, redbins, redcolors i = 1 for f in plotfilterlist: ax = pylab.subplot(yplots, xplots, i) j = 0 for g in gallist: galbase, redshift = g.split('_') redshift = float(redshift) redidx = int(redshift / redbinsize) pylab.plot(gi[j], dmags[f][j], redcolors[redidx] + '.') j = j + 1 i = i + 1 elif sedtype == 'mlt': print "# looking at MLT" # set the colors of data points based on their type mltlist = sedcolorkey[0] mlist = sedcolorkey[1] llist = sedcolorkey[2] tlist = sedcolorkey[3] i = 1 for f in plotfilterlist: ax = pylab.subplot(yplots, xplots, i) for j in range(len(mltlist)): if (mltlist[j] in mlist): pylab.plot(gi[j], dmags[f][j], 'bx') elif (mltlist[j] in llist): pylab.plot(gi[j], dmags[f][j], 'gx') elif (mltlist[j] in tlist): pylab.plot(gi[j], dmags[f][j], 'mx') i = i + 1 elif sedtype == 'white_dwarf': print "# looking at White Dwarf" # set the colors of data points based on their type wdlist = sedcolorkey[0] hlist = sedcolorkey[1] helist = sedcolorkey[2] i = 1 for f in plotfilterlist: ax = pylab.subplot(yplots, xplots, i) for j in range(len(wdlist)): if (wdlist[j] in hlist): pylab.plot(gi[j], dmags[f][j], 'y+') elif (wdlist[j] in helist): pylab.plot(gi[j], dmags[f][j], 'y+') i = i + 1 elif sedtype == 'sn': print "# looking at SN" # set the color of data points based on their redshift and day snlist = sedcolorkey[0] redcolors = ['b', 'b', 'g', 'g', 'r', 'r', 'm', 'm'] redbinsize = 0.18 redbins = numpy.arange(0.0, 1.0 + redbinsize, redbinsize) print redbinsize, redbins, redcolors day_symbol = {'0': 's', '20': 's', '40': 's'} i = 1 for f in plotfilterlist: ax = pylab.subplot(yplots, xplots, i) j = 0 for s in snlist: day, redshift = s.split('_') redshift = float(redshift) redidx = int(redshift / redbinsize) pylab.plot(gi[j], dmags[f][j], redcolors[redidx] + day_symbol[day]) j = j + 1 i = i + 1 # set up generic items for i in range(0, len(plotfilterlist)): f = plotfilterlist[i - 1] ax = pylab.subplot(yplots, xplots, i + 1) pylab.xlabel("g-i") pylab.ylabel(r"$\Delta$ %s (mmag)" % (f)) def axis_formatter(x, pos): return "%.1f" % (x) formatter = pylab.FuncFormatter(axis_formatter) ax.yaxis.set_major_formatter(formatter) # set axes limits if ylims == None: pass else: try: pylab.ylim(ylims[f][0], ylims[f][1]) except KeyError: pass if xlims == None: pass else: try: pylab.xlim(xlims[f][0], xlims[f][1]) except KeyError: pass # put a grid in the background if newfig: for i in range(0, len(plotfilterlist)): ax = pylab.subplot(yplots, xplots, i + 1) pylab.grid(True) if titletext != None: pylab.suptitle(titletext) if figname != None: pylab.savefig(figname + '.png', format='png') return
edgecolor='b', label="Best Vis") ax1.set_xscale('log') ax1.legend() ax2.scatter(abs_vsby, numpy.array(abs_sknt) * 1.15, marker='+', color='r', label='Best Wind') ax2.scatter(abv_vsby, numpy.array(abv_sknt) * 1.15, marker='o', facecolor='w', edgecolor='b', label='Best Vis') ax2.set_xscale('log') ax2.legend() formatter = pylab.FuncFormatter(log_10_product) ax1.xaxis.set_major_formatter(formatter) ax2.xaxis.set_major_formatter(formatter) ax1.set_title(" \nMax/Min Method: %s/%s Sites Hit" % (len(hits), len(bs_vsby))) ax2.set_title(" \nAverage Method: %s/%s Sites Hit" % (len(ahits), len(abs_vsby))) fig.savefig('test.png') #import iemplot #iemplot.makefeature('test')
def draw_graph(getgraph, opts): # http://matplotlib.sourceforge.net/index.html fg = pl.figure() ax = fg.add_subplot(111) global graph_plots, all_points graph_plots, all_points = [], [] def draw_tradeoff_plots(ratio, points, attrs): # select tradeoff given ratio best = atos_lib.atos_client_results.select_tradeoff(points, ratio) # graph limits xmin = min([p.sizered for p in points]) xmax = max([p.sizered for p in points]) ymin = min([p.speedup for p in points]) ymax = max([p.speedup for p in points]) # number of points on ratio line nbtk = int((ratio >= 1 and 2 or 1 / ratio) * 32) # ratio line points coordinates xtk = [xmin + i * ((xmax - xmin) / nbtk) for i in range(nbtk + 1)] ytk = [best.speedup + (1.0 / ratio) * (best.sizered - x) for x in xtk] coords = filter(lambda (x, y): y >= ymin and y <= ymax, zip(xtk, ytk)) # first plot: selected tradeoff point attrs.update({'label': '_nolegend_'}) attrs.update({'markersize': 20, 'linewidth': 0, 'alpha': 0.4}) plots = [(([best.sizered], [best.speedup]), dict(attrs))] # second plot: ratio line attrs.update({'marker': '', 'linewidth': 2, 'linestyle': 'solid'}) plots += [((zip(*coords)[0], zip(*coords)[1]), dict(attrs))] return plots def draw_all(): global graph_plots, all_points, selected_points, similar_points # :( # remove old plots old_points = [(p.sizered, p.speedup, p.variant) for p in all_points] for x in list(graph_plots): graph_plots.remove(x) x.remove() # get graph values scatters, frontiers = getgraph() all_points = sum([x[0] for x in scatters + frontiers], []) # draw scatters for (points, attrs) in scatters: attrsmap = { 's': 20, 'label': '_nolegend_', 'zorder': 2, 'color': 'r', 'edgecolor': 'k' } attrsmap.update(attrs) xy = zip(*[(p.sizered, p.speedup) for p in points]) gr = ax.scatter(*xy, **attrsmap) graph_plots.append(gr) # draw frontiers (line plots) for (points, attrs) in frontiers: attrsmap = { 'color': 'r', 'marker': 'o', 'label': '_nolegend_', 'zorder': 2, 'markersize': 7, 'linestyle': 'dashed', 'linewidth': 2 } attrsmap.update(attrs) xy = zip(*sorted([(p.sizered, p.speedup) for p in points])) gr, = ax.plot(xy[0], xy[1], **attrsmap) graph_plots.append(gr) # show tradeoffs for each frontier for ratio in opts.tradeoffs or []: for ((xcrd, ycrd), attrs) in \ draw_tradeoff_plots(ratio, points, dict(attrsmap)): graph_plots.append(ax.plot(xcrd, ycrd, **attrs)[0]) # draw selected points (hidden) if opts.show and all_points: # workaround pb with pick_event event ind (4000) attrsmap = { 'color': 'b', 'marker': 'o', 'markersize': 20, 'linewidth': 0, 'alpha': 0.4 } xy = zip(*sorted([(p.sizered, p.speedup) for p in all_points])) selected_points, = \ ax.plot(xy[0], xy[1], visible=False, picker=4000, **attrsmap) graph_plots.append(selected_points) # similar point plot attrsmap.update({'color': 'g'}) similar_points, = ax.plot(None, None, visible=False, **attrsmap) graph_plots.append(similar_points) # highlight new points if opts.follow and old_points: new_points = [ p for p in all_points if ((p.sizered, p.speedup, p.variant) not in old_points) ] attrsmap = { 'color': 'r', 'marker': 'o', 'markersize': 20, 'linewidth': 0, 'alpha': 0.4, 'zorder': 1 } if new_points: xy = zip(*[(p.sizered, p.speedup) for p in new_points]) new_points, = ax.plot(*xy, **attrsmap) graph_plots.append(new_points) # redraw legend and figure if opts.xlim: pl.xlim([float(l) for l in opts.xlim.split(",")]) if opts.ylim: pl.ylim([float(l) for l in opts.ylim.split(",")]) ax.legend(loc='lower left') fg.canvas.draw() # dynamic annotations def on_pick(event): def closest(x, y): dp = [(math.hypot(p.sizered - x, p.speedup - y), p) for p in all_points] return sorted(dp)[0][1] def highlight(p): # print point on console print '-' * 40 + '\n' + point_str(p) # highlight point selected_points.set_visible(True) selected_points.set_data(p.sizered, p.speedup) # highlight similar points (same variant) sim = zip(*([(c.sizered, c.speedup) for c in all_points if c.variant == p.variant and c != p])) similar_points.set_visible(True) similar_points.set_data(sim and sim[0], sim and sim[1]) # selected point legend main_legend = ax.legend_ lg = point_str(p, short=True, no_id=opts.anonymous) lp = pl.legend([selected_points], [lg], loc='lower right', numpoints=1) pl.setp(lp.get_texts(), fontsize='medium') lp.get_frame().set_alpha(0.5) pl.gca().add_artist(main_legend) fg.canvas.draw() ax.legend_ = main_legend highlight(closest(event.mouseevent.xdata, event.mouseevent.ydata)) # live plotting def on_timer(): draw_all() # draw graph for the first time draw_all() # graph title title = 'Optimization Space for %s' % (opts.id or opts.targets or (all_points and all_points[0].target)) if opts.refid: title += ' [ref=%s]' % opts.refid if opts.filter: title += ' [filter=%s]' % opts.filter # redraw axis, set labels, legend, grid, ... def labelfmt(x, pos=0): return '%.2f%%' % (100.0 * x) ax.xaxis.set_major_formatter(pl.FuncFormatter(labelfmt)) ax.yaxis.set_major_formatter(pl.FuncFormatter(labelfmt)) pl.axhspan(0.0, 0.0) pl.axvspan(0.0, 0.0) pl.title(title) pl.xlabel('size reduction (higher is better) -->') pl.ylabel('speedup (higher is better) -->') if opts.xlim: pl.xlim([float(l) for l in opts.xlim.split(",")]) if opts.ylim: pl.ylim([float(l) for l in opts.ylim.split(",")]) pl.grid(True) if opts.outfile: fg.savefig(opts.outfile) if opts.show: fg.canvas.mpl_connect('pick_event', on_pick) if opts.follow: timer = atos_lib.repeatalarm(on_timer, 5.0).start() pl.show() if opts.follow: timer.stop()
def plot_colorcolor(mags, sedkeylist, sedcolorkey, sedtype, filterlist, titletext=None, newfig=True): if newfig: pylab.figure() gi = calc_stdcolors(mags) magcolors = {} colorlabels = [] colorslabels[0] = None for i in range(1 - 7): colorlabels[i] = filterlist[i - 1] + '-' + filterlist[i] magscolors[colorlabels[i]] = mags[filterlist[i - 1]] - mags[filterlist[i]] # colors = ug, gr, ri, iz, zy if sedtype == 'kurucz': print "# plotting kurucz" # set colors of data points based on their metallicity metallicity = sedcolorkey metcolors = ['c', 'c', 'b', 'g', 'y', 'r', 'm'] metbinsize = abs(sedcolorkey.min() - sedcolorkey.max()) / 6.0 metbins = numpy.arange(sedcolorkey.min(), sedcolorkey.max() + metbinsize, metbinsize) print metbinsize, metbins i = 1 # use a different subplot for each color/color combo for i in range(len(colorlabels - 1)): ax = pylab.subplot(3, 2, i) for metidx in range(len(metbins)): condition = ((metallicity >= metbins[metidx]) & (metallicity <= metbins[metidx] + metbinsize)) mcolor = metcolors[metidx] pylab.plot(magscolors[colorlabels[i]][condition], magscolors[colorlabels[i + 1]][f][condition], mcolor + '.') i = i + 1 ax = pylab.subplot(3, 2, 7) for metidx in range(len(metbins)): condition = ((metallicity >= metbins[metidx]) & (metallicity <= metbins[metidx] + metbinsize)) mcolor = metcolors[metidx] pylab.plot(gi[condition], magscolors[colorlabels[i + 1]][f][condition], mcolor + '.') elif sedtype == 'quasar': print "# looking at quasars" # set the colors of data points based on their redshift redshift = sedcolorkey redcolors = ['b', 'b', 'g', 'g', 'r', 'r', 'm', 'm'] redbinsize = 0.5 redbins = numpy.arange(0.0, 3.0 + redbinsize, redbinsize) print redbinsize, redbins, redcolors i = 1 # for each filter, use a different subplot for f in filterlist: ax = pylab.subplot(3, 2, i) for redidx in range(len(redbins)): condition = ((redshift >= redbins[redidx]) & (redshift <= redbins[redidx] + redbinsize)) rcolor = redcolors[redidx] pylab.plot(magscolors[colorlabels[i]][condition], magscolors[colorlabels[i + 1]][f][condition], rcolor + 'o') i = i + 1 ax = pylab.subplot(3, 2, 7) for redidx in range(len(redbins)): condition = ((redshift >= redbins[redidx]) & (redshift <= redbins[redidx] + redbinsize)) rcolor = redcolors[redidx] pylab.plot(gi[condition], magscolors[colorlabels[i + 1]][f][condition], rcolor + 'o') elif sedtype == 'galaxy': print "# looking at galaxies" # set the colors of data points based on their redshift gallist = sedcolorkey redcolors = ['b', 'b', 'g', 'g', 'r', 'r', 'm', 'm'] redbinsize = 0.3 redbins = numpy.arange(0.0, 1.7 + redbinsize, redbinsize) print redbinsize, redbins, redcolors i = 1 # for each filter, use a different subplot for f in filterlist: ax = pylab.subplot(3, 2, i) j = 0 for g in gallist: galbase, redshift = g.split('_') redshift = float(redshift) redidx = int(redshift / redbinsize) pylab.plot(gi[j], dmags[f][j], redcolors[redidx]) j = j + 1 i = i + 1 ax = pylab.subplot(3, 2, 7) j = 0 for g in gallist: galbase, redshift = g.split('_') redshift = float(redshift) redidx = int(redshift / redbinsize) pylab.plot(gi[j], dmags[f][j], redcolors[redidx]) j = j + 1 elif sedtype == 'mlt': print "# looking at MLT" # set the colors of data points based on their type mltlist = sedcolorkey[0] mlist = sedcolorkey[1] llist = sedcolorkey[2] tlist = sedcolorkey[3] i = 1 for f in filterlist: ax = pylab.subplot(3, 2, i) for j in range(len(mltlist)): if (mltlist[j] in mlist): pylab.plot(magscolors[colorlabels[i]][condition], magscolors[colorlabels[i + 1]][f][condition], 'bx') elif (mltlist[j] in llist): pylab.plot(magscolors[colorlabels[i]][condition], magscolors[colorlabels[i + 1]][f][condition], 'gx') elif (mltlist[j] in tlist): pylab.plot(magscolors[colorlabels[i]][condition], magscolors[colorlabels[i + 1]][f][condition], 'mx') i = i + 1 ax = pylab.subplot(3, 2, 7) for j in range(len(mltlist)): if (mltlist[j] in mlist): pylab.plot(gi[condition], magscolors[colorlabels[i + 1]][f][condition], 'bx') elif (mltlist[j] in llist): pylab.plot(gi[condition], magscolors[colorlabels[i + 1]][f][condition], 'gx') elif (mltlist[j] in tlist): pylab.plot(gi[condition], magscolors[colorlabels[i + 1]][f][condition], 'mx') elif sedtype == 'white_dwarf': print "# looking at White Dwarf" # set the colors of data points based on their type wdlist = sedcolorkey[0] hlist = sedcolorkey[1] helist = sedcolorkey[2] i = 1 for f in filterlist: ax = pylab.subplot(3, 2, i) for j in range(len(wdlist)): if (wdlist[j] in hlist): pylab.plot(magscolors[colorlabels[i]][condition], magscolors[colorlabels[i + 1]][f][condition], 'y+') elif (wdlist[j] in helist): pylab.plot(magscolors[colorlabels[i]][condition], magscolors[colorlabels[i + 1]][f][condition], 'y+') i = i + 1 ax = pylab.subplot(3, 2, 7) for j in range(len(wdlist)): if (wdlist[j] in hlist): pylab.plot(gi[condition], magscolors[colorlabels[i + 1]][f][condition], 'y+') elif (wdlist[j] in helist): pylab.plot(gi[condition], magscolors[colorlabels[i + 1]][f][condition], 'y+') # set up generic items for i in range(1, 7): f = filterlist[i - 1] ax = pylab.subplot(3, 2, i) #pylab.xlabel("g-i") #pylab.ylabel(r"$\Delta$ %s (mmag)" %(f)) def axis_formatter(x, pos): return "%.1f" % (x) formatter = pylab.FuncFormatter(axis_formatter) ax.yaxis.set_major_formatter(formatter) # set axes limits if ylims == None: pass else: try: pylab.ylim(ylims[f][0], ylims[f][1]) except KeyError: pass # put a grid in the background if newfig: for i in range(1, 7): ax = pylab.subplot(3, 2, i) pylab.grid(True) #pylab.suptitle(titletext) #pylab.savefig("delta_mags2.eps", format='eps') return
def __init__(self, in_file, vg_files=[1], data_type=1, projection='cyl', color_map='jet', time_zone=0, plot_contours=False, plot_center='t', plot_meridians=True, plot_parallels=True, plot_terminator=True, resolution='c', points_of_interest=[], save_file='', run_quietly=False, dpi=150, parent=None): self.run_quietly = run_quietly self.dpi = float(dpi) plot_parameters = VOAFile((in_file + '.voa')) plot_parameters.parse_file() if (plot_parameters.get_projection() != 'cyl'): print _("Error: Only lat/lon (type 1) input files are supported") sys.exit(1) grid = plot_parameters.get_gridsize() self.image_defs = VOAAreaPlot.IMG_TYPE_DICT[int(data_type)] # TODO This needs a little more work... what if the pcenter card is not specified if plot_center == 'p': plot_centre_location = plot_parameters.get_location( plot_parameters.P_CENTRE) else: plot_centre_location = plot_parameters.get_location( plot_parameters.TX_SITE) self.points_of_interest = [plot_centre_location] if len(points_of_interest) > 0: self.points_of_interest.extend(points_of_interest) imageBuf = P.zeros([grid, grid], float) area_rect = plot_parameters.get_area_rect() points = P.zeros([grid, grid], float) lons = P.zeros(grid * grid, float) lats = P.zeros(grid * grid, float) lons = P.arange(area_rect.get_sw_lon(), area_rect.get_ne_lon() + 0.001, (area_rect.get_ne_lon() - area_rect.get_sw_lon()) / float(grid - 1)) lats = P.arange(area_rect.get_sw_lat(), area_rect.get_ne_lat() + 0.001, (area_rect.get_ne_lat() - area_rect.get_sw_lat()) / float(grid - 1)) colString = 'P.cm.' + color_map colMap = eval(colString) self.subplots = [] matplotlib.rcParams['axes.edgecolor'] = 'gray' matplotlib.rcParams['axes.facecolor'] = 'white' matplotlib.rcParams['figure.facecolor'] = 'white' #matplotlib.rcParams['figure.figsize'] = (6, 10) # matplotlib.rcParams['figure.subplot.hspace'] = 0.45 # matplotlib.rcParams['figure.subplot.wspace'] = 0.35 # matplotlib.rcParams['figure.subplot.right'] = 0.85 colorbar_fontsize = 8 self.num_rows = 1 self.main_title_fontsize = 18 matplotlib.rcParams['legend.fontsize'] = 12 matplotlib.rcParams['axes.labelsize'] = 10 matplotlib.rcParams['axes.titlesize'] = 10 matplotlib.rcParams['xtick.labelsize'] = 8 matplotlib.rcParams['ytick.labelsize'] = 8 # matplotlib.rcParams['figure.subplot.top'] = 0.8 # single figure plots have a larger title so require more space at the top. self.fig = Figure(figsize=(9, 5)) # self.main_title_label = self.fig.suptitle(unicode(self.image_defs['title'],'utf-8'), fontsize=self.main_title_fontsize) ax = self.fig.add_subplot(111, axisbg='white') self.subplots.append(ax) ax.label_outer() #print "opening: ",(in_file+'.vg'+str(vg_files[plot_ctr])) plot_ctr = 0 # dir_name = os.path.dirname(in_file) # if not os.path.exists(dir_name): # os.makedirs(dir_name) vgFile = open(in_file + '.vg' + str(vg_files[plot_ctr])) pattern = re.compile(r"[a-z]+") for line in vgFile: match = pattern.search(line) if not match: value = float(line[int(self.image_defs['first_char'] ):int(self.image_defs['last_char'])]) # TODO Does this need to be normalised here if it's also being done in the plot? value = max(self.image_defs['min'], value) value = min(self.image_defs['max'], value) #if value < self.image_defs[2] : value = self.image_defs[2] #if value > self.image_defs[3] : value = self.image_defs[3] points[int(line[3:6]) - 1][int(line[0:3]) - 1] = value vgFile.close() map = Basemap(\ llcrnrlon=area_rect.get_sw_lon(), llcrnrlat=area_rect.get_sw_lat(),\ urcrnrlon=area_rect.get_ne_lon(), urcrnrlat=area_rect.get_ne_lat(),\ projection=projection,\ lat_0=plot_centre_location.get_latitude(),\ lon_0=plot_centre_location.get_longitude(),\ resolution=resolution, ax=ax) map.drawcoastlines(color='black') map.drawcountries(color='grey') map.drawmapboundary(color='black', linewidth=1.0) warped = ma.zeros((grid, grid), float) warped, warped_lon, warped_lat = map.transform_scalar( points, lons, lats, grid, grid, returnxy=True, checkbounds=False, masked=True) warped = warped.filled(self.image_defs['min'] - 1.0) colMap.set_under(color='k', alpha=0.0) im = map.imshow(warped, cmap=colMap, extent=(-180, 180, -90, 90), origin='lower', norm=P.Normalize(clip=False, vmin=self.image_defs['min'], vmax=self.image_defs['max'])) ####################### # Plot greyline ####################### if plot_terminator: the_sun = Sun() the_month = plot_parameters.get_month(vg_files[plot_ctr] - 1) the_day = plot_parameters.get_day(vg_files[plot_ctr] - 1) the_hour = plot_parameters.get_utc(vg_files[plot_ctr] - 1) if (the_day == 0): the_day = 15 the_year = datetime.date.today().year num_days_since_2k = the_sun.daysSince2000Jan0( the_year, the_month, the_day) res = the_sun.sunRADec(num_days_since_2k) declination = res[1] if (declination == 0.0): declination = -0.001 tau = the_sun.computeGHA(the_day, the_month, the_year, the_hour) if declination > 0: terminator_end_lat = area_rect.get_sw_lat() else: terminator_end_lat = area_rect.get_ne_lat() terminator_lat = [terminator_end_lat] terminator_lon = [area_rect.get_sw_lon()] for i in range(int(area_rect.get_sw_lon()), int(area_rect.get_ne_lon()), 1) + [int(area_rect.get_ne_lon())]: longitude = i + tau tan_lat = -the_sun.cosd(longitude) / the_sun.tand(declination) latitude = the_sun.atand(tan_lat) latitude = max(latitude, area_rect.get_sw_lat()) latitude = min(latitude, area_rect.get_ne_lat()) xpt, ypt = map(i, latitude) terminator_lon.append(xpt) terminator_lat.append(ypt) terminator_lon.append(area_rect.get_ne_lon()) terminator_lat.append(terminator_end_lat) #This is a little simplistic and doesn't work for ortho plots.... ax.plot(terminator_lon, terminator_lat, color='grey', alpha=0.75) ax.fill(terminator_lon, terminator_lat, facecolor='grey', alpha=0.5) tau = -tau if (tau > 180.0): tau = tau - 360.0 if (tau < -180.0): tau = tau + 360.0 #Plot the position of the sun (if it's in the coverage area) if area_rect.contains(declination, tau): xpt, ypt = map(tau, declination) #sbplt_ax.plot([xpt],[ypt],'yh') ax.plot([xpt], [ypt], 'yh') ########################## # Points of interest ########################## for location in self.points_of_interest: if area_rect.contains(location.get_latitude(), location.get_longitude()): xpt, ypt = map(location.get_longitude(), location.get_latitude()) ax.plot([xpt], [ypt], 'ro') ax.text(xpt + 100000, ypt + 100000, location.get_name()) if plot_meridians: if (area_rect.get_lon_delta() <= 90.0): meridians = P.arange(-180, 190.0, 10.0) elif (area_rect.get_lon_delta() <= 180.0): meridians = P.arange(-180.0, 210.0, 30.0) else: meridians = P.arange(-180, 240.0, 60.0) if ((projection == 'ortho') or (projection == 'vandg')): map.drawmeridians(meridians) else: map.drawmeridians(meridians, labels=[1, 1, 0, 1]) if plot_parallels: if (area_rect.get_lat_delta() <= 90.0): parallels = P.arange(-90.0, 120.0, 60.0) else: parallels = P.arange(-90.0, 120.0, 30.0) if ((projection == 'ortho') or (projection == 'vandg')): map.drawparallels(parallels) else: map.drawparallels(parallels, labels=[1, 0, 0, 1]) if plot_contours: map.contour(warped_lon, warped_lat, warped, self.image_defs['y_labels'], linewidths=1.0, colors='k', alpha=0.5) #add a title title_str = plot_parameters.get_plot_description_string( vg_files[plot_ctr] - 1, self.image_defs['plot_type'], time_zone) title_str = title_str + "\n" + plot_parameters.get_detailed_plot_description_string( vg_files[plot_ctr] - 1) self.subplot_title_label = ax.set_title(title_str) # Add a colorbar on the right hand side, aligned with the # top of the uppermost plot and the bottom of the lowest # plot. pos = [0.91, 0.19, 0.02, 0.62] self.cb_ax = self.fig.add_axes(pos) cb = self.fig.colorbar(im, cax=self.cb_ax, orientation='vertical', ticks=self.image_defs['y_labels'], format=P.FuncFormatter( eval('self.' + self.image_defs['formatter']))) cb.set_label(unicode(self.image_defs['title'], 'utf-8')) #print self.image_defs['y_labels'] for t in self.cb_ax.get_yticklabels(): t.set_fontsize(colorbar_fontsize) canvas = FigureCanvasAgg(self.fig) if save_file: self.save_plot(canvas, save_file)
def main(): if os.name == 'nt': dataDir = os.path.expanduser('~') + '\\Appdata\\Roaming\\Freeorion' elif os.name == 'posix': dataDir = os.environ.get( 'XDG_DATA_HOME', os.environ.get('HOME', "") + "/.local/share") + "/freeorion" else: dataDir = os.environ.get('HOME', "") + "/.freeorion" graphDir = dataDir fileRoot = "game1" saveFile = True turnsP = None rankings = [] doPlotTypes = ["PP + 2RP"] + ["PP"] + ["RP"] + ["RP_Ratio" ] # +[ "ShipCount"] allData = {} species = {} empireColors = {} playerName = "Player" logfiles = sorted(glob(dataDir + os.sep + "A*.log")) A1log = glob(dataDir + os.sep + "AI_1.log") if not os.path.exists(dataDir + os.sep + "freeorion.log"): print "can't find freeorion.log" elif A1log and (A1log[0] in logfiles) and ( os.path.getmtime(dataDir + os.sep + "freeorion.log") < os.path.getmtime(A1log[0]) - 300): print "freeorion.log file is stale ( more than 5 minutes older than AI_1.log) and will be skipped" else: data, details = parse_file(dataDir + os.sep + "freeorion.log", False) if len(data.get('PP', [])) > 0: allData[playerName] = data empireColors[playerName] = details['color'] logfiles = sorted(glob(dataDir + os.sep + "A*.log")) A1log = glob(dataDir + os.sep + "AI_1.log") if A1log and A1log[0] in logfiles: A1Time = os.path.getmtime(A1log[0]) for path in logfiles[::-1]: logtime = os.path.getmtime(path) # print "path ", path, "logtime diff: %.1f"%(A1Time -logtime) if logtime < A1Time - 300: del logfiles[logfiles.index(path)] print "skipping stale logfile ", path for lfile in logfiles: try: data, details = parse_file(lfile, True) allData[details['name']] = data empireColors[details['name']] = details['color'] species[details['name']] = details['species'] except: print "error processing %s" % lfile print "Error: exception triggered and caught: ", traceback.format_exc( ) print for plotType in doPlotTypes: print "--------------------" if plotType == "PP": caption = "Production" elif plotType == "RP": caption = "Research" elif plotType == "RP_Ratio": caption = "Res:Prod Ratio" elif plotType == "RP_Ratio": caption = "Res:Prod Ratio" else: caption = plotType pylab.figure(figsize=(10, 6)) ax = pylab.gca() ymin = 9999 ymax = 0 rankings = [] turns = [] pdata = allData.get(playerName, {}).get(plotType, []) if pdata: ymin = min(ymin, min(pdata)) ymax = max(ymax, max(pdata)) turns = allData.get(playerName, {}).get('turnsP', []) for empireName, data in allData.items(): if empireName == playerName: continue adata = data.get(plotType, []) if adata: rankings.append((adata[-1], empireName)) thisMin = min(adata) if thisMin > 0: ymin = min(ymin, thisMin) ymax = max(ymax, max(adata)) if not turns: turns = data.get('turnsP', []) if not turns: if len(allData) == 0: break turns = range(1, len(allData.values()[0].get(plotType, [])) + 1) rankings.sort() pylab.xlabel('Turn') pylab.ylabel(plotType) pylab.title(caption + ' Progression') if ymax >= 100: ax.set_yscale('log', basey=10) if playerName not in allData: print "\t\t\tcan't find playerData in allData\n" else: if playerName in empireColors: turnsP = allData[playerName].get("turnsP", []) thisData = allData.get(playerName, {}).get(plotType, []) print "plotting with color for player: ", playerName, "data min/max: ", min( allData[playerName].get(plotType, [])), ' | ', max( allData[playerName].get(plotType, [])) pylab.plot(turnsP, thisData, 'o-', color=empireColors[playerName], label="%s - %.1f" % (playerName, sum(thisData)), linewidth=2.0) else: print "plotting withOUT color for player: ", playerName, "data min/max: ", min( allData[playerName].get(plotType, [])), ' | ', max( allData[playerName].get(plotType, [])) pylab.plot(turnsP, allData[playerName].get(plotType, []), 'bx-', label=playerName, linewidth=2.0) print "Ranked by ", plotType for rank, name in rankings[::-1]: print name if name in empireColors: adata = allData[name].get(plotType, []) pylab.plot(range(turns[0], turns[0] + len(adata)), adata, color=empireColors[name], label="%s: %s - %.1f" % (name, species[name], sum(adata)), linewidth=2.0) else: print "can't find empire color for ", name # pylab.plot(range(turns[0], turns[0]+len(allData[name])), allData[name].get(plotType, []), label="(%d) "%(empires.index(name)+1)+name+" : "+species[name], linewidth=2.0) # legend(loc='upper left',prop={"size":'medium'}) pylab.legend(loc='upper left', prop={"size": 9}, labelspacing=0.2) pylab.xlabel('Turn') pylab.ylabel(plotType) pylab.title(caption + ' Progression ') x1, x2, y1, y2 = pylab.axis() newY2 = y2 if plotType in ["PP + 2RP", "PP", "RP", "ShipCount"]: for yi in range(1, 10): if 1.05 * ymax < yi * y2 / 10: newY2 = yi * y2 / 10 break print "y1: %.1f ; ymin: %.1f ; newY2/100: %.1f" % (y1, ymin, newY2 / 100) y1 = max(y1, 4, ymin, newY2 / 100) pylab.axis((x1, x2, y1, newY2)) pylab.grid(b=True, which='major', color='0.25', linestyle='-') pylab.grid(b=True, which='minor', color='0.1', linestyle='--') ax.yaxis.set_minor_formatter(pylab.FuncFormatter(show_only_some)) ax.yaxis.set_ticks_position('right') ax.yaxis.set_ticks_position('both') ax.tick_params(labelleft='on' ) # for matplotlib versions where 'both' doesn't work # ax.tick_params(labelright='on') if saveFile: pylab.savefig(graphDir + os.sep + plotType + "_" + fileRoot + ".png") pylab.show()
def analyze_interactions(df): print("\n=== Interactions table, original shape={} ===\n".format(df.shape)) df = df.copy() df['ITEM_ID'] = df['ITEM_ID'].astype(str) df['USER_ID'] = df['USER_ID'].astype(str) df.index = df["TIMESTAMP"].values.astype("datetime64[s]") na_rate = df[INTERACTIONS_REQUIRED_FIELDS].isnull().any(axis=1).mean() print("missing rate in fields", INTERACTIONS_REQUIRED_FIELDS, na_rate) if na_rate > NA_RATE_THRESHOLD: warnings.warn( "High data missing rate for required fields ({:.1%})!".format( na_rate)) df = df.dropna(subset=INTERACTIONS_REQUIRED_FIELDS) print("dropna shape", df.shape) dup_rate = (df.groupby(INTERACTIONS_REQUIRED_FIELDS).size() - 1.0).sum() / df.shape[0] print("duplication rate", dup_rate) if dup_rate > DUP_RATE_THRESHOLD: warnings.warn(""" High duplication rate ({:.1%})! Only one event can be taken at the same (user,item,timestamp) index. """.format(dup_rate)) df = df.drop_duplicates(subset=INTERACTIONS_REQUIRED_FIELDS) print("drop_duplicates shape", df.shape) repeat_rate = (df.groupby(["USER_ID", "ITEM_ID"]).size() - 1.0).sum() / df.shape[0] print("user item repeat rate", repeat_rate) if repeat_rate > REPEAT_RATE_THRESHOLD: warnings.warn(""" High rate of repeated consumptions ({:.1%})! We would not do anything, but it may beneficial to (1) consider keeping only the last interaction between the same user-item pair, (2) consider if the ITEM_IDs have collisions, and/or (3) use high-order hierarchical models. """.format(repeat_rate)) summary = describe_dataframe(df, 'interactions table') print("\n=== Hourly activity pattern ===") print(df.groupby(df.index.hour).size()) print("\n=== Day of week activity pattern ===") print(df.groupby(df.index.dayofweek).size()) plot_patterns = { "date": df.index.date, "hour": df.index.hour, "dayofweek": df.index.dayofweek } for k, v in plot_patterns.items(): pl.plot(df.groupby(v).size(), '.-') pl.gcf().autofmt_xdate() pl.title("Activity pattern by %s" % k) pl.grid() pl.show() print("\n=== Temporal shift analysis ===\n") print( "Sorting and removing repeated user-items for temporal shift analysis..." ) df.sort_index(inplace=True, kind='mergesort') df_dedup = df.drop_duplicates(['USER_ID', 'ITEM_ID'], keep='last') print("\n=== Temporal shift - retrain frequency ===\n") for method in TEMPORAL_LOSS_METHODS: bootstrap_avg = [] past_fut_avg = [] for freq in RETRAIN_FREQUENCY: _, _, _bs_avg, loss_fmt = compute_bootstrap_loss( df_dedup, freq, method) _, _, _ts_avg, loss_fmt = compute_temporal_loss( df_dedup, freq, method, 1) bootstrap_avg.append(_bs_avg) past_fut_avg.append(_ts_avg) pl.plot(RETRAIN_FREQUENCY, bootstrap_avg, '.--', label='same-period bootstrap') pl.plot(RETRAIN_FREQUENCY, past_fut_avg, '.-', label='lagged popularity') pl.legend() pl.xlabel('retrain frequency') pl.title(method + ' loss at different frequencies') pl.grid() pl.gca().yaxis.set_major_formatter( pl.FuncFormatter(lambda y, _: loss_fmt.format(y))) pl.show() print("\n=== Temporal shift - history cutoffs ===\n") for method in TEMPORAL_LOSS_METHODS: for freq in TEMPORAL_FREQUENCY: bootstrap_loss, _, avg_loss, loss_fmt = compute_bootstrap_loss( df_dedup, freq, method) pl.plot(bootstrap_loss.iloc[-TEMPORAL_PLOT_LIMIT:], '.--', label='boostrap baseline={}'.format( loss_fmt.format(avg_loss))) for hist_len in ROLLING_HISTORY_LEN: temporal_loss, df_wgt, avg_loss, loss_fmt = compute_temporal_loss( df_dedup, freq, method, hist_len) pl.plot(temporal_loss.iloc[-TEMPORAL_PLOT_LIMIT:], '.-', label='hist={} * {}, avg={}'.format( hist_len, freq, loss_fmt.format(avg_loss))) pl.gca().yaxis.set_major_formatter( pl.FuncFormatter(lambda y, _: loss_fmt.format(y))) pl.title('{} {} from rolling history (lower is better)'.format( freq, method)) pl.grid() pl.gcf().autofmt_xdate() pl.legend(loc='upper left') pl.twinx() pl.plot(df_wgt.iloc[-TEMPORAL_PLOT_LIMIT:], color='grey', lw=3, ls='--', alpha=0.5) pl.legend(['activity density'], loc='upper right') pl.show() print("\n=== session time delta describe ===") user_time_delta = df.groupby('USER_ID')["TIMESTAMP"].transform( pd.Series.diff).dropna() user_time_delta.sort_values(ascending=False, inplace=True) print(user_time_delta.describe()) plot_loglog(user_time_delta, 'session time delta', show=False) for k, v in TIMEDELTA_REFERENCES: if pl.ylim()[0] < v < pl.ylim()[1]: pl.plot(pl.xlim(), [v, v], '--') pl.text(pl.xlim()[0], v, k) pl.show() user_time_span = df.groupby('USER_ID')["TIMESTAMP"].apply( lambda x: max(x) - min(x)) user_time_span.sort_values(ascending=False, inplace=True) print("=== user time span describe ===") print(user_time_span.describe()) plot_loglog(user_time_span, 'user time span', show=False) for k, v in TIMEDELTA_REFERENCES: if pl.ylim()[0] < v < pl.ylim()[1]: pl.plot(pl.xlim(), [v, v], '--') pl.text(pl.xlim()[0], v, k) pl.show()
def __init__(self, data_file, plot_groups=[1], data_type=2, color_map='jet', plot_contours=False, plot_label="", plot_bands=None, time_zone=0, plot_max_freq=30.0, run_quietly=False, save_file='', dpi=150, parent=None): self.data_type = data_type self.run_quietly = run_quietly self.dpi = dpi self.df = VOAOutFile(data_file, time_zone=time_zone, data_type=self.data_type, quiet=run_quietly) self.image_defs = self.IMG_TYPE_DICT[self.data_type] color_map = eval('P.cm.' + color_map) if plot_groups[0] == 'a': num_grp = self.df.get_number_of_groups() plot_groups = range(0, num_grp) self.subplots = [] number_of_subplots = len(plot_groups) matplotlib.rcParams['axes.edgecolor'] = 'gray' matplotlib.rcParams['axes.facecolor'] = 'white' matplotlib.rcParams['axes.grid'] = True matplotlib.rcParams['figure.facecolor'] = 'white' matplotlib.rcParams['legend.fancybox'] = True matplotlib.rcParams['legend.shadow'] = True matplotlib.rcParams['figure.subplot.hspace'] = 0.45 matplotlib.rcParams['figure.subplot.wspace'] = 0.35 matplotlib.rcParams['figure.subplot.right'] = 0.85 colorbar_fontsize = 12 if number_of_subplots <= 1: self.num_rows = 1 self.main_title_fontsize = 24 matplotlib.rcParams['legend.fontsize'] = 12 matplotlib.rcParams['axes.labelsize'] = 12 matplotlib.rcParams['axes.titlesize'] = 8 matplotlib.rcParams['xtick.labelsize'] = 10 matplotlib.rcParams['ytick.labelsize'] = 10 matplotlib.rcParams[ 'figure.subplot.top'] = 0.79 # single figure plots have a larger title so require more space at the top. self.x_axes_ticks = P.arange(0, 25, 2) elif ((number_of_subplots >= 2) and (number_of_subplots <= 6)): self.num_rows = 2 self.main_title_fontsize = 18 matplotlib.rcParams['legend.fontsize'] = 10 matplotlib.rcParams['axes.labelsize'] = 10 matplotlib.rcParams['axes.titlesize'] = 11 matplotlib.rcParams['xtick.labelsize'] = 8 matplotlib.rcParams['ytick.labelsize'] = 8 self.x_axes_ticks = P.arange(0, 25, 4) else: self.num_rows = 3 self.main_title_fontsize = 16 matplotlib.rcParams['legend.fontsize'] = 8 matplotlib.rcParams['axes.labelsize'] = 8 matplotlib.rcParams['axes.titlesize'] = 10 matplotlib.rcParams['xtick.labelsize'] = 6 matplotlib.rcParams['ytick.labelsize'] = 6 self.x_axes_ticks = P.arange(0, 25, 4) self.num_cols = int( math.ceil(float(number_of_subplots) / float(self.num_rows))) self.fig = Figure(figsize=(7, 6.5)) self.main_title_label = self.fig.suptitle( plot_label + unicode(self.image_defs['title'], 'utf-8'), fontsize=self.main_title_fontsize) for chan_grp in plot_groups: (group_name, group_info, fot, muf, hpf, image_buffer) = self.df.get_group_data(chan_grp) ax = self.fig.add_subplot(self.num_rows, self.num_cols, plot_groups.index(chan_grp) + 1) self.subplots.append(ax) if number_of_subplots > 4: #save a little space by only labelling the outer edges of the plot ax.label_outer() _sign = '+' if (time_zone >= 0) else '' self.x_label = ax.set_xlabel( _('Time (UTC%(sig)s%(tz)s)') % { 'sig': _sign, 'tz': time_zone }) self.y_label = ax.set_ylabel(_('Frequency (MHz)')) ## Autoscale y (frequency axis) if (plot_max_freq == self.AUTOSCALE): y_max = math.ceil(max(muf) / 5.0) * 5.0 y_max = min(plot_max_freq, 30.0) y_max = max(plot_max_freq, 5.0) else: y_max = math.ceil(plot_max_freq / 5.0) * 5.0 #resize the image image_buffer = image_buffer[0:y_max - 1, :] y_ticks = [2, 5] for y_tick_value in P.arange(10, y_max + 1, 5): y_ticks.append(y_tick_value) ax.plot(range(0, 25), muf, 'r-', range(0, 25), fot, 'g-') ax.set_ylim([2, y_max]) ax.set_xticks(self.x_axes_ticks) ax.set_yticks(y_ticks) self.add_legend(ax) title_str = group_info.strip() if number_of_subplots > 1: title_str = self.get_small_title(title_str) self.subplot_title_label = ax.set_title(title_str, multialignment='left', **self.mono_font) if (self.data_type > 0): im = ax.imshow(image_buffer, interpolation='bicubic', extent=(0, 24, 2, y_max), origin='lower', cmap=color_map, alpha=0.95, norm=P.Normalize(clip=False, vmin=self.image_defs['min'], vmax=self.image_defs['max'])) if plot_contours: ax.contour(image_buffer, self.image_defs['y_labels'], extent=(0, 24, 2, y_max), linewidths=1.0, colors='k', alpha=0.6) if plot_bands: for a, b in plot_bands: ax.axhspan(a, b, alpha=0.5, ec='k', fc='k') if (self.data_type > 0): self.cb_ax = self.fig.add_axes(self.get_cb_axes()) self.fig.colorbar(im, cax=self.cb_ax, orientation='vertical', format=P.FuncFormatter( eval('self.' + self.image_defs['formatter']))) for t in self.cb_ax.get_yticklabels(): t.set_fontsize(colorbar_fontsize) canvas = FigureCanvasGTKAgg(self.fig) self.fig.canvas.mpl_connect('draw_event', self.on_draw) canvas.show() if save_file: self.save_plot(canvas, save_file) if not self.run_quietly: # TODO consider using a scrolled pane here... dia = VOAPlotWindow('pythonProp - ' + self.image_defs['title'], canvas, parent, dpi=self.dpi) return
condition = ((ur >= urbins[urbidx]) & (ur <= urbins[urbidx]+urbinsize)) coloridx = urcolors[urbidx] pylab.plot(gi[condition], shifts[f][condition], coloridx+'.') """ for metidx in range(len(metbins)): condition = ((metallicity >= metbins[metidx]) & (metallicity <= metbins[metidx] + metbinsize)) coloridx = urcolors[metidx] pylab.plot(gi[condition], shifts[f][condition], coloridx + '.') pylab.xlabel("g-i") pylab.ylabel(r"$\Delta$ %s (mmag)" % (f)) def axis_formatter(x, pos): return "%.1f" % (x) formatter = pylab.FuncFormatter(axis_formatter) ax.yaxis.set_major_formatter(formatter) if f == 'u': pylab.ylim(-1, 1) #elif f=='g': # pylab.ylim(-5, 3) #elif f=='i': # pylab.ylim(-1, 1) elif f == 'y': pylab.ylim(-2, 1) else: pylab.ylim(-1, 1) pylab.grid(True) i = i + 1 #pylab.figtext(0.2, 0.95, "Change in magnitude for %s atmosphere and filter shift of %s" %(atmo_choice, shift_perc, "%")) pylab.figtext(
def setYFormatter(self, formatter): """ Set the y-axis formatter used by this plot to the given function. """ self.yFormatter = pylab.FuncFormatter(formatter)
def setMeterFormat(self): self.subplot.xaxis.set_major_formatter( PL.FuncFormatter(self.meter_format)) self.subplot.yaxis.set_major_formatter( PL.FuncFormatter(self.meter_format))