def term_sheet_to_db(self, window=30, windows=[30, 60, 90, 120], quantiles=[0.25, 0.75], bins=100, normed=True, bench='SPY', open=False): ext = '.json' cones_fig, cones_plt = self.cones(windows=windows, quantiles=quantiles) rolling_quantiles_fig, rolling_quantiles_plt = self.rolling_quantiles( window=window, quantiles=quantiles) rolling_extremes_fig, rolling_extremes_plt = self.rolling_extremes( window=window) rolling_descriptives_fig, rolling_descriptives_plt = self.rolling_descriptives( window=window) histogram_fig, histogram_plt = self.histogram(window=window, bins=bins, normed=normed) cones_fig_dict = mpld3.fig_to_dict(cones_fig) rolling_quantiles_fig_dict = mpld3.fig_to_dict(rolling_quantiles_fig) rolling_extremes_fig_dict = mpld3.fig_to_dict(rolling_extremes_fig) rolling_descriptives_fig_dict = mpld3.fig_to_dict( rolling_descriptives_fig) histogram_fig_dict = mpld3.fig_to_dict(histogram_fig) pyplt.close(cones_fig) pyplt.close(rolling_quantiles_fig) pyplt.close(rolling_extremes_fig) pyplt.close(rolling_descriptives_fig) pyplt.close(histogram_fig)
def show_total(values, options, toSend={}, show=True): values = np.real(values) total_trans = (values[:, 0].sum(axis=1) + values[:, 1].sum(axis=1)) / 2 total_ref = (values[:, 2].sum(axis=1) + values[:, 3].sum(axis=1)) / 2 points = np.linspace(options['divisions_start'], options['divisions_end'], options['divisions']) fig3 = plt.figure(3, figsize=(6.75, 5.55)) plt.title("Total transmission and reflection") plt.plot(points, total_trans, 'r-', label='Total transmission') plt.plot(points, total_ref, 'b-', label='Total reflection ') plt.xlabel(options['dependence']['label']) max = np.array([total_trans, total_ref]).max() plt.axis([options['divisions_start'], options['divisions_end'], 0, 1.4 * max]) plt.legend(loc='upper right') plt.grid(True) plt.tight_layout() if show: plt.show() fig4 = plt.figure(4, figsize=(6.75, 5.55)) plt.title("Total transmission and reflection and absorbance") plt.plot(points, total_ref + total_trans, 'g-', label='Total transmission + reflection') plt.plot(points, 1 - total_ref - total_trans, 'y-', label='Absorbance') plt.xlabel(options['dependence']['label']) plt.axis([options['divisions_start'], options['divisions_end'], 0, 1.4]) plt.legend(loc='upper right') plt.grid(True) plt.tight_layout() if show: plt.show() toSend.graphs['total_trans_ref'] = mpld3.fig_to_dict(fig3) toSend.graphs['total_abs'] = mpld3.fig_to_dict(fig4)
def plot_data_join_metrics(self, num_buckets=30): res = es.query_data_join_metrics(self._job.name, num_buckets) if not res['aggregations']['OVERALL']['buckets']: raise NotFoundException() metrics = [] # plot pie chart for overall join rate overall = res['aggregations']['OVERALL']['buckets'][0] labels = ['joined', 'fake', 'unjoined'] sizes = [ overall['JOINED']['doc_count'], overall['FAKE']['doc_count'], overall['UNJOINED']['value'] ] fig = Figure() ax = fig.add_subplot(111) ax.pie(sizes, labels=labels, autopct='%1.1f%%') metrics.append(mpld3.fig_to_dict(fig)) # plot stackplot for event time by_et = res['aggregations']['EVENT_TIME']['buckets'] et_index = [self._to_datetime(buck['key']) for buck in by_et] et_joined = [buck['JOINED']['doc_count'] for buck in by_et] et_faked = [buck['FAKE']['doc_count'] for buck in by_et] et_unjoined = [buck['UNJOINED']['value'] for buck in by_et] fig = Figure() ax = fig.add_subplot(111) ax.stackplot(et_index, et_joined, et_faked, et_unjoined, labels=labels) twin_ax = ax.twinx() twin_ax.patch.set_alpha(0.0) et_rate = [buck['JOIN_RATE']['value'] for buck in by_et] twin_ax.plot(et_index, et_rate, label='join rate', color='black') ax.xaxis_date() ax.legend() metrics.append(mpld3.fig_to_dict(fig)) # plot processing time vs event time by_pt = res['aggregations']['PROCESS_TIME']['buckets'] pt_index = [self._to_datetime(buck['key']) for buck in by_pt] pt_min = [ self._to_datetime(buck['MIN_EVENT_TIME']['value']) \ for buck in by_pt] pt_max = [ self._to_datetime(buck['MAX_EVENT_TIME']['value']) \ for buck in by_pt] fig = Figure() ax = fig.add_subplot(111) ax.plot(pt_index, pt_min, label='min event time') ax.plot(pt_index, pt_max, label='max event time') ax.xaxis_date() ax.yaxis_date() ax.legend() metrics.append(mpld3.fig_to_dict(fig)) return metrics
def show_comprehensive(values, options, toSend={}, show=True): values = np.real(values) points = np.linspace(options['divisions_start'], options['divisions_end'], options['divisions']) fig1 = plt.figure(10, figsize=(6.75, 5.55)) plt.title("Zero orders - Reflection") plt.plot(points, values[:, 0, 0], 'r-', label='Rs') plt.plot(points, values[:, 1, 0], 'b-', label='Rp') plt.xlabel(options['dependence']['label']) max = np.array([values[:, 0, 0], values[:, 1, 0]]).max() plt.axis([options['divisions_start'], options['divisions_end'], 0, 1.4 * max]) plt.legend(loc='upper right') plt.grid(True) plt.tight_layout() if show: plt.show() fig2 = plt.figure(11, figsize=(6.75, 5.55)) plt.title("Negative orders - Reflection") plt.plot(points, values[:, 0, 2], 'r-', label='Rs -1') plt.plot(points, values[:, 0, 4], 'r--', label='Rs -2') plt.plot(points, values[:, 0, 6], 'r-.', label='Rs -3') plt.plot(points, values[:, 1, 2], 'b-', label='Rp -1') plt.plot(points, values[:, 1, 4], 'b--', label='Rp -2') plt.plot(points, values[:, 1, 6], 'b-.', label='Rp -3') plt.xlabel(options['dependence']['label']) max = np.array( [values[:, 0, 2], values[:, 0, 4], values[:, 0, 6], values[:, 1, 2], values[:, 1, 4], values[:, 1, 6]]).max() plt.axis([options['divisions_start'], options['divisions_end'], 0, 1.4 * max]) plt.legend(loc='upper right') plt.grid(True) plt.tight_layout() if show: plt.show() fig3 = plt.figure(12, figsize=(6.75, 5.55)) plt.title("Positive orders - Reflection") plt.plot(points, values[:, 0, 1], 'r-', label='Rs 1') plt.plot(points, values[:, 0, 3], 'r--', label='Rs 2') plt.plot(points, values[:, 0, 5], 'r-.', label='Rs 3') plt.plot(points, values[:, 1, 1], 'b-', label='Rp 1') plt.plot(points, values[:, 1, 3], 'b--', label='Rp 2') plt.plot(points, values[:, 1, 5], 'b-.', label='Rp 3') plt.xlabel(options['dependence']['label']) max = np.array( [values[:, 0, 1], values[:, 0, 3], values[:, 0, 5], values[:, 1, 1], values[:, 1, 3], values[:, 1, 5]]).max() plt.axis([options['divisions_start'], options['divisions_end'], 0, 1.4 * max]) plt.legend(loc='upper right') plt.grid(True) plt.tight_layout() if show: plt.show() toSend.graphs['comp_zero'] = mpld3.fig_to_dict(fig1) toSend.graphs['comp_neg'] = mpld3.fig_to_dict(fig2) toSend.graphs['comp_pos'] = mpld3.fig_to_dict(fig3)
def plot_test(): db=get_db() username=session['username'] print username data_raw=get_db_user_data_dic(username,db) data=data_pack_hist(data_raw) fig=plt.figure() plt.hist(data) json01 = json.dumps(mpld3.fig_to_dict(fig)) json02 = json.dumps(mpld3.fig_to_dict(fig)) json03 = json.dumps(mpld3.fig_to_dict(fig)) return render_template('page_mld3.html',json1=json01,json2=json02,json3=json03)
def plot_data_join_metrics(self, num_buckets=30): res = es.query_data_join_metrics(self._job.name, num_buckets) time_res = es.query_time_metrics(self._job.name, num_buckets, index='data_join*') metrics = [] if not res['aggregations']['OVERALL']['buckets']: return metrics # plot pie chart for overall join rate overall = res['aggregations']['OVERALL']['buckets'][0] labels = ['joined', 'fake', 'unjoined'] sizes = [ overall['JOINED']['doc_count'], overall['FAKE']['doc_count'], overall['UNJOINED']['doc_count'] ] fig = Figure() ax = fig.add_subplot(111) ax.pie(sizes, labels=labels, autopct='%1.1f%%') metrics.append(mpld3.fig_to_dict(fig)) # plot stackplot for event time by_et = res['aggregations']['EVENT_TIME']['buckets'] et_index = [self._to_datetime(buck['key']) for buck in by_et] et_joined = [buck['JOINED']['doc_count'] for buck in by_et] et_faked = [buck['FAKE']['doc_count'] for buck in by_et] et_unjoined = [buck['UNJOINED']['doc_count'] for buck in by_et] fig = Figure() ax = fig.add_subplot(111) ax.stackplot(et_index, et_joined, et_faked, et_unjoined, labels=labels) twin_ax = ax.twinx() twin_ax.patch.set_alpha(0.0) et_rate = [buck['JOIN_RATE']['value'] for buck in by_et] et_rate_fake = [buck['JOIN_RATE_WITH_FAKE']['value'] for buck in by_et] twin_ax.plot(et_index, et_rate, label='join rate', color='black') twin_ax.plot(et_index, et_rate_fake, label='join rate w/ fake', color='#8f8f8f') # grey color ax.xaxis_date() ax.legend() metrics.append(mpld3.fig_to_dict(fig)) # plot processing time vs event time fig_dict = self._plot_pt_vs_et(time_res) metrics.append(fig_dict) return metrics
def plot_dashboard(self, opt_dash_df, delta_sat, plot_type='show'): """ Args: opt_dash_df (dataframe): this is the optimization dash df delta_sat (float): this is the change in SAT plot_type (str): this is the nature of the plot which is going to have the following possible outputs: - 'show': will show the plot - 'html': will return html as a string - 'json': will return the plot as json - 'mpld3': will show the plot in the browser Returns: plot_output (any of {str, dict}): returns an html string, None, or a, plot dict, depending on the requested output """ fig = plt.figure() ax = fig.add_subplot(111) title = 'Quarterly Cost Avoidance ' title += 'when Delta SAT = {} degC'.format(delta_sat) plt.title(title) opt_dash_df['cap'].plot.barh(ax=ax) plt.ylabel('CRAH Unit') plt.xlabel('Amount in CAD') if plot_type == 'show': plt.show() elif plot_type == 'html': plot_output = mpld3.fig_to_html(fig) return plot_output elif plot_type == 'json': plot_output = mpld3.fig_to_dict(fig) return plot_output elif plot_type == 'mpld3': mpld3.show()
def ReuseRecycledrawfigs(data): fig, ax = plt.subplots() with open('neighhydro.csv', 'wb') as f: w = csv.DictWriter(f, data.keys()) w.writeheader() w.writerow(data) print data model = pysd.read_vensim ('IWRET_13.mdl') for k,v in data.items(): if v == 'on' or k=='undefined' or k=='formId': del data[k] modeldata=model.run(params=data, return_columns=['RH Total Water Harvested']) glist = ['RH Total Water Harvested','IA Runoff Daily','IA Runoff Total', 'GWS Tank Inflow','Water Reuse Daily','Result RR LCC', 'Result RR GHG Emissions','Result RR Energy'] dict_of_plots=list() x1 = range(7301) y1 = modeldata['LU Sum'] indata=pd.DataFrame(x1,y1) indata.plot(ax=ax) single_chart=dict() single_chart['id']="NeighHyd1" single_chart['json']=json.dumps(mpld3.fig_to_dict(fig)) dict_of_plots.append(single_chart) return render_template("results.html", dict_of_plots=dict_of_plots)#snippet=plot_snippet)
def delete(id): point_to_delete = Data.query.get_or_404(id) try: db.session.delete(point_to_delete) db.session.commit() #return redirect('/') except: return 'There was a problem deleting that point' global reg points = Data.query.order_by(Data.date_created).all() try: g, f, r = prepare_graph(reg) json1 = json.dumps(mpld3.fig_to_dict(g)) except: return render_template('index.html', points=points, json1=None, regstatus=reg) print(f) print(r) return render_template('index.html', points=points, json1=json1, regstatus=reg, function=f, r2=r)
def mpld3ify(fig, sanitize=True, jsonify=True): ''' Do all the processing steps that might be necessary to render a figure displayable ''' import mpld3 # Do this here since currently a bug that sets the wrong backend # Nothing to do if already a string if sc.isstring(fig): return fig # If it's empty or null, make a small figure if not fig: fig = pl.figure(figsize=(1, 1)) # Make a dummy plot since no legend fig.add_subplot(111) # So axis commands work fig.get_axes()[0].set_visible(False) # Turn off axes # Convert to mpld3 -- this is the big step if isinstance(fig, pl.Figure): fig = mpld3.fig_to_dict(fig) # Optionally do additional sanitization if sanitize: fig = sc.sanitizejson(fig) if jsonify and not sc.isstring(fig): fig = json.dumps(fig) # Could cause problems to jsonify a string return fig
def main(): #get our data as an array from read_in() coords = read_in() x = list() y = list() z = list() i = 0 while i < len(coords): x.append(coords[i]['coords']['x']) y.append(coords[i]['coords']['y']) z.append(coords[i]['coords']['z']) i += 1 mynegy = [-coord for coord in y] im = plt.imread("./public/assets/miramar.png") fig, ax = plt.subplots() ax.imshow(im, extent=[0, 820000, -820000, 0], alpha=1, zorder=1) # ax.scatter(x, mynegy, c=z, alpha=1, zorder=2) ax.plot(x, mynegy, c='b', alpha=1, zorder=3) axes = plt.gca() axes.set_xlim([-200000, 1000000]) axes.set_ylim([-1000000, 200000]) print(json.dumps(mpld3.fig_to_dict(fig)))
def makeHMIGraph(noaaNmbr): fig, ax = plt.subplots(figsize=(6,6)) for i in range(len(dateAndTimeHMI)): xStart = lonMin[i] yStart = latMin[i] xLength = lonMax[i] - lonMin[i] yLength = latMax[i] - latMin[i] ax.add_patch(Rectangle((xStart, yStart), xLength, yLength, edgecolor = 'blue', facecolor='none')) ax.add_patch(Circle((0, 0), 980 , facecolor='none', edgecolor=(0, 0.8, 0.8), linewidth=3, alpha=0.5)) ax.set_xlabel('X-Cen (HPC Arcseconds)') ax.set_ylabel('Y-Cen (HPC Arcseconds)') plt.axis('equal') ax.grid(True) plt.gca().set_aspect('equal', adjustable='box') ax.set_title("HMI Dataseries:", size=30) js_data = json.dumps(mpld3.fig_to_dict(fig)) return js_data
def update_figure(self, rnd_draws): # regenerate matplotlib plot self.ax1.cla() self.ax1.set_xlabel('r1') self.ax1.set_ylabel('Normalized Distribtuion') self.ax1.set_xlim(0, 1) self.ax1.set_ylim(0, 1.5) self.ax1.grid(True) self.ax1.hist( [r[0] for r in rnd_draws], 50, normed=1, facecolor='green', alpha=0.75 ) self.ax2.cla() self.ax2.set_xlabel('r2') self.ax2.set_ylabel('Normalized Distribtuion') self.ax2.set_xlim(0, 1) self.ax2.set_ylim(0, 1.5) self.ax2.grid(True) self.ax2.hist( [r[1] for r in rnd_draws], 50, normed=1, facecolor='blue', alpha=0.75 ) # send new matplotlib plots to frontend self.emit('mpld3canvas', mpld3.fig_to_dict(self.fig))
def comp_outcome(outcome): fig, ax = plt.subplots() fig.set_size_inches(8, 6) labels_new = [] if outcome == "WON": points = ax.plot(winner_list, 'or', alpha=1,markersize=12, markeredgewidth=1, color = 'red') for i in range(len(df)): if df.iloc[i]["Outcome of Bid"] == "Won Tender Bid": label = df.iloc[[i], :].T label.columns = ['Project {0}'.format(i)] labels_new.append(str(label.to_html())) elif outcome == "LOST": points = ax.plot(losing_list, 'or', alpha=1,markersize=12, markeredgewidth=1, color = 'black') for i in range(len(df)): if df.iloc[i]["Outcome of Bid"] == "Lost Tender Bid": label = df.iloc[[i], :].T label.columns = ['Project {0}'.format(i)] labels_new.append(str(label.to_html())) elif outcome == 'ALL': points = ax.plot(y, 'or', alpha=0.7,markersize=12, markeredgewidth=1, color = 'orange') labels_new = labels tooltip = plugins.PointHTMLTooltip(points[0], labels_new, voffset=10, hoffset=10, css=css) plugins.connect(fig, tooltip) plugins.connect(fig, DragPlugin(points[0])) json01 = json.dumps(mpld3.fig_to_dict(fig)) return(json01)
def draw_spectrum(spectrum, dummy=False): rmf = pf.open('rmf_62bands.fits') src_spectrum = spectrum[8].data E_min = rmf[3].data['E_min'] E_max = rmf[3].data['E_max'] msk = src_spectrum['RATE'] > 0. y = src_spectrum['RATE'] / (E_max - E_min) x = (E_max + E_min) dx = np.log10(np.e) * (E_max - E_min) / x x = np.log10(x) dy = src_spectrum['STAT_ERR'] / (E_max - E_min) dy = np.log10(np.e) * dy / y y = np.log10(y) fig, ax = plt.subplots(figsize=(4, 2.8)) ax.set_xlabel('log(E) keV') ax.set_ylabel('log(counts/s/keV)') ax.errorbar(x[msk], y[msk], yerr=dy[msk] * 0.5, xerr=dx[msk] * 0.5, fmt='o') #print (x,y,dy) plugins.connect(fig, plugins.MousePosition(fontsize=14)) return mpld3.fig_to_dict(fig)
def filesize_plot(filesizes): # Pull all filesizes sizes = [] while True: size = yield marv.pull(filesizes) if size is None: break sizes.append(size) # plot fig = plt.figure() axis = fig.add_subplot(1, 1, 1) axis.plot(sizes, 'bo') # EE: save figure to file plotfile = yield marv.make_file('filesizes.json') with open(plotfile.path, 'w') as f: json.dump(mpld3.fig_to_dict(fig), f) # EE: create plot widget referencing file widget = { 'title': 'Filesizes', 'mpld3': 'marv-partial:{}'.format(plotfile.relpath), } # Alternative code for community edition #plotfile = yield marv.make_file('filesizes.jpg') #fig.savefig(plotfile.path) #widget = { # 'title': 'Filesizes', # 'image': {'src': plotfile.relpath}, #} yield marv.push(widget)
def get_daily_data(company): '''Data retrieval from the stock market using Alpha Vantage API for intraday and daily time series''' plt.close() # ts = TimeSeries(key='EZ3UZE4SWE1JI2A9', output_format='pandas') # data, meta_data = ts.get_daily(symbol="NSE:"+company, outputsize="compact") data = quandl.get("NSE/" + company) data = data.dropna(axis=0, how='any') # print(data) fig_size = plt.rcParams["figure.figsize"] fig_size[0] = 12 fig_size[1] = 7 plt.rcParams["figure.figsize"] = fig_size # print(data.index) fig, ax = plt.subplots() ax.plot(data.index, data['Close']) # ax.plot(data.index, data['close']) # data['close'].plot() ax.set_title('Daily Times Series for the ' + company + ' stock') ax.set_xticklabels(data.index, rotation=90, ha='left', fontsize=10) # fig.autofmt_xdate() # ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d') # plt.savefig("/Users/rjmac/Desktop/SahanaMD_Project/FrontEnd/src/assets/"+company+"_daily.png", dpi = 1000) # mpld3.save_html(fig, company+'_daily_mpld3.html') # html_string_daily = mpld3.fig_to_html(fig) dictionary_rep = mpld3.fig_to_dict(fig) # mpld3.save_json(fig,company+'_daily_mpld3_json.json') # print(dictionary_rep) plt.close() # plt.show() '''Data retrieval complete''' return data, dictionary_rep
def mdsGraph(): json = request.get_json() # lists=[] # for row in json: # aa=[row["Open"],row["Close"],row["Change"],row["Volume"]] # #print aa # lists.append(aa) # x =[] # clusters=json["clusters"] # tmp=[0]*clusters[0] # x.extend(tmp) # tmp=[50]*clusters[1] # x.extend(tmp) # tmp=[100]*clusters[2] # x.extend(tmp) # x=np.array(x) x = colorArray(json["clusters"]) a = np.array(json["result"]).astype(np.float) Y = manifold.MDS(2, max_iter=100, n_init=4).fit_transform(a) fig = plt.figure() plt.scatter(Y[:, 0], Y[:, 1], c=x) plt.xlabel('x_values') plt.ylabel('y_values') plt.xlim([-4, 4]) plt.ylim([-4, 4]) plt.title('MDS Graph') # X_iso = manifold.Isomap(10, n_components=2).fit_transform(mlab_pca.Y) # #mpld3.show() # print mpld3.fig_to_dict(fig) return jsonify(result=mpld3.fig_to_dict(fig))
def pcaPlot(): print 1 json = request.get_json() # lists=[] # for row in json: # aa=[row["Open"],row["Close"],row["Change"],row["Volume"]] # #print aa # lists.append(aa) print 2 x = colorArray(json["clusters"]) a = np.array(json["result"]).astype(np.float) fig = plt.figure() dims = random.sample(range(0, 3), 2) plt.scatter(a[:, dims[0]], a[:, dims[1]], c=x) plt.xlabel('x_values') plt.ylabel('y_values') plt.xlim([-4, 4]) plt.ylim([-4, 4]) plt.title('Scree Plot of PCA') # X_iso = manifold.Isomap(10, n_components=2).fit_transform(mlab_pca.Y) # #mpld3.show() # print mpld3.fig_to_dict(fig) return jsonify(result=mpld3.fig_to_dict(fig))
def pcaGraph(): json = request.get_json() # lists=[] # for row in json: # aa=[row["Open"],row["Close"],row["Change"],row["Volume"]] # #print aa # lists.append(aa) x = colorArray(json["clusters"]) a = np.array(json["result"]).astype(np.float) # print a fig = plt.figure() plt.scatter(a[:, 0], a[:, 1], c=x) plt.xlabel('x_values') plt.ylabel('y_values') plt.xlim([-4, 4]) plt.ylim([-4, 4]) plt.title('PCA Graph') # X_iso = manifold.Isomap(10, n_components=2).fit_transform(mlab_pca.Y) # mpld3.show() # print mpld3.fig_to_dict(fig) return jsonify(result=mpld3.fig_to_dict(fig))
def _plot_pt_vs_et(self, res): # plot process time vs event time by_pt = res['aggregations']['PROCESS_TIME']['buckets'] pt_index = [self._to_datetime(buck['key']) for buck in by_pt] pt_min = [ self._to_datetime(buck['MIN_EVENT_TIME']['value']) \ for buck in by_pt] pt_max = [ self._to_datetime(buck['MAX_EVENT_TIME']['value']) \ for buck in by_pt] fig = Figure() ax = fig.add_subplot(111) pt_index = [ idx for idx, time in zip(pt_index, pt_min) if time is not None ] ax.plot(pt_index, list(filter(lambda x: x is not None, pt_min)), label='min event time') ax.plot(pt_index, list(filter(lambda x: x is not None, pt_max)), label='max event time') ax.xaxis_date() ax.yaxis_date() ax.legend() return mpld3.fig_to_dict(fig)
def run(self): # Pass DataFrame itself into task? # Pointless to read url, write to csv, then read csv alertsdata = pandas.read_csv(self.input().path) alertsdata.columns = [x.replace('#','').strip().lower() for x in alertsdata.columns.values.tolist()] ra = np.array(alertsdata['radeg']) dec = np.array(alertsdata['decdeg']) mag = np.array(alertsdata['alertmag']) alclass = np.array(alertsdata['class']) cmap = mpl.cm.rainbow classes = list(set(alclass)) colours = {classes[i]: cmap(i / float(len(classes))) for i in range(len(classes))} fig = plt.figure() for i in range(len(ra)): plt.plot(ra[i], dec[i], 'o', ms=self.magtopoint(mag[i], mag), color=colours[alclass[i]]) plt.xlabel('Right Ascension') plt.ylabel('Declination') with open(self.output().path, 'w') as out: json.dump(mpld3.fig_to_dict(fig), out)
def plot_tree_metrics(self): metric_list = [ 'acc', 'auc', 'precision', 'recall', 'f1', 'ks', 'mse', 'msre', 'abs' ] metrics = [] aggregations = es.query_tree_metrics(self._job.name, metric_list) for name in metric_list: train_ = aggregations[name.upper()]['TRAIN']['TOP']['hits']['hits'] eval_ = aggregations[name.upper()]['EVAL']['TOP']['hits']['hits'] if len(train_) == 0 and len(eval_) == 0: continue fig = Figure() ax = fig.add_subplot(111) if len(train_) > 0: train_metric = [(item['_source']['tags']['iteration'], item['_source']['value']) for item in train_] ax.plot(*zip(*train_metric), label='train', color='blue') if len(eval_) > 0: eval_metric = [(item['_source']['tags']['iteration'], item['_source']['value']) for item in eval_] ax.plot(*zip(*eval_metric), label='eval', color='red') ax.legend() ax.set_title(name) ax.set_xlabel('iteration') ax.set_ylabel('value') metrics.append(mpld3.fig_to_dict(fig)) return metrics
def _figure_data(self, plot, fmt='png', bbox_inches='tight', **kwargs): """ Render matplotlib figure object and return the corresponding data. Similar to IPython.core.pylabtools.print_figure but without any IPython dependency. """ fig = plot.state if self.mode == 'nbagg': manager = plot.comm.get_figure_manager() if manager is None: return '' self.counter += 1 manager.show() return '' elif self.mode == 'mpld3': import mpld3 fig.dpi = self.dpi mpld3.plugins.connect(fig, mpld3.plugins.MousePosition(fontsize=14)) if fmt == 'json': return mpld3.fig_to_dict(fig) else: figid = "fig_el" + plot.comm.id if plot.comm else None html = mpld3.fig_to_html(fig, figid=figid) html = "<center>" + html + "<center/>" if plot.comm: comm, msg_handler = self.comms[self.mode] msg_handler = msg_handler.format(comm_id=plot.comm.id) return comm.template.format(init_frame=html, msg_handler=msg_handler, comm_id=plot.comm.id) return html traverse_fn = lambda x: x.handles.get('bbox_extra_artists', None) extra_artists = list( chain(*[ artists for artists in plot.traverse(traverse_fn) if artists is not None ])) kw = dict(format=fmt, facecolor=fig.get_facecolor(), edgecolor=fig.get_edgecolor(), dpi=self.dpi, bbox_inches=bbox_inches, bbox_extra_artists=extra_artists) kw.update(kwargs) # Attempts to precompute the tight bounding box try: kw = self._compute_bbox(fig, kw) except: pass bytes_io = BytesIO() fig.canvas.print_figure(bytes_io, **kw) data = bytes_io.getvalue() if fmt == 'svg': data = data.decode('utf-8') return data
def filesize_plot(filesizes): # Pull all filesizes sizes = [] while True: size = yield marv.pull(filesizes) if size is None: break sizes.append(size) # plot with plotly fig = go.Figure(data=go.Scatter(y=sizes)) # save plotly figure to file plotfile = yield marv.make_file('filesizes_plotly.json') Path(plotfile.path).write_text(fig.to_json()) # create plotly widget referencing file widget_plotly = { 'title': 'Filesizes (plotly)', 'plotly': f'marv-partial:{plotfile.relpath}', } # plot with matplotlib fig = plt.figure() axis = fig.add_subplot(1, 1, 1) axis.plot(sizes, 'bo') # save mpld3 figure to file plotfile = yield marv.make_file('filesizes_mpld3.json') Path(plotfile.path).write_text(json.dumps(mpld3.fig_to_dict(fig))) # create mpld3 plot widget referencing file widget_mpld3 = { 'title': 'Filesizes (mpld3)', 'mpld3': f'marv-partial:{plotfile.relpath}', } # save plot as image plotfile = yield marv.make_file('filesizes.jpg') fig.savefig(plotfile.path) # create image widget referencing file widget_image = { 'title': 'Filesizes (image)', 'image': { 'src': plotfile.relpath }, } # Let the user choose which widget to show with a dropdown yield marv.push({ 'title': 'Filesize plots', 'dropdown': { 'widgets': [ widget_plotly, widget_mpld3, widget_image, ], }, })
def chart_template_direct(request, disease_id, state_id): return HttpResponse(request_url) js_data = json.dumps(mpld3.fig_to_dict(fig)) return render_to_response('direct_plot.html', {"my_data": js_data})
def filesize_plot_fixed(filesizes): # set_header() helps marv to schedule nodes yield marv.set_header() # Pull all filesizes sizes = [] while True: size = yield marv.pull(filesizes) if size is None: break sizes.append(size) # plot fig = plt.figure() axis = fig.add_subplot(1, 1, 1) axis.plot(sizes, 'bo') # axis.set_xlabel('foo') # axis.set_ylabel('bat') # save figure to file plotfile = yield marv.make_file('filesizes.json') with open(plotfile.path, 'w') as f: json.dump(mpld3.fig_to_dict(fig), f) # create plot widget referencing file widget = { 'title': 'Filesizes', 'mpld3': f'marv-partial:{plotfile.relpath}', } yield marv.push(widget)
def pcaPlot(): print 1 json = request.get_json() # lists=[] # for row in json: # aa=[row["Open"],row["Close"],row["Change"],row["Volume"]] # #print aa # lists.append(aa) print 2 x=colorArray(json["clusters"]) a= np.array(json["result"]).astype(np.float) fig=plt.figure() dims=random.sample(range(0, 3), 2) plt.scatter(a[:,dims[0]],a[:,dims[1]], c=x) plt.xlabel('x_values') plt.ylabel('y_values') plt.xlim([-4,4]) plt.ylim([-4,4]) plt.title('Scree Plot of PCA') # X_iso = manifold.Isomap(10, n_components=2).fit_transform(mlab_pca.Y) # #mpld3.show() # print mpld3.fig_to_dict(fig) return jsonify(result=mpld3.fig_to_dict(fig))
def network_delay_histogram(self, app, butler, alg_label): """ Description: returns the data to network delay histogram of the time it takes to getQuery+processAnswer for each algorithm Expected input: (string) alg_label : must be a valid alg_label contained in alg_list list of dicts Expected output (in dict): (dict) MPLD3 plot dictionary """ list_of_query_dict,didSucceed,message = self.db.get_docs_with_filter(app.app_id+':queries',{'exp_uid':app.exp_uid,'alg_label':alg_label}) t = [] for item in list_of_query_dict: try: t.append(item['network_delay']) except: pass fig, ax = plt.subplots(subplot_kw=dict(axisbg='#FFFFFF')) ax.hist(t,MAX_SAMPLES_PER_PLOT,range=(0,5),alpha=0.5,color='black') ax.set_xlim(0, 5) ax.set_axis_off() ax.set_xlabel('Durations (s)') ax.set_ylabel('Count') ax.set_title(alg_label + " - network delay", size=14) plot_dict = mpld3.fig_to_dict(fig) plt.close() return plot_dict
def pcaGraph(): json = request.get_json() # lists=[] # for row in json: # aa=[row["Open"],row["Close"],row["Change"],row["Volume"]] # #print aa # lists.append(aa) x=colorArray(json["clusters"]) a= np.array(json["result"]).astype(np.float) # print a fig=plt.figure() plt.scatter(a[:,0],a[:,1], c=x) plt.xlabel('x_values') plt.ylabel('y_values') plt.xlim([-4,4]) plt.ylim([-4,4]) plt.title('PCA Graph') # X_iso = manifold.Isomap(10, n_components=2).fit_transform(mlab_pca.Y) # mpld3.show() # print mpld3.fig_to_dict(fig) return jsonify(result=mpld3.fig_to_dict(fig))
def mdsGraph(): json = request.get_json() # lists=[] # for row in json: # aa=[row["Open"],row["Close"],row["Change"],row["Volume"]] # #print aa # lists.append(aa) # x =[] # clusters=json["clusters"] # tmp=[0]*clusters[0] # x.extend(tmp) # tmp=[50]*clusters[1] # x.extend(tmp) # tmp=[100]*clusters[2] # x.extend(tmp) # x=np.array(x) x=colorArray(json["clusters"]) a= np.array(json["result"]).astype(np.float) Y = manifold.MDS(2 , max_iter=100, n_init=4).fit_transform(a) fig=plt.figure() plt.scatter(Y[:,0],Y[:,1],c=x) plt.xlabel('x_values') plt.ylabel('y_values') plt.xlim([-4,4]) plt.ylim([-4,4]) plt.title('MDS Graph') # X_iso = manifold.Isomap(10, n_components=2).fit_transform(mlab_pca.Y) # #mpld3.show() # print mpld3.fig_to_dict(fig) return jsonify(result=mpld3.fig_to_dict(fig))
def plot_grid_3d(X_range, Y_range, Z, X_label='X', Y_label='Y', Z_label='Z', json_data=False): fig = plt.figure(figsize=(18,6)) ax = fig.add_subplot(1, 1, 1, projection='3d') X, Y = np.meshgrid(X_range, Y_range) Zm = Z #zip(*Z) # print "[plot_check]", np.shape(X_range), np.shape(Y_range), np.shape(X), np.shape(Y), np.shape(Z) p = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False) x_offset = (max(X_range) - min(X_range))*0.2 y_offset = (max(Y_range) - min(Y_range))*0.2 Zmax = max(max(Zm)) Zmin = min(min(Zm)) z_offset = (Zmax - Zmin)*0.2 cset = ax.contour(X, Y, Z, zdir='x', offset=X_range[0]-x_offset, cmap=cm.coolwarm) cset = ax.contour(X, Y, Z, zdir='y', offset=Y_range[-1]+y_offset, cmap=cm.coolwarm) cset = ax.contour(X, Y, Z, zdir='z', offset=Zmin-z_offset, cmap=cm.coolwarm) ax.set_xlabel(X_label) ax.set_ylabel(Y_label) ax.set_zlabel(Z_label) ax.set_xlim(X_range[0]-x_offset, X_range[-1]) ax.set_ylim(Y_range[0], Y_range[-1]+y_offset) ax.set_zlim(Zmin-z_offset, Zmax+z_offset) cb = fig.colorbar(p, shrink=0.5) # print "[mpld3] before json serialize." if json_data: return mpld3.fig_to_dict(fig)
def api_activity_histogram(self,app_id,exp_uid,task): """ Description: returns the data to plot all API activity (for all algorithms) in a histogram with respect to time for any task in {getQuery,processAnswer,predict} Expected input: (string) task : must be in {'getQuery','processAnswer','predict'} Expected output (in dict): (dict) MPLD3 plot dictionary """ list_of_log_dict,didSucceed,message = self.ell.get_logs_with_filter(app_id+':APP-CALL',{'exp_uid':exp_uid,'task':task}) from datetime import datetime from datetime import timedelta start_date_str,didSucceed,message = self.db.get('experiments_admin',exp_uid,'start_date') start_date = utils.str2datetime(start_date_str) numerical_timestamps = [ ( utils.str2datetime(item['timestamp'])-start_date).total_seconds() for item in list_of_log_dict] import matplotlib.pyplot as plt import mpld3 fig, ax = plt.subplots(subplot_kw=dict(axisbg='#FFFFFF'),figsize=(12,1.5)) ax.hist(numerical_timestamps,int(1+4*numpy.sqrt(len(numerical_timestamps))),alpha=0.5,color='black') ax.set_frame_on(False) ax.get_xaxis().set_ticks([]) ax.get_yaxis().set_ticks([]) ax.get_yaxis().set_visible(False) ax.set_xlim(0, max(numerical_timestamps)) plot_dict = mpld3.fig_to_dict(fig) return plot_dict
def update_figure(self, rnd_draws): # regenerate matplotlib plot self.ax1.cla() self.ax1.set_xlabel('r1') self.ax1.set_ylabel('Normalized Distribtuion') self.ax1.set_xlim(0, 1) self.ax1.set_ylim(0, 1.5) self.ax1.grid(True) self.ax1.hist([r[0] for r in rnd_draws], 50, normed=1, facecolor='green', alpha=0.75) self.ax2.cla() self.ax2.set_xlabel('r2') self.ax2.set_ylabel('Normalized Distribtuion') self.ax2.set_xlim(0, 1) self.ax2.set_ylim(0, 1.5) self.ax2.grid(True) self.ax2.hist([r[1] for r in rnd_draws], 50, normed=1, facecolor='blue', alpha=0.75) # send new matplotlib plots to frontend self.emit('mpld3canvas', mpld3.fig_to_dict(self.fig))
def makeIRISGraph(noaaNmbr): fig, ax = plt.subplots(figsize = (6,6)) HEKgraph = ax.scatter(xcen, ycen, s=25, alpha = 1.0) HEKcoordinates = [] for i in range(len(xcen)): HEKcoordinates.append(str(xcen[i]) + ", " + str(ycen[i])) plt.plot(xcen, ycen, linestyle="dashed", color="red") tooltip = mpld3.plugins.PointLabelTooltip(HEKgraph, labels=HEKcoordinates) mpld3.plugins.connect(fig,tooltip) ax.add_patch(Circle((0, 0), 980 , facecolor='none', edgecolor=(0, 0.8, 0.8), linewidth=3, alpha=0.5)) ax.set_xlabel('X-Cen (HPC Arcseconds)') ax.set_ylabel('Y-Cen (HPC Arcseconds)') plt.axis('equal') ax.grid(True) plt.gca().set_aspect('equal', adjustable='box') ax.set_title("IRIS Observations:", size=30) js_data = json.dumps(mpld3.fig_to_dict(fig)) return js_data
def api_activity_histogram(self, app, butler): """ Description: returns the data to plot all API activity (for all algorithms) in a histogram with respect to time for any task in {getQuery,processAnswer,predict} Expected output (in dict): (dict) MPLD3 plot dictionary """ queries = butler.queries.get(pattern={'exp_uid': app.exp_uid}) #self.db.get_docs_with_filter(app_id+':queries',{'exp_uid':exp_uid}) start_date = utils.str2datetime( butler.admin.get(uid=app.exp_uid)['start_date']) numerical_timestamps = [ (utils.str2datetime(item['timestamp_query_generated']) - start_date).total_seconds() for item in queries ] fig, ax = plt.subplots(subplot_kw=dict(axisbg='#FFFFFF'), figsize=(12, 1.5)) ax.hist(numerical_timestamps, min(int(1 + 4 * numpy.sqrt(len(numerical_timestamps))), 300), alpha=0.5, color='black') ax.set_frame_on(False) ax.get_xaxis().set_ticks([]) ax.get_yaxis().set_ticks([]) ax.get_yaxis().set_visible(False) ax.set_xlim(0, max(numerical_timestamps)) plot_dict = mpld3.fig_to_dict(fig) plt.close() return plot_dict
def get_intraday(company): '''Data retrieval from the stock market using Alpha Vantage API for intraday and daily time series''' plt.close() ts = TimeSeries(key='EZ3UZE4SWE1JI2A9', output_format='pandas') data, meta_data = ts.get_intraday(symbol=company, interval="1min", outputsize="full") fig_size = plt.rcParams["figure.figsize"] fig_size[0] = 12 fig_size[1] = 5 plt.rcParams["figure.figsize"] = fig_size fig, ax = plt.subplots() ax.plot(data.index, data['close']) # data['close'].plot() ax.set_title('Intraday Times Series for the ' + company + ' stock (15 min)') ax.set_xticklabels(data.index, rotation=90, ha='left', fontsize=10) # dictionary_rep = mpld3.fig_to_dict(fig) # data['close'].plot() # plt.title('Intraday Times Series for the ' + company + ' stock (1 min)') dictionary_rep = mpld3.fig_to_dict(fig) # fig.autofmt_xdate() # ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d') # plt.savefig("/Users/rjmac/Desktop/SahanaMD_Project/FrontEnd/src/assets/"+company+ "_intraday.png", dpi=1000) plt.close() # plt.show() '''Data retrieval complete''' return dictionary_rep
def getPayoffProfile(request): strategyName = request.args['strategyName'] s = getStrategy(strategyName, request) fig, ax = plt.subplots() s.CalcPayoffProfile() return mpld3.fig_to_dict(fig)
def _plot_figure(self, idx): from .display_hooks import display_figure fig = self.plot[idx] if OutputMagic.options['backend'] == 'd3': import mpld3 mpld3.plugins.connect(fig, mpld3.plugins.MousePosition(fontsize=14)) return mpld3.fig_to_dict(fig) return display_figure(fig)
def _plot_figure(self, idx): from .display_hooks import display_frame self.plot.update(idx) if OutputMagic.options['backend'] == 'd3': import mpld3 mpld3.plugins.connect(fig, mpld3.plugins.MousePosition(fontsize=14)) return mpld3.fig_to_dict(self.plot.state) return display_frame(self.plot, **self.display_options)
def get_statistics_rasterplot(videoname, runname): with Run(videoname, runname) as run: run['time_per_bin'] = float(request.form['time_per_bin']) fig_raster = analyzer.plot.plot_rasterplot(run['statistics']['spikes'], run['exposure_time'], run['time_per_bin']) rasterplot = mpld3.fig_to_dict(fig_raster) return jsonify(rasterplot=rasterplot)
def on_run(self): """Run when button is pressed.""" self.emit('log', 'Hi. The run button was pressed. Going to sleep.') time.sleep(self.sleep_duration) self.emit('log', 'Waking up again. Run is done.') # draw something on self.fig # regenerate matplotlib plot self.ax.cla() self.ax.set_xlabel('output of random.random()') self.ax.set_ylabel('normalized distribtuion of 100 trials') self.ax.set_xlim(0, 1) self.ax.set_ylim(0, 1.5) self.ax.grid(True) self.ax.hist( [random.random() for i in xrange(100)], 5, normed=1, facecolor='green', alpha=0.75 ) self.emit('mpld3canvas', mpld3.fig_to_dict(self.fig)) # create the data for the Basic d3.js part data = [ {'id': 1, 'x1': 0.1, 'y1': 0.1, 'x2': 0.8, 'y2': 0.5, 'width': 0.05, 'color': 0.5}, {'id': 2, 'x1': 0.1, 'y1': 0.3, 'x2': 0.8, 'y2': 0.7, 'width': 0.05, 'color': 0.7}, {'id': 3, 'x1': 0.1, 'y1': 0.5, 'x2': 0.8, 'y2': 0.9, 'width': 0.05, 'color': 0.9}, ] self.emit('update_basic', data) # update with some new data after a short wait time.sleep(1) data2 = [ {'id': 1, 'x1': 0.1, 'y1': 0.1, 'x2': 0.8, 'y2': 0.5, 'width': 0.2, 'color': 0.5}, {'id': 2, 'x1': 0.1, 'y1': 0.3, 'x2': 0.8, 'y2': 0.7, 'width': 0.2, 'color': 0.7}, {'id': 3, 'x1': 0.1, 'y1': 0.5, 'x2': 0.8, 'y2': 0.9, 'width': 0.2, 'color': 0.9}, ] self.emit('update_basic', data2) # create and send data for the d3.js plot self.emit('log', 'Increasing numbers.') self.emit('update_plot', [0.1, 0.3, 0.5, 0.7, 0.9]) time.sleep(1) self.emit('log', 'Random numbers.') self.emit('update_plot', [random.random() for i in xrange(5)]) time.sleep(1) # Animation of a sin wave. Use numpy. self.emit('log', 'Animate a sin wave.') x = numpy.linspace(0, numpy.pi, 5) for t in xrange(50): numpy_data = 0.5 + 0.4*numpy.sin(x + t/3.0) self.emit('update_plot', numpy_data.tolist()) time.sleep(0.25)
def plot_grid_2d(X, Z, X_label='X', Z_label='Z', json_data=False): fig = plt.figure(figsize=(6,2.5)) ax = fig.add_subplot(1, 1, 1) p = ax.plot(X, Z) ax.set_xlabel(X_label) ax.set_ylabel(Z_label) if json_data: chart_data = mpld3.fig_to_dict(fig) plt.close() return chart_data
def viz_content(): params = get_params(request) df = get_events_by_content(g.db_engine, params) ax = df.plot(x='loaded', y='played', kind='scatter', figsize=(12, 8)) mpld3_data = mpld3.fig_to_dict(ax.get_figure()) url_format = lambda x: '<a href="%s">%s</a>' % (x, x) table_html = df.head(20).to_html(classes=['table'], formatters={'content_url': url_format}) return render_template('base_viz.html', \ clients=get_clients(), content_hosts=get_content_hosts(), params=params, \ data_table=table_html, mpld3_data=json.dumps(mpld3_data))
def home(request): context = {} context['requestMethod'] = request.META['REQUEST_METHOD'] if request.method == 'GET' : if request.GET.__contains__('hidden') : #if 'name' in context and 'end_date' in context and 'start_date' in context: context['name'] = request.GET['name'] context['start_date'] = request.GET['start_date'] context['end_date'] = request.GET['end_date'] print context requestContext = RequestContext(request, context) templateIndex = loader.get_template('index.html') renderedTemplate = templateIndex.render(requestContext) response = HttpResponse() response['Age'] = 120 response.write(renderedTemplate) if 'name' in context:# and 'end_date' in context and 'start_date' in context: company = context['name'].encode('ascii','ignore') start_date = context['start_date'].encode('ascii','ignore') end_date = context['end_date'].encode('ascii','ignore') a = request.GET.get('name') start_date = start_date.split('-') end_date = end_date.split('-') start = datetime.datetime(int(start_date[0]),int(start_date[1]),int(start_date[2])) end = datetime.datetime(int(end_date[0]),int(end_date[1]),int(end_date[2])) f = web.DataReader(company,'yahoo',start,end) a = f['Close'] b = a.index.tolist() array = [] for i in range(b.__len__()): c = b[i] s = str(c)[:10] d = a.tolist() e = round(d[i],2) array.append(e) #print f fig = figure(1) plot([3,1,2,4,1]) js_data = json.dumps(mpld3.fig_to_dict(fig)) return render_to_response('index.html',{'array':json.dumps(array), 'start_date':json.dumps(start_date), 'end_date':json.dumps(end_date), 'js_data': js_data}) return render_to_response('index.html',{})
def viz_date(): params = get_params(request) df = get_events_date_df(g.db_engine, params) df = df.unstack(1) ax = df.plot(legend=['load', 'play'], figsize=(12, 8)) ax.set_xlabel('Date') mpld3_data = mpld3.fig_to_dict(ax.get_figure()) table_df = get_events_by_source_df(g.db_engine, params) ratio_format = lambda x: '<span class="significant"><bold>%f</bold></span>' % x table_html = table_df.head(20).to_html(classes=['table'], formatters={'ratio': ratio_format}) return render_template('base_viz.html', \ clients=get_clients(), content_hosts=get_content_hosts(), params=params, \ data_table=table_html, mpld3_data=json.dumps(mpld3_data))
def vis_rec(data, fig_id, title=None): data = data.copy() data -= data.min() data /= data.max() plt.figure() plt.xticks(np.arange(0, 10, 1.0)) plt.yticks([]) plt.imshow(data) if title: plt.title(title) plt.tight_layout() return {'id': fig_id, 'json': json.dumps(mpld3.fig_to_dict(plt.gcf()))}
def _figure_data(self, plot, fmt='png', bbox_inches='tight', **kwargs): """ Render matplotlib figure object and return the corresponding data. Similar to IPython.core.pylabtools.print_figure but without any IPython dependency. """ fig = plot.state if self.mode == 'nbagg': manager = self.get_figure_manager(plot.state) if manager is None: return '' self.counter += 1 manager.show() return '' elif self.mode == 'mpld3': import mpld3 fig.dpi = self.dpi mpld3.plugins.connect(fig, mpld3.plugins.MousePosition(fontsize=14)) if fmt == 'json': return mpld3.fig_to_dict(fig) else: return "<center>" + mpld3.fig_to_html(fig) + "<center/>" traverse_fn = lambda x: x.handles.get('bbox_extra_artists', None) extra_artists = list(chain(*[artists for artists in plot.traverse(traverse_fn) if artists is not None])) kw = dict( format=fmt, facecolor=fig.get_facecolor(), edgecolor=fig.get_edgecolor(), dpi=self.dpi, bbox_inches=bbox_inches, bbox_extra_artists=extra_artists ) kw.update(kwargs) # Attempts to precompute the tight bounding box try: kw = self._compute_bbox(fig, kw) except: pass bytes_io = BytesIO() fig.canvas.print_figure(bytes_io, **kw) data = bytes_io.getvalue() if fmt == 'svg': data = data.decode('utf-8') return data
def show_plot_shear(span_length, x_loc, feet_or_frac, max_shear_loc): axle_pos = max_shear_loc x_loc = x_loc * span_length if feet_or_frac == 'frac' else x_loc # Plot span fig = plt.figure() ax = fig.add_subplot(1,1,1) span = ax.plot([0, span_length], [0, 0], 'b') # Add boundaries to span # tri_width = (span_length*1.2+20)/120 # bound1 = plt.plot([0, -tri_width, tri_width, 0], [0, -2, -2, 0]) bound1 = ax.plot(0, -0.7, 'b^') bound2 = ax.plot(span_length, -0.7, 'bo') axle_pos += span_length # Cooper E-80 Axle Layout axle_loads = [40, 80, 80, 80, 80, 52, 52, 52, 52, 40, 80, 80, 80, 80, 52, 52, 52, 52, 8] axle_spaces = [0, 8, 5, 5, 5, 9, 5, 6, 5, 8, 8, 5, 5, 5, 9, 5, 6, 5, 5.5] train_len = sum(axle_spaces) while (sum(axle_spaces) - train_len) < span_length: axle_spaces.append(1) axle_loads.append(8) for pos, spac in enumerate(axle_spaces): axle_pos = axle_pos - spac axle_load = 1 + axle_loads[pos] / 4 axle = ax.plot([axle_pos, axle_pos], [1, axle_load], 'r') axle_label = ax.text(axle_pos - 1.5, axle_load + 2, str(axle_loads[pos]) + 'k', fontsize=8, color='r') axle_end = ax.plot(axle_pos, 1, 'rv') xplot = ax.plot([x_loc, x_loc], [-1, -5], 'y') xplotarr = ax.plot(x_loc, -1 ,'y^') ax.set_xlim(-20, span_length + 20) ax.set_ylim(-30, 60) ax.set_title('Train Position for Max Shear') return mpld3.fig_to_dict(fig)
def test_extra_contour(self): """ Create an empty line collection by requesting invalid contours, and check that a collection with length(paths) === 0 is not passed to the JavaScript. """ fig, ax = plt.subplots() X, Y = np.mgrid[0:5, 0:5] Z = X**2 - Y**2 ax.contour(X, Y, Z, range(-15, 18, 2)) # last contour level is 17 # but Z.max() is 16 jsondict = mpld3.fig_to_dict(fig) for collection in jsondict["axes"][0]["collections"]: self.assertTrue(len(collection["paths"]) != 0) return
def star_param_scatterplot(): # Create star population # starpop = vespa.MultipleStarPopulation(1) # return starpop.prophist2d('distmod','distmod') # x = starpop['H_mag'] # y = starpop['distmod'] x = np.random.randint(200, size=100) y = np.random.randint(200, size=100) fig = plt.figure() ax = fig.add_subplot(111) #, xlabel='$distance$', ylabel='$distmod$') the_plot = ax.plot(x,y,marker='.',lw=0) f = open("mpld3_product.txt","w") json01 = json.dumps(mpld3.fig_to_dict(fig)) return render_template('test.html', testvar=json01)
def _figure_data(self, plot, fmt="png", bbox_inches="tight", **kwargs): """ Render matplotlib figure object and return the corresponding data. Similar to IPython.core.pylabtools.print_figure but without any IPython dependency. """ fig = plot.state if self.mode == "nbagg": manager = self.get_figure_manager(plot) if manager is None: return "" self.counter += 1 manager.show() return "" elif self.mode == "mpld3": import mpld3 fig.dpi = self.dpi mpld3.plugins.connect(fig, mpld3.plugins.MousePosition(fontsize=14)) if fmt == "json": return mpld3.fig_to_dict(fig) else: return "<center>" + mpld3.fig_to_html(fig) + "<center/>" kw = dict( format=fmt, facecolor=fig.get_facecolor(), edgecolor=fig.get_edgecolor(), dpi=self.dpi, bbox_inches=bbox_inches, ) kw.update(kwargs) # Attempts to precompute the tight bounding box try: kw = self._compute_bbox(fig, kw) except: pass bytes_io = BytesIO() fig.canvas.print_figure(bytes_io, **kw) data = bytes_io.getvalue() if fmt == "svg": data = data.decode("utf-8") return data
def plot_experiment(gene_data): gene_data.sort(key=operator.itemgetter(1)) filtered_genes = [gene_tuple[0] for gene_tuple in gene_data] filtered_values = [gene_tuple[1] for gene_tuple in gene_data] filtered_values.reverse() filtered_genes.reverse() fig, ax = plt.subplots(1) scatter = ax.scatter(range(len(gene_data)), filtered_values, edgecolor='none', facecolor='#ff69b4') tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=filtered_genes) mpld3.plugins.connect(fig, tooltip) # When you've made your plot, convert it with the mpld3 library like so: mpld3_dict = mpld3.fig_to_dict(fig) return mpld3_dict
def update_plot(self, path_data): codes, verts = zip(*path_data) path = mpl.path.Path(verts, codes) patch = mpl.patches.PathPatch(path, facecolor='r', alpha=0.5) self.ax.add_patch(patch) # plot control points and connecting lines x, y = zip(*path.vertices[:-1]) points = self.ax.plot(x, y, 'go', ms=10) line = self.ax.plot(x, y, '-k') self.ax.grid(True, color='gray', alpha=0.5) self.ax.axis('equal') self.ax.set_title("Drag Points to Change Path", fontsize=18) # enable custom plugin mpld3.plugins.connect(self.fig, LinkedDragPlugin(points[0], line[0], patch)) # send plot to frontend self.emit('mpld3canvas', mpld3.fig_to_dict(self.fig))
def response_time_histogram(self,app_id,exp_uid,alg_label): """ Description: returns the data to plot response time histogram of processAnswer for each algorithm Expected input: (string) alg_label : must be a valid alg_label contained in alg_list list of dicts Expected output (in dict): (dict) MPLD3 plot dictionary """ alg_list,didSucceed,message = self.db.get(app_id+':experiments',exp_uid,'alg_list') for algorithm in alg_list: if algorithm['alg_label'] == alg_label: alg_id = algorithm['alg_id'] alg_uid = algorithm['alg_uid'] list_of_query_dict,didSucceed,message = self.db.get_docs_with_filter(app_id+':queries',{'exp_uid':exp_uid,'alg_uid':alg_uid}) t = [] for item in list_of_query_dict: try: t.append(item['response_time']) except: pass import matplotlib.pyplot as plt import mpld3 fig, ax = plt.subplots(subplot_kw=dict(axisbg='#FFFFFF')) ax.hist(t,MAX_SAMPLES_PER_PLOT,range=(0,30),alpha=0.5,color='black') ax.set_xlim(0, 30) ax.set_axis_off() ax.set_xlabel('Durations (s)') ax.set_ylabel('Count') ax.set_title(alg_label + " - response time", size=14) plot_dict = mpld3.fig_to_dict(fig) plt.close() return plot_dict
def onconnect(): """Run as soon as a browser connects to this.""" fig, ax = plt.subplots() points = ax.scatter( np.random.rand(40), np.random.rand(40), s=300, alpha=0.3, ) # use the mpld3 tooltop plugin labels = ["Point {0}".format(i) for i in range(40)] tooltip = mpld3.plugins.PointLabelTooltip(points, labels) mpld3.plugins.connect(fig, tooltip) # send the plot to the frontend ANALYSIS.signals.emit('mpld3canvas', mpld3.fig_to_dict(fig)) # done ANALYSIS.signals.emit('log', {'action': 'done'})
def api_activity_histogram(self, app, butler): """ Description: returns the data to plot all API activity (for all algorithms) in a histogram with respect to time for any task in {getQuery,processAnswer,predict} Expected output (in dict): (dict) MPLD3 plot dictionary """ queries = butler.queries.get(pattern={'exp_uid':app.exp_uid}) #self.db.get_docs_with_filter(app_id+':queries',{'exp_uid':exp_uid}) start_date = utils.str2datetime(butler.admin.get(uid=app.exp_uid)['start_date']) numerical_timestamps = [(utils.str2datetime(item['timestamp_query_generated'])-start_date).total_seconds() for item in queries] fig, ax = plt.subplots(subplot_kw=dict(axisbg='#FFFFFF'),figsize=(12,1.5)) ax.hist(numerical_timestamps,min(int(1+4*numpy.sqrt(len(numerical_timestamps))),300),alpha=0.5,color='black') ax.set_frame_on(False) ax.get_xaxis().set_ticks([]) ax.get_yaxis().set_ticks([]) ax.get_yaxis().set_visible(False) ax.set_xlim(0, max(numerical_timestamps)) plot_dict = mpld3.fig_to_dict(fig) plt.close() return plot_dict
def onconnect(): """Run as soon as a browser connects to this.""" # initialize plot fig, ax = mpl.pyplot.subplots() # create plot elements path_data = [ (mpl.path.Path.MOVETO, (1.58, -2.57)), (mpl.path.Path.CURVE4, (0.35, -1.1)), (mpl.path.Path.CURVE4, (-1.75, 2.0)), (mpl.path.Path.CURVE4, (0.375, 2.0)), (mpl.path.Path.LINETO, (0.85, 1.15)), (mpl.path.Path.CURVE4, (2.2, 3.2)), (mpl.path.Path.CURVE4, (3, 0.05)), (mpl.path.Path.CURVE4, (2.0, -0.5)), (mpl.path.Path.CLOSEPOLY, (1.58, -2.57)), ] codes, verts = zip(*path_data) path = mpl.path.Path(verts, codes) patch = mpl.patches.PathPatch(path, facecolor='r', alpha=0.5) ax.add_patch(patch) # plot control points and connecting lines x, y = zip(*path.vertices[:-1]) points = ax.plot(x, y, 'go', ms=10) line = ax.plot(x, y, '-k') ax.grid(True, color='gray', alpha=0.5) ax.axis('equal') ax.set_title("Drag Points to Change Path", fontsize=18) # enable custom plugin mpld3.plugins.connect(fig, LinkedDragPlugin(points[0], line[0], patch)) # send plot to frontend ANALYSIS.signals.emit('mpld3canvas', mpld3.fig_to_dict(fig)) ANALYSIS.signals.emit('log', {'action': 'done'})
def vis_square(data, fig_id, padsize=1, padval=0, title=None): data = data.copy() data -= data.min() data /= data.max() # force the number of filters to be square n = int(np.ceil(np.sqrt(data.shape[0]))) padding = ((0, n ** 2 - data.shape[0]), (0, padsize), (0, padsize)) + ((0, 0),) * (data.ndim - 3) data = np.pad(data, padding, mode='constant', constant_values=(padval, padval)) # tile the filters into an image data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1))) data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:]) data = data.squeeze() plt.figure() plt.xticks([]) plt.yticks([]) plt.imshow(data) if title: plt.title(title) plt.tight_layout() return {'id': fig_id, 'json': json.dumps(mpld3.fig_to_dict(plt.gcf()))}