def plotdata(trendselection=None, startdate='2000-01-01', enddate='2018-01-01', trendline=False): # Handle inputs startyear = convertdate(startdate, '%Y-%m-%d') endyear = convertdate(enddate, '%Y-%m-%d') trendoptions = getoptions(tojson=False) if trendselection is None: trendselection = trendoptions.keys()[0] datatype = trendoptions[trendselection] # Make graph fig = pl.figure() fig.add_subplot(111) thesedata = df.findrows(key=datatype, col='type') years = thesedata['date'] vals = thesedata['close'] validinds = sc.findinds(pl.logical_and(years>=startyear, years<=endyear)) x = years[validinds] y = vals[validinds] pl.plot(x, y) pl.xlabel('Date') pl.ylabel('Trend index') # Add optional trendline if trendline: newy = sc.smoothinterp(x, x, y, smoothness=200) pl.plot(x, newy, lw=3) # Convert to FE graphjson = sw.mpld3ify(fig, jsonify=False) # Convert to dict return graphjson # Return the JSON representation of the Matplotlib figure
def plot_burden(project_id, burdenkey, dosave=True): ''' Plot the disease burden ''' proj = load_project(project_id) # Get the Project object. burdenset = proj.burden( key=burdenkey) # Get the burden set that matches burdenset_numindex. # Create the figures and convert to mpld3 figdicts = [] if burdenset.data: figs = burdenset.plot() for fig in figs: figdict = sw.mpld3ify(fig, jsonify=False) figdicts.append(figdict) # Optionally save the figures if dosave: filepath = get_path(filename=figures_filename, username=proj.webapp.username) sc.savefigs(figs=figs, filetype='singlepdf', filename=filepath) print('Figures saved to %s' % filepath) # Return success -- WARNING, hard-coded to 3 graphs! return { 'graph1': figdicts[0], 'graph2': figdicts[1], 'graph3': figdicts[2], } else: return None # No graphs to make
def computation(seed=0, n=1000): # Make graph pl.seed(int(seed)) fig = pl.figure() ax = fig.add_subplot(111) xdata = pl.randn(n) ydata = pl.randn(n) colors = sc.vectocolor(pl.sqrt(xdata**2+ydata**2)) ax.scatter(xdata, ydata, c=colors) # Convert to FE graphjson = sw.mpld3ify(fig, jsonify=False) # Convert to dict return graphjson # Return the JSON representation of the Matplotlib figure
def plot_packages(project_id, packagekey, dosave=True): ''' Plot the health packages ''' proj = load_project(project_id) # Get the Project object. packageset = proj.package( key=packagekey ) # Get the package set that matches packageset_numindex. # Make the plots figs = [] fig1 = packageset.plot_spending(which='current') fig2 = packageset.plot_spending(which='optimized') fig3 = packageset.plot_dalys(which='current') fig4 = packageset.plot_dalys(which='optimized') fig5 = packageset.plot_cascade() figs.append(fig1) figs.append(fig2) figs.append(fig3) figs.append(fig4) figs.append(fig5) figdicts = [] for fig in figs: figdict = sw.mpld3ify(fig, jsonify=False) figdicts.append(figdict) # Optionally save to PDF if dosave: filepath = get_path(filename=figures_filename, username=proj.webapp.username) sc.savefigs(figs=figs, filetype='singlepdf', filename=filepath) print('Figures saved to %s' % filepath) # Return success -- WARNING, should not be hard-coded! return { 'graph1': figdicts[0], 'graph2': figdicts[1], 'graph3': figdicts[2], 'graph4': figdicts[3], 'graph5': figdicts[4], }
if __name__ == '__main__': if 'blank' in torun and doplot: if doplot: sw.browser() if 'browser' in torun: figs = [] for n in [10, 50]: fig = pl.figure() pl.plot(pl.rand(n), pl.rand(n)) figs.append(fig) barfig = pl.figure() pl.bar(pl.arange(10), pl.rand(10)) barjson = sw.mpld3ify(barfig) if doplot: sw.browser(figs=figs + [barjson]) if 'advanced' in torun: def make_fig(): fig = pl.figure() ax = fig.add_subplot(111) ax.plot([ 1, 4, 3, 4, ], label='mish') ax.plot([8, 4, 3, 2], label='mashed potatoes')