我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用plotly.graph_objs.Figure()。
def plot_frame(dataframe, uname=None, api_key=None, mode='lines', line={}): try: import plotly.plotly as ply import plotly.tools as ptls from plotly.graph_objs import Scatter, Layout, Data, Figure except ImportError: raise InvalidOperationException("Please install the Python plotly bindings") if uname and api_key: ply.sign_in(uname, api_key) c1 = dataframe.ch1 c2 = dataframe.ch2 x = list(range(len(c1))) t1 = Scatter(x=x, y=c1, mode=mode, line=line) t2 = Scatter(x=x, y=c2, mode=mode, line=line) layout = Layout(title="Moku:Lab Frame Grab") data = Data([t1, t2]) fig = Figure(data=data, layout=layout) return ply.plot(fig)
def plot_results(results, plot_name='temp-plot.html'): ''' results is a list of dictionaries, each of which defines a trace e.g. [{'x': x_data, 'y': y_data, 'name': 'plot_name'}, {...}, {...}] Each dictionary's key-value pairs will be passed into go.Scatter to generate a trace on the graph ''' traces = [] for input_args in results: traces.append(go.Scatter(**input_args)) layout = go.Layout( title='Trading performance over time', yaxis=dict( title='Value (USD)' ), ) plot(go.Figure(data=traces, layout=layout), filename=plot_name)
def probabilitygraph(data, vec1, high, k=5, weightf=gaussian, ss=5.0): t1 = np.arange(0.0, high, 0.1) probs = np.array([probguess(data, vec1, v, v+0.1, k, weightf) for v in t1]) smoothed = [] for i in xrange(len(probs)): sv = 0.0 for j in xrange(len(probs)): dist = abs(i-j)*0.1 weight = gaussian(dist, sigma=ss) sv += weight*probs[j] smoothed.append(sv) smoothed = np.array(smoothed) data = go.Scatter(x=t1, y=smoothed) fig = go.Figure(data=[data]) py.plot(fig, filename='wineguess_smoothed')
def __init__(self): """Initialize PlotlyRenderer obj. PlotlyRenderer obj is called on by an Exporter object to draw matplotlib objects like figures, axes, text, etc. All class attributes are listed here in the __init__ method. """ self.plotly_fig = go.Figure() self.mpl_fig = None self.current_mpl_ax = None self.bar_containers = None self.current_bars = [] self.axis_ct = 0 self.x_is_mpl_date = False self.mpl_x_bounds = (0, 1) self.mpl_y_bounds = (0, 1) self.msg = "Initialized PlotlyRenderer\n"
def close_figure(self, fig): """Closes figure by cleaning up data and layout dictionaries. The PlotlyRenderer's job is to create an appropriate set of data and layout dictionaries. When the figure is closed, some cleanup and repair is necessary. This method removes inappropriate dictionary entries, freeing up Plotly to use defaults and best judgements to complete the entries. This method is called by an Exporter object. Positional arguments: fig -- a matplotlib.figure.Figure object. """ self.plotly_fig.force_clean() self.plotly_fig['layout']['showlegend'] = False self.msg += "Closing figure\n"
def make_number_line(similar, group, df): # ''' # description: makes a scatter plot of most relevant courses can be grouped by group data # params: similar: list of tuples, where each tuple is a course name and its similarity score. # group: string, name of column containing group data # df: data frame where the row index is by course title and column name is the string group. contains the name of the group the course belongs to # returns: plotly figure # ''' titles, scores = zip(*similar) if group == None: y_axis = [0]*len(scores) layout = dict(yaxis = dict(visible = False), title = 'Similar Courses') else: y_axis = [df.loc[title][group] for title in titles] layout = dict(title = 'Similar Courses') trace = go.Scatter( x = scores, y= y_axis, mode = 'markers', text = titles) data = [trace] fig = go.Figure(data=data, layout = layout) return fig
def running_times(): rospack = rospkg.RosPack() data_path = os.path.join(rospack.get_path('vslam_evaluation'), 'out') df = pd.read_csv(os.path.join(data_path, 'runtimes.txt'), header=None, index_col=0) bars = [] for col_idx in df: this_stack = df[col_idx].dropna() bars.append( go.Bar( x=this_stack.index, y=this_stack.values, name='Thread {}'.format(col_idx))) layout = go.Layout( barmode='stack', yaxis={'title': 'Running time [s]'}) fig = go.Figure(data=bars, layout=layout) url = py.plot(fig, filename='vslam_eval_run_times')
def generate_chart(self, _): try: import plotly import plotly.graph_objs as go data = [[0, 0, 0], [0, 0, 0]] ok, viol = self.results.get_ok_viol() x = ["OK (%d)" % ok, "Tampering (%d)" % viol] for ret in self.results: i = 1 if ret.is_tampering() else 0 data[i][0] += ret.is_aligned() data[i][1] += ret.is_disaligned() data[i][2] += ret.is_single() final_data = [go.Bar(x=x, y=[x[0] for x in data], name="Aligned"), go.Bar(x=x, y=[x[1] for x in data], name="Disaligned"), go.Bar(x=x, y=[x[2] for x in data], name="Single")] fig = go.Figure(data=final_data, layout=go.Layout(barmode='group', title='Call stack tampering labels')) plotly.offline.plot(fig, output_type='file', include_plotlyjs=True, auto_open=True) except ImportError: self.log("ERROR", "Plotly module not available")
def plotVAEplotly(self, logdir, prefix, ctable=None, reverseUtt=False, batch_size=128, debug=False): ticks = [[-1,-0.5,0,0.5,1]]*self.latentDim samplePoints = np.array(np.meshgrid(*ticks)).T.reshape(-1,3) input_placeholder = np.ones(tuple([len(samplePoints)] + list(self.phon.output_shape[1:-1]) + [1])) preds = self.decode_word([samplePoints, input_placeholder], batch_size=batch_size) if reverseUtt: preds = getYae(preds, reverseUtt) reconstructed = reconstructXae(np.expand_dims(preds.argmax(-1), -1), ctable, maxLen=5) data = [go.Scatter3d( x = samplePoints[:,0], y = samplePoints[:,1], z = samplePoints[:,2], text = reconstructed, mode='text' )] layout = go.Layout() fig = go.Figure(data=data, layout=layout) plotly.offline.plot(fig, filename=logdir + '/' + prefix + '_VAEplot.html', auto_open=False)
def scatter_plot(xs, ys=None, xlabel='', ylabel='', title='', lines=False, marker=dict()): layout = Layout( title=title, xaxis=dict(title=xlabel), yaxis=dict(title=ylabel) ) if ys is None: ys = xs xs = list(range(len(xs))) data = [ Scatter(x=xs, y=ys, mode='lines' if lines else 'markers', marker=marker) ] figure = Figure(data=data, layout=layout) plotly.offline.plot(figure, filename=title + '.html')
def scatter_line_chart(df, y_title): """Generate a reproducible plotly scatter and line plot.""" if len(df.columns) == 3: x_axis, y_axis, factor = df.columns data = go.Data([ go.Scatter(x=sub[x_axis], y=sub[y_axis], name=key) for key, sub in df.groupby(factor, as_index=False, sort=False)]) else: x_axis, y_axis = df.columns data = go.Data([ go.Scatter(x=df[x_axis], y=df[y_axis])]) layout = go.Layout( width=650, height=500, xaxis=go.XAxis( title="Commit Hash" if x_axis == "commit_hash" else "Timestamp", tickangle=-45 if x_axis == "commit_hash" else 0 ), yaxis=go.YAxis( title=y_title ) ) return Markup(py.plot(go.Figure(data=data, layout=layout), **_DEFAULT_ARGS))
def label_usage(self): """Returns a pie chart showing usage information for labels. """ c = self.conn.cursor() c.execute('''SELECT gmail_labels FROM messages WHERE gmail_labels != '' AND gmail_labels NOT LIKE '%Chat%';''') counts = {} for row in c.fetchall(): for label in row[0].split(','): if label not in counts: counts[label] = 0 counts[label] += 1 trace = pgo.Pie( labels=counts.keys(), values=counts.values(), marker=dict( colors=[ self.config.get('color', 'primary'), self.config.get('color', 'secondary'), ] ) ) layout_args = plotly_default_layout_options() layout_args['title'] = 'Label Usage' del layout_args['xaxis'] del layout_args['yaxis'] layout = pgo.Layout(**layout_args) return plotly_output(pgo.Figure(data=[trace], layout=layout))
def thread_sizes(self): """Returns a graph showing thread size information. A "thread" must consist of more than one email. """ c = self.conn.cursor() c.execute('''SELECT COUNT(message_key) AS message_count FROM messages WHERE gmail_labels NOT LIKE '%Chat%' GROUP BY gmail_thread_id HAVING message_count > 1;''') counts = {} for row in c.fetchall(): if row[0] not in counts: counts[row[0]] = 0 counts[row[0]] += 1 data = dict( x=counts.keys(), y=counts.values(), name='Emails in thread', mode='lines+markers', marker=dict( color=self.config.get('color', 'primary'), ), ) layout_args = plotly_default_layout_options() layout_args['title'] = 'Thread Sizes' layout_args['xaxis']['title'] = 'Number of messages' layout_args['yaxis']['title'] = 'Number of threads' layout = pgo.Layout(**layout_args) return plotly_output(pgo.Figure(data=[pgo.Scatter(**data)], layout=layout))
def talk_times(self): """Returns a plotly graph showing chat habits by hour of the day (UTC). """ c = self.conn.cursor() c.execute('''SELECT strftime('%H', `date`) AS hour, COUNT(message_key) AS talk_messages FROM messages WHERE gmail_labels LIKE '%Chat%' GROUP BY hour ORDER BY hour ASC;''') data = OrderedDict() for row in c.fetchall(): data[row[0]] = row[1] total_messages = sum(data.values()) percentages = OrderedDict() for hour in data.keys(): percentages[hour] = str(round(float(data[hour])/float(total_messages) * 100, 2)) + '%' data_args = dict( x=data.keys(), y=data.values(), text=percentages.values(), name='Chat messages', marker=dict( color=self.config.get('color', 'primary') ), fill='tozeroy', ) layout_args = plotly_default_layout_options() layout_args['title'] = 'Chat Times (UTC)' layout_args['xaxis']['title'] = 'Hour of day (UTC)' layout_args['yaxis']['title'] = 'Chat messages' trace = pgo.Scatter(**data_args) layout = pgo.Layout(**layout_args) return plotly_output(pgo.Figure(data=[trace], layout=layout))
def plotly_samtools_depth(ids_depths): n_positions = len(ids_depths[max(ids_depths, key=lambda x: len(set(ids_depths[x])))]) traces = [] for id, depths in sorted(ids_depths.items()): traces.append( go.Scattergl( x=list(range(1, n_positions)), y=depths, mode='lines', name=id, text=id)) layout = go.Layout( title='Depth of coverage', xaxis=dict( title='Position', gridcolor='rgb(255, 255, 255)', gridwidth=2), yaxis=dict( title='Depth', gridcolor='rgb(255, 255, 255)', gridwidth=2, type='log'), paper_bgcolor='rgb(243, 243, 243)', plot_bgcolor='rgb(243, 243, 243)') fig = go.Figure(data=traces, layout=layout) py.plot(fig, filename='depths.html')
def plotly_variants(ids_data): import plotly.offline as py import plotly.graph_objs as go traces = [] for id, data in sorted(ids_data.items()): traces.append( go.Scattergl( x=data['pos'], y=data['max_alt'], mode='markers', name=id, text=id)) layout = go.Layout( title='Variants', xaxis=dict( title='Position', gridcolor='rgb(255, 255, 255)', gridwidth=2), yaxis=dict( title='Abundance', gridcolor='rgb(255, 255, 255)', gridwidth=2, type='linear'), paper_bgcolor='rgb(243, 243, 243)', plot_bgcolor='rgb(243, 243, 243)') fig = go.Figure(data=traces, layout=layout) py.plot(fig, filename='variants.html')
def cumulativegraph(data,vec1,high,k=5,weightf=gaussian): t1 = np.arange(0.0, high, 0.1) cprob = np.array([probguess(data, vec1, 0, v, k, weightf) for v in t1]) data = go.Scatter(x=t1, y=cprob) fig = go.Figure(data=[data]) py.plot(fig, filename='wineguess')
def open_figure(self, fig, props): """Creates a new figure by beginning to fill out layout dict. The 'autosize' key is set to false so that the figure will mirror sizes set by mpl. The 'hovermode' key controls what shows up when you mouse around a figure in plotly, it's set to show the 'closest' point. Positional agurments: fig -- a matplotlib.figure.Figure object. props.keys(): [ 'figwidth', 'figheight', 'dpi' ] """ self.msg += "Opening figure\n" self.mpl_fig = fig self.plotly_fig['layout'] = go.Layout( width=int(props['figwidth'] * props['dpi']), height=int(props['figheight'] * props['dpi']), autosize=False, hovermode='closest') self.mpl_x_bounds, self.mpl_y_bounds = mpltools.get_axes_bounds(fig) margin = go.Margin( l=int(self.mpl_x_bounds[0] * self.plotly_fig['layout']['width']), r=int( (1-self.mpl_x_bounds[1]) * self.plotly_fig['layout']['width']), t=int((1-self.mpl_y_bounds[1]) * self.plotly_fig['layout'][ 'height']), b=int(self.mpl_y_bounds[0] * self.plotly_fig['layout']['height']), pad=0) self.plotly_fig['layout']['margin'] = margin
def costs_curve(self) : values = self.cost_values s = unique(values) values = values - s[0] + .5 * (s[1] - s[0]) # Create a trace trace = go.Scatter( x = arange(len(values))+1, y = array(values), name = 'Cost excess' ) mark = go.Scatter( x = array([1]), y = array([values[0]]), marker = dict( color = "rgb(0, 0, 128)", size = 15 ), name = 'Current value', mode = 'markers' ) data = [trace, mark] layout = go.Layout( title='Cost excess across iterations', width=800, height=800, legend = dict( x = .8, y = 1), #xaxis = dict(range = [-3,3]), #yaxis = dict(range = [-3,3]) yaxis=dict( type='log', autorange=True ) ) return my_iplot(go.Figure(data=data, layout=layout)) + (values,)
def iplot(self, title) : "Interactive manifold display " self.layout['title'] = title return my_iplot(go.Figure(data=self.current_axis, layout=self.layout))
def iplot(self, title = '') : "Interactive manifold display " self.layout['title'] = title (wid, uid) = my_iplot(go.Figure(data=self.current_axis, layout=self.layout)) display(wid)
def build_grouped_bar_chart(chart_data): print "Building Chart with data: " + str(chart_data) py.sign_in(config.PLOTLY_USERNAME, config.PLOTLY_PASSWORD) bars = [] for name in chart_data['groups']: x_data_list = chart_data["x"]["data"][name] y_data_list = chart_data["y"]["data"][name] bar = go.Bar( x=x_data_list, y=y_data_list, name=name ) bars.append(bar) chart_layout = dict( title=chart_data['title'], xaxis=dict(title=chart_data['x']['label'], tickangle=-45, rangemode='tozero'), yaxis=dict(title=chart_data['y']['label'], rangemode='tozero') ) chart = go.Figure(data=bars, layout=chart_layout) filename = id_generator.generate_chart_image_filename() filepath = config.LOCAL_CHARTS_DIR_PATH + filename py.image.save_as(chart, filename=filepath) s3.upload_file(filename, filepath, config.S3_USER_CHARTS_BUCKET) download_url = s3.get_download_url( bucket=config.S3_USER_CHARTS_BUCKET, path=filename, expiry=603148) cleanup_file(filepath) return download_url
def generate_group_bar_charts(y_values, x_values, trace_header, output_directory, output_file_name): """ Plots multiple bar graphs on same graph example usage: generate_group_bar_charts([ [5.10114882, 5.0194652482, 4.9908093076], [4.5824497358, 4.7083614037, 4.3812775722], [2.6839471308, 3.0441476209, 3.6403820447] ], ['#kubuntu-devel', '#ubuntu-devel', '#kubuntu'], ['head1', 'head2', 'head3'], '/home/rohan/Desktop/', 'multi_box' ) Args: x_in (list of int): x_axis data y_in (list of int): y_axis data output_drectory(str): location to save graph output_file_name(str): name of the image file to be saved Returns: null """ data = [ go.Bar( x=x_values, y=y_values[i], name=trace_header[i] ) for i in range(len(y_values)) ] layout = go.Layout( barmode='group' ) fig = go.Figure(data=data, layout=layout) py.image.save_as(fig, output_directory + "/" + output_file_name+".png")
def csv_heatmap_generator_plotly(in_directory, output_directory, output_file_name): """ Plots heatmaps for all the csv files in the given directory Args: in_directory (str): location of input csv files output_drectory(str): location to save graph output_file_name(str): name of the image file to be saved Returns: null """ file_list = glob.glob(in_directory+"*.csv") for file in file_list: csv_data = genfromtxt(file, delimiter=',') trace = go.Heatmap( z=csv_data, x=list(range(48)), y=list(range(1, 12)), colorscale=[ [0, 'rgb(255, 255, 204)'], [0.13, 'rgb(255, 237, 160)'], [0.25, 'rgb(254, 217, 118)'], [0.38, 'rgb(254, 178, 76)'], [0.5, 'rgb(253, 141, 60)'], [0.63, 'rgb(252, 78, 42)'], [0.75, 'rgb(227, 26, 28)'], [0.88, 'rgb(189, 0, 38)'], [1.0, 'rgb(128, 0, 38)'] ] ) data = [trace] layout = go.Layout(title='HeatMap', width=800, height=640) fig = go.Figure(data=data, layout=layout) py.image.save_as(fig, filename=in_directory+file[file.rfind("/")+1:-4]+'_heatmap.png')
def skew_plot(skew_list): data = [go.Bar( x=['True', 'False'], y=[skew_list[1], skew_list[2]], )] layout = go.Layout(title="Skew Balance", width=300, height=300) fig = go.Figure(data=data, layout=layout) iplot(fig, config={'showLink' : False, 'displaylogo' : False, 'modeBarButtonsToRemove' : ['sendDataToCloud']})
def plotly_metric_plot(alg_names, x_axis, y_axis, color_list=None, title=None): #TODO: Check if the x axis and y axis are the same length if color_list == None: color_list = ['rgb(244, 167, 66)', 'rgb(49,130,189)', 'rgb(244, 65, 86)','rgb(100, 201, 137)'] barList = [] # List of bar plots to be plotted i = 0 for name in alg_names: y_axis_holder = [] #PROBLEM: The dict entries may be out of order. This would cause the # incorrect entries to be graphed. for metric_dict in y_axis: y_axis_holder.append(metric_dict[name]) barList.append( go.Bar( x=x_axis, y=y_axis_holder, name=name, marker=dict( color=color_list[i], ))) i += 1 layout = go.Layout(barmode='group', legend=dict(x=20.0, y=20.0), title = title) fig = go.Figure(data=barList, layout=layout) return fig
def position_comparison(): plot_layout = go.Layout( title='X position comparison', xaxis={'title': 'Time [s]'}, yaxis={'title': 'X position [m]'} ) plot_data = [] for i, (label, bag_file_name, odometry_topic_name) in enumerate(comparisons): td_visual, td_vicon = load_one_comparison(bag_file_name, odometry_topic_name) plot_data.append( go.Scatter(x=td_visual.col(0)[::4], y=td_visual.col(1)[::4], mode='lines+markers', name=label, marker={'maxdisplayed': 150})) if i == 0: # Only plot ground truth once plot_data.append( go.Scatter(x=td_vicon.col(0)[::20], y=td_vicon.col(1)[::20], mode='lines+markers', name='Truth', marker={'maxdisplayed': 150})) fig = go.Figure(data=plot_data, layout=plot_layout) url = py.plot(fig, filename='vslam_eval_x_pos')
def plot_graph(traces, title): py.sign_in(settings.PLOTLY_USERNAME, settings.PLOTLY_PASSWORD) filename = str(uuid.uuid4()) + ".png" layout = go.Layout(title=title, width=800, height=640) fig = go.Figure(data=traces, layout=layout) # plot_file = offline.plot(fig, show_link=False, auto_open=False, # filename=settings.MEDIA_ROOT + filename, # include_plotlyjs=True) # ghost = Ghost() # page, resources = ghost.open(plot_file) plot_url = py.plot(fig, filename=filename, auto_open=False, file_opt='new') return plot_url + ".png" # return "http://8d30bf7d.ngrok.io/media/" + filename + ".html"
def generate_trend_plot(plot_data): # return '<pre>{}</pre>'.format(plot_data) ptype = 'line' figs = [] for field in sorted(plot_data.keys()): if ptype == 'line': yvals = [] yvalcount = 0 for x in plot_data[field]: try: yvals.append(float(x['value'])) yvalcount += 1 except ValueError: yvals.append(None) figs.append( go.Scatter( x = [ x['time'] for x in plot_data[field] ], y = yvals, mode = 'markers', text = [ x['name'] for x in plot_data[field] ], name = "{} ({})".format(field, yvalcount) ) ) else: return 'Error - unrecognised plot type: {}'.format(ptype) plot_div = py.plot( go.Figure(data = figs), output_type = 'div', show_link = False, config = dict( modeBarButtonsToRemove = [ 'sendDataToCloud', 'resetScale2d', 'hoverClosestCartesian', 'hoverCompareCartesian', 'toggleSpikelines' ], displaylogo = False ) ) return plot_div
def boolean_chart(df, y_title): """Generate a reproducible plotly scatter and line plot.""" if len(df.columns) == 3: x_axis, y_axis, factor = df.columns data = go.Data([ go.Scatter(x=sub[x_axis], y=sub[y_axis].astype(int), name=key) for key, sub in df.groupby(factor, as_index=False, sort=False)]) else: x_axis, y_axis = df.columns data = go.Data([ go.Scatter(x=df[x_axis], y=df[y_axis].astype(int))]) layout = go.Layout( width=650, height=500, xaxis=go.XAxis( title="Commit Hash" if x_axis == "commit_hash" else "Timestamp", tickangle=-45 if x_axis == "commit_hash" else 0 ), yaxis=go.YAxis( title=y_title, zeroline=False, tickmode="array", tickvals=[0, 1], ticktext=["No", "Yes"] ) ) return Markup(py.plot(go.Figure(data=data, layout=layout), **_DEFAULT_ARGS))
def showTrace(trace, layout=None, title=None): """ Wrapper around plotlys offline .plot() function Parameters ---------- trace : plotly_trace Plotly generated trace to be displayed offline colorize : Bool, optional Toggles bw / colored plot [Default: True] Returns ------- fig : plotly_fig_handle JSON representation of generated figure """ if layout is None: layout = go.Layout( scene=dict( xaxis=dict(range=[-1, 1]), yaxis=dict(range=[-1, 1]), zaxis=dict(range=[-1, 1]), aspectmode='cube' ) ) # Wrap trace in array if needed if not isinstance(trace, list): trace = [trace] fig = go.Figure( data=trace, layout=layout ) if title is not None: fig.layout.update(title=title) filename = title + '.html' else: try: filename = fig.layout.title + '.html' except TypeError: filename = str(current_time()) + '.html' # if colorize: # data[0].autocolorscale = False # data[0].surfacecolor = [0, 0.5, 1] if env_info() == 'jupyter_notebook': plotly_off.init_notebook_mode() plotly_off.iplot(fig) else: plotly_off.plot(fig, filename=filename) return fig
def generatePlot(trace, traceLabels, plotname): data = [trace, traceLabels] layout = go.Layout( margin=dict( l=0, r=0, b=0, t=32, ), title=plotname, scene=dict( xaxis=dict( showgrid=False, showticklabels=False, showspikes=False, showline=False, showaxeslabels=False, zeroline=False, title='', ), yaxis=dict( showgrid=False, showticklabels=False, showspikes=False, showline=False, showaxeslabels=False, zeroline=False, title='', ), zaxis=dict( showgrid=False, showticklabels=False, showspikes=False, showline=False, showaxeslabels=False, zeroline=False, title='', ), ) ) fig = go.Figure(data=data, layout=layout) return py.plot(fig, filename=plotname, auto_open=False)
def _update_graph(map_style, region): dff = dataframe radius_multiplier = {'inner': 1.5, 'outer': 3} layout = go.Layout( title=metadata['title'], autosize=True, hovermode='closest', height=750, font=dict(family=theme['font-family']), margin=go.Margin(l=0, r=0, t=45, b=10), mapbox=dict( accesstoken=mapbox_access_token, bearing=0, center=dict( lat=regions[region]['lat'], lon=regions[region]['lon'], ), pitch=0, zoom=regions[region]['zoom'], style=map_style, ), ) data = go.Data([ # outer circles represent magnitude go.Scattermapbox( lat=dff['Latitude'], lon=dff['Longitude'], mode='markers', marker=go.Marker( size=dff['Magnitude'] * radius_multiplier['outer'], colorscale=colorscale_magnitude, color=dff['Magnitude'], opacity=1, ), text=dff['Text'], # hoverinfo='text', showlegend=False, ), # inner circles represent depth go.Scattermapbox( lat=dff['Latitude'], lon=dff['Longitude'], mode='markers', marker=go.Marker( size=dff['Magnitude'] * radius_multiplier['inner'], colorscale=colorscale_depth, color=dff['Depth'], opacity=1, ), # hovering behavior is already handled by outer circles hoverinfo='skip', showlegend=False ), ]) figure = go.Figure(data=data, layout=layout) return figure
def thread_durations(self): """Returns a pie chart showing grouped thread duration information. A "thread" must consist of more than one email. """ c = self.conn.cursor() c.execute('''SELECT strftime('%s', MAX(`date`)) - strftime('%s', MIN(`date`)) AS duration, COUNT(message_key) AS message_count FROM messages WHERE gmail_labels NOT LIKE '%Chat%' GROUP BY gmail_thread_id HAVING message_count > 1;''') data = {'<= 10 min.': 0, '10 mins - 1 hr.': 0, '1 - 10 hrs.': 0, '10 - 24 hrs.': 0, '1 - 7 days': 0, '1 - 2 weeks': 0, 'more than 2 weeks': 0} for row in c.fetchall(): if row[0] <= 600: data['<= 10 min.'] += 1 elif row[0] <= 3600: data['10 mins - 1 hr.'] += 1 elif row[0] <= 36000: data['1 - 10 hrs.'] += 1 elif row[0] <= 86400: data['10 - 24 hrs.'] += 1 elif row[0] <= 604800: data['1 - 7 days'] += 1 elif row[0] <= 1209600: data['1 - 2 weeks'] += 1 else: data['more than 2 weeks'] += 1 trace = pgo.Pie( labels=data.keys(), values=data.values(), marker=dict( colors=[ self.config.get('color', 'primary'), self.config.get('color', 'secondary'), ] ) ) layout_args = plotly_default_layout_options() layout_args['title'] = 'Thread Durations' del layout_args['xaxis'] del layout_args['yaxis'] layout = pgo.Layout(**layout_args) return plotly_output(pgo.Figure(data=[trace], layout=layout))
def time_of_day(self): """Returns a graph showing email activity (sent/received) by time of day. """ c = self.conn.cursor() c.execute('''SELECT strftime('%H', `date`) AS hour, COUNT(CASE WHEN `from` LIKE ? THEN 1 ELSE NULL END) AS emails_sent, COUNT(CASE WHEN `from` NOT LIKE ? THEN 1 ELSE NULL END) AS emails_received FROM messages WHERE gmail_labels LIKE '%Chat%' GROUP BY hour ORDER BY hour ASC;''', ('%' + self.owner_email + '%', '%' + self.owner_email + '%')) sent = OrderedDict() sent_total = 0 received = OrderedDict() received_total = 0 for row in c.fetchall(): sent_total += row[1] received_total += row[2] sent[row[0]] = row[1] received[row[0]] = row[2] sent_args = dict( x=sent.keys(), y=sent.values(), name='Emails sent', marker=dict( color=self.config.get('color', 'primary'), ), ) received_args = dict( x=received.keys(), y=received.values(), name='Emails received', marker=dict( color=self.config.get('color', 'secondary') ), ) layout_args = plotly_default_layout_options() layout_args['title'] = 'Activity by Hour of the Day (UTC)' layout_args['xaxis']['title'] = 'Hour of the day (UTC)' layout_args['yaxis']['title'] = 'Number of emails' sent_trace = pgo.Scatter(**sent_args) received_trace = pgo.Scatter(**received_args) layout = pgo.Layout(**layout_args) return plotly_output(pgo.Figure(data=[sent_trace, received_trace], layout=layout))
def top_recipients(self, limit=10): """Returns a bar graph showing the top `limit` number of recipients of emails sent. Keyword arguments: limit -- Number of recipients to include. """ c = self.conn.cursor() c.execute('''SELECT address, COUNT(r.message_key) AS message_count FROM recipients AS r LEFT JOIN messages AS m ON(m.message_key = r.message_key) WHERE m.gmail_labels LIKE '%Sent%' GROUP BY address ORDER BY message_count DESC LIMIT ?''', (limit,)) addresses = OrderedDict() longest_address = 0 for row in c.fetchall(): addresses[row[0]] = row[1] longest_address = max(longest_address, len(row[0])) data = dict( x=addresses.values(), y=addresses.keys(), marker=dict( color=self.config.get('color', 'primary_light'), line=dict( color=self.config.get('color', 'primary'), width=1, ), ), orientation='h', ) layout = plotly_default_layout_options() layout['margin']['l'] = longest_address * self.config.getfloat('font', 'size')/1.55 layout['margin'] = pgo.Margin(**layout['margin']) layout['title'] = 'Top ' + str(limit) + ' Recipients' layout['xaxis']['title'] = 'Emails sent to' layout['yaxis']['title'] = 'Recipient address' return plotly_output(pgo.Figure(data=[pgo.Bar(**data)], layout=pgo.Layout(**layout)))
def talk_clients(self): """Returns a pie chart showing distribution of services/client used (based on known resourceparts). This likely not particularly accurate! """ c = self.conn.cursor() c.execute('''SELECT value FROM headers WHERE header = 'To' AND value NOT LIKE '%,%';''') clients = {'android': 0, 'Adium': 0, 'BlackBerry': 0, 'Festoon': 0, 'fire': 0, 'Gush': 0, 'Gaim': 0, 'gmail': 0, 'Meebo': 0, 'Miranda': 0, 'Psi': 0, 'iChat': 0, 'iGoogle': 0, 'IM+': 0, 'Talk': 0, 'Trillian': 0, 'Unknown': 0 } for row in c.fetchall(): try: domain = row[0].split('@', 1)[1] resource_part = domain.split('/', 1)[1] except IndexError: # Throws when the address does not have an @ or a / in the string. continue unknown = True for client in clients: if client in resource_part: clients[client] += 1 unknown = False if unknown: clients['Unknown'] += 1 for client in clients.keys(): if clients[client] is 0: del clients[client] trace = pgo.Pie( labels=clients.keys(), values=clients.values(), marker=dict( colors=[ self.config.get('color', 'primary'), self.config.get('color', 'secondary'), ] ) ) layout_args = plotly_default_layout_options() layout_args['title'] = 'Chat Clients' del layout_args['xaxis'] del layout_args['yaxis'] layout = pgo.Layout(**layout_args) return plotly_output(pgo.Figure(data=[trace], layout=layout))
def talk_days(self): """Returns a stacked bar chart showing percentage of chats and emails on each day of the week. """ c = self.conn.cursor() c.execute('''SELECT strftime('%w', `date`) AS dow, COUNT(CASE WHEN gmail_labels LIKE '%Chat%' THEN 1 ELSE NULL END) AS talk_messages, COUNT(CASE WHEN gmail_labels NOT LIKE '%Chat%' THEN 1 ELSE NULL END) AS email_messages FROM messages WHERE dow NOTNULL GROUP BY dow;''') talk_percentages = OrderedDict() talk_messages = OrderedDict() email_percentages = OrderedDict() email_messages = OrderedDict() for row in c.fetchall(): dow = calendar.day_name[int(row[0]) - 1] # sqlite strftime() uses 0 = SUNDAY. talk_percentages[dow] = str(round(float(row[1]) / sum([row[1], row[2]]) * 100, 2)) + '%' email_percentages[dow] = str(round(float(row[2]) / sum([row[1], row[2]]) * 100, 2)) + '%' talk_messages[dow] = row[1] email_messages[dow] = row[2] chats_trace = pgo.Bar( x=talk_messages.keys(), y=talk_messages.values(), text=talk_percentages.values(), name='Chat messages', marker=dict( color=self.config.get('color', 'primary'), ), ) emails_trace = pgo.Bar( x=email_messages.keys(), y=email_messages.values(), text=email_percentages.values(), name='Email messages', marker=dict( color=self.config.get('color', 'secondary'), ), ) layout = plotly_default_layout_options() layout['barmode'] = 'stack' layout['margin'] = pgo.Margin(**layout['margin']) layout['title'] = 'Chat (vs. Email) Days' layout['xaxis']['title'] = 'Day of the week' layout['yaxis']['title'] = 'Messages exchanged' return plotly_output(pgo.Figure(data=[chats_trace, emails_trace], layout=pgo.Layout(**layout)))
def talk_durations(self): """Returns a plotly pie chart showing grouped chat duration information. """ c = self.conn.cursor() c.execute('''SELECT strftime('%s', MAX(`date`)) - strftime('%s', MIN(`date`)) AS duration FROM messages WHERE gmail_labels LIKE '%Chat%' GROUP BY gmail_thread_id HAVING duration > 0;''') data = {'<= 1 min.': 0, '1 - 10 mins.': 0, '10 - 30 mins.': 0, '30 mins. - 1 hr.': 0, '> 1 hr.': 0} for row in c.fetchall(): if row[0] <= 60: data['<= 1 min.'] += 1 elif row[0] <= 600: data['1 - 10 mins.'] += 1 elif row[0] <= 1800: data['10 - 30 mins.'] += 1 elif row[0] <= 3600: data['30 mins. - 1 hr.'] += 1 else: data['> 1 hr.'] += 1 trace = pgo.Pie( labels=data.keys(), values=data.values(), marker=dict( colors=[ self.config.get('color', 'primary'), self.config.get('color', 'secondary'), ] ) ) layout_args = plotly_default_layout_options() layout_args['title'] = 'Chat Durations' del layout_args['xaxis'] del layout_args['yaxis'] layout = pgo.Layout(**layout_args) return plotly_output(pgo.Figure(data=[trace], layout=layout))
def talk_thread_sizes(self): """Returns a plotly scatter/bubble graph showing the sizes (by message count) of chat thread over time. """ c = self.conn.cursor() c.execute('''SELECT gmail_thread_id, strftime('%Y-%m-%d', `date`) AS thread_date, COUNT(message_key) as thread_size, GROUP_CONCAT(DISTINCT `from`) AS participants FROM messages WHERE gmail_labels LIKE '%Chat%' GROUP BY gmail_thread_id;''') messages = [] marker_sizes = [] dates = [] descriptions = [] for row in c.fetchall(): messages.append(row[2]) marker_sizes.append(max(10, row[2]/5)) dates.append(row[1]) descriptions.append('Messages: ' + str(row[2]) + '<br>Date: ' + str(row[1]) + '<br>Participants:<br> - ' + str(row[3]).replace(',', '<br> - ') ) trace = pgo.Scatter( x=dates, y=messages, mode='markers', marker=dict( size=marker_sizes, ), text=descriptions ) layout_args = plotly_default_layout_options() layout_args['title'] = 'Chat Thread Sizes' layout_args['hovermode'] = 'closest' layout_args['height'] = 800 layout_args['margin'] = pgo.Margin(**layout_args['margin']) layout_args['xaxis']['title'] = 'Date' layout_args['yaxis']['title'] = 'Messages in thread' layout = pgo.Layout(**layout_args) return plotly_output(pgo.Figure(data=[trace], layout=layout))
def plotly_clips(bam_path): import plotly.offline as py import plotly.graph_objs as go aln = list(parse_bam(bam_path).items())[0][1] aligned_depth = [sum(weight.values()) for weight in aln.weights] ins = [sum(i.values()) for i in aln.insertions] x_axis = list(range(len(aligned_depth))) traces = [ go.Scattergl( x = x_axis, y = aligned_depth, mode = 'lines', name = 'Aligned depth'), go.Scattergl( x = x_axis, y = aln.consensus_depth, mode = 'lines', name = 'Consensus depth'), go.Scattergl( x = x_axis, y = aln.clip_start_depth, mode = 'lines', name = 'Soft clip start depth'), go.Scattergl( x = x_axis, y = aln.clip_end_depth, mode = 'lines', name = 'Soft clip end depth'), go.Scattergl( x = x_axis, y = aln.clip_starts, mode = 'markers', name = 'Soft clip starts'), go.Scattergl( x = x_axis, y = aln.clip_ends, mode = 'markers', name = 'Soft clip ends'), go.Scattergl( x = x_axis, y = ins, mode = 'markers', name = 'Insertions'), go.Scattergl( x = x_axis, y = aln.deletions, mode = 'markers', name = 'Deletions')] layout = go.Layout( xaxis=dict( type='linear', autorange=True), yaxis=dict( type='linear', autorange=True)) fig = go.Figure(data=traces, layout=layout) out_fn = os.path.splitext(os.path.split(bam_path)[1])[0] py.plot(fig, filename=out_fn + '.clips.html')
def graph_creation(strategy, data, mae_, system_slug, symbol_slug, period, direction, bh_title): try: if not (strategy is None): strategy_ = strategy.loc[strategy != 0.0] x = strategy.loc[strategy != 0].index mae_ = mae_.loc[(mae_ != mae_.shift()) | (strategy != 0.0)] #graphs strategy_graph = go.Scatter(x=x, y=strategy_, mode="lines", \ name=T('Strategy'), line=dict(color=('rgb(205, 12, 24)'), width=6)) mae_graph = go.Scatter(x=x, y=mae_, mode="markers", \ name=T('Strategy MAE'), line=dict(color=('rgb(115,115,115,1)'), \ width=4, dash='dot')) if not (data is None): data_ = data.loc[strategy != 0.0] data_graph = go.Scatter(x=x, y=data_, mode="lines", name=bh_title, \ line=dict(color=('rgb(22, 96, 167)'), width=4, dash='dot')) title = "{0} on {1} {2} {3}".format(system_slug, symbol_slug, \ period, direction) #else: #title = "Portfolio {}".format(system_slug) if not (data is None): fig_data = go.Data([data_graph, strategy_graph, mae_graph], \ showgrid=True) else: fig_data = go.Data([strategy_graph, mae_graph], showgrid=True) layout = go.Layout(title=title, xaxis={'title':T('Dates')}, \ yaxis={'title':T('$')}, font=dict(family='Courier New, \ monospace', size=14, color='#7f7f7f') ) figure = go.Figure(data=fig_data, layout=layout) graph = opy.plot(figure, auto_open=False, output_type='div') else: graph = None return graph except Exception as e: #stck = inspect.stack() #msg='{0} by {1}: {2}'.format(stck[0][3], stck[1][3], e) #error_email(e=msg) return None
def auto_portfolio_page(request, **kwargs): try: broker_slug = kwargs['broker_slug'] broker_slug = force_text(broker_slug, encoding='utf-8', strings_only=True, errors='strict') except: return HttpResponseRedirect('/') broker = Brokers.objects.get(slug=broker_slug) filename = join(settings.DATA_PATH, 'portfolios', '{}_qndx'.format(broker.slug)) df = nonasy_df_multi_reader(filename=filename, limit=True) hist = list(df['LONG_PL'].loc[df['LONG_PL'] != 0]) hold_hist = list(df['DIFF'].loc[df['DIFF'] != 0]) stats = Stats.objects.get(symbol__symbol='AI50', period__period='1440', \ system__title='AI50', direction=1, broker=broker) hist_graph = hist_graph_creation(hist=hist, hold_hist=hold_hist) margin_data = go.Scatter(x=df.index, y=df.LONG_MARGIN, mode="lines", \ name=T('Margin'), line=dict(color=('rgb(205, 12, 24)'), width=2)) trades_data = go.Scatter(x=df.index, y=df.LONG_TRADES, mode="lines", \ name=T('Trades'), line=dict(color=('rgb(205, 12, 24)'), width=2)) pl_data = go.Scatter(x=df.index, y=df.LONG_PL_CUMSUM, mode="lines", \ name=T('Cumulative returns'), line=dict(color=('rgb(205, 12, 24)'), width=6)) mae_data = go.Scatter(x=df.index, y=df.LONG_MAE, mode="markers", \ name=T('MAE'), line=dict(color=('rgb(115,115,115,1)'), width=4, dash='dot')) margin_g = go.Data([margin_data], showgrid=True) trades_g = go.Data([trades_data], showgrid=True) main_g = go.Data([pl_data, mae_data], showgrid=True) main_layout = go.Layout(title='Autoportfolio Index 50 performance', xaxis={'title':T('Dates')}, \ yaxis={'title':T('$')}, font=dict(family='Courier New, \ monospace', size=14, color='#7f7f7f') ) margin_layout = go.Layout(title='Used margin over time', xaxis={'title':T('Dates')}, \ yaxis={'title':T('$')}, font=dict(family='Courier New, \ monospace', size=14, color='#7f7f7f') ) trades_layout = go.Layout(title='Trades over time', xaxis={'title':T('Dates')}, \ yaxis={'title':T('Count')}, font=dict(family='Courier New, \ monospace', size=14, color='#7f7f7f') ) margin_fig = go.Figure(data=margin_g, layout=margin_layout) trades_fig = go.Figure(data=trades_g, layout=trades_layout) main_fig = go.Figure(data=main_g, layout=main_layout) margin_graph = opy.plot(margin_fig, auto_open=False, output_type='div') trades_graph = opy.plot(trades_fig, auto_open=False, output_type='div') graph = opy.plot(main_fig, auto_open=False, output_type='div') return render(request, '{}/auto_portfolio.html'.format(settings.TEMPLATE_NAME), { 'graph': graph, 'hist_graph': hist_graph, 'stats': stats, 'autoportfolio': True, 'heatmap': stats.heatmap, 'margin_graph': margin_graph, 'trades_graph': trades_graph, 'yearly_img': stats.yearly_ret, 'broker': broker })
def plot(self, figure_or_data, validate=True): """Plot figure_or_data in the Plotly graph widget. Args: figure_or_data (dict, list, or plotly.graph_obj object): The standard Plotly graph object that describes Plotly graphs as used in `plotly.plotly.plot`. See examples of the figure_or_data in https://plot.ly/python/ Returns: None Example 1 - Graph a scatter plot:
from plotly.graph_objs import Scatter g = GraphWidget() g.plot([Scatter(x=[1, 2, 3], y=[10, 15, 13])]) ``` Example 2 - Graph a scatter plot with a title: ``` from plotly.graph_objs import Scatter, Figure, Data fig = Figure( data = Data([ Scatter(x=[1, 2, 3], y=[20, 15, 13]) ]), layout = Layout(title='Experimental Data') ) g = GraphWidget() g.plot(fig) ``` Example 3 - Clear a graph widget ``` from plotly.graph_objs import Scatter, Figure g = GraphWidget() g.plot([Scatter(x=[1, 2, 3], y=[10, 15, 13])]) # Now clear it g.plot({}) # alternatively, g.plot(Figure()) ``` """ if figure_or_data == {} or figure_or_data == Figure(): validate = False figure = tools.return_figure_from_figure_or_data(figure_or_data, validate) message = { 'task': 'newPlot', 'data': figure.get('data', []), 'layout': figure.get('layout', {}), 'graphId': self._graphId } self._handle_outgoing_message(message)
```
def plot_pairdensity_mpl(ls, column1, column2): """Plot the pairwise density between two columns. This plot is an approximation of a scatterplot through a 2D Kernel Density Estimate for two numerical variables. When one of the variables is categorical, a 1D KDE for each of the categories is shown, normalised to the total number of non-null observations. For two categorical variables, the plot produced is a heatmap representation of the contingency table. Parameters ---------- ls : :class:`~lens.Summary` Lens `Summary`. column1 : str First column. column2 : str Second column. Returns ------- :class:`plt.Figure` Matplotlib figure containing the pairwise density plot. """ pair_details = ls.pair_details(column1, column2) pairdensity = pair_details['pairdensity'] x = np.array(pairdensity['x']) y = np.array(pairdensity['y']) Z = np.array(pairdensity['density']) fig, ax = plt.subplots() if ls.summary(column1)['desc'] == 'categorical': idx = np.argsort(x) x = x[idx] Z = Z[:, idx] # Create labels and positions for categorical axis x_labels = dict(enumerate(x)) _set_integer_tick_labels(ax.xaxis, x_labels) x = np.arange(-0.5, len(x), 1.0) if ls.summary(column2)['desc'] == 'categorical': idx = np.argsort(y) y = y[idx] Z = Z[idx] y_labels = dict(enumerate(y)) _set_integer_tick_labels(ax.yaxis, y_labels) y = np.arange(-0.5, len(y), 1.0) X, Y = np.meshgrid(x, y) ax.pcolormesh(X, Y, Z, cmap=DEFAULT_COLORSCALE.lower()) ax.set_xlabel(column1) ax.set_ylabel(column2) ax.set_title(r'$\it{{ {} }}$ vs $\it{{ {} }}$'.format(column1, column2)) return fig
def plot_correlation_mpl(ls, include=None, exclude=None): """Plot the correlation matrix for numeric columns Plot a Spearman rank order correlation coefficient matrix showing the correlation between columns. The matrix is reordered to group together columns that have a higher correlation coefficient. The columns to be plotted in the correlation plot can be selected through either the ``include`` or ``exclude`` keyword arguments. Only one of them can be given. Parameters ---------- ls : :class:`~lens.Summary` Lens `Summary`. include : list of str List of columns to include in the correlation plot. exclude : list of str List of columns to exclude from the correlation plot. Returns ------- :class:`plt.Figure` Matplotlib figure containing the pairwise density plot. """ columns, correlation_matrix = ls.correlation_matrix(include, exclude) num_cols = len(columns) if num_cols > 10: annotate = False else: annotate = True fig, ax = plt.subplots() sns.heatmap(correlation_matrix, annot=annotate, fmt='.2f', ax=ax, xticklabels=columns, yticklabels=columns, vmin=-1, vmax=1, cmap='RdBu_r', square=True) ax.xaxis.tick_top() # Enforces a width of 2.5 inches per cell in the plot, # unless this exceeds 10 inches. width_inches = len(columns) * 2.5 while width_inches > 10: width_inches = 10 fig.set_size_inches(width_inches, width_inches) return fig
def plot_pairdensity(ls, column1, column2): """Plot the pairwise density between two columns. This plot is an approximation of a scatterplot through a 2D Kernel Density Estimate for two numerical variables. When one of the variables is categorical, a 1D KDE for each of the categories is shown, normalised to the total number of non-null observations. For two categorical variables, the plot produced is a heatmap representation of the contingency table. Parameters ---------- ls : :class:`~lens.Summary` Lens `Summary`. column1 : str First column. column2 : str Second column. Returns ------- :class:`plotly.Figure` Plotly figure containing the pairwise density plot. """ pair_details = ls.pair_details(column1, column2) pairdensity = pair_details['pairdensity'] x = np.array(pairdensity['x']) y = np.array(pairdensity['y']) Z = np.array(pairdensity['density']) if ls.summary(column1)['desc'] == 'categorical': idx = np.argsort(x) x = x[idx] Z = Z[:, idx] if ls.summary(column2)['desc'] == 'categorical': idx = np.argsort(y) y = y[idx] Z = Z[idx] data = [go.Heatmap(z=Z, x=x, y=y, colorscale=DEFAULT_COLORSCALE)] layout = go.Layout( title='<i>{}</i> vs <i>{}</i>'.format(column1, column2)) layout['xaxis'] = { 'type': pairdensity['x_scale'], 'autorange': True, 'title': column1 } layout['yaxis'] = { 'type': pairdensity['y_scale'], 'autorange': True, 'title': column2 } fig = go.Figure(data=data, layout=layout) fig.data[0]['showscale'] = False return fig
def allocation_bar_plotly(allocation): ''' Modified from https://plot.ly/python/bar-charts/ Function to create interactive bar graph of our recommended portfolio allocation to a user using the Python package plotly. Input: allocation: recommended allocation (ex: 'Aggressive') Output: 'User-Allocation': Interactive plotly bar graph of allocation. First graph on results webpage ''' funds = list(ETF_ALLOCATION_DICT[allocation].keys()) percentages = [x * 100 for x in ETF_ALLOCATION_DICT[allocation].values()] data = [ go.Bar( x = funds, y = percentages, marker = dict( color = 'rgb(158,202,225)', line = dict( color = 'rgb(8,48,107)', width = 1.5 ), ), opacity = 0.6 ) ] layout = go.Layout( annotations = [ dict( x = xi, y = yi, text = str(yi) + '%', xanchor = 'center', yanchor = 'bottom', showarrow = False, ) for xi, yi in zip(funds, percentages)], title = 'Your Portfolio Allocation: ' + allocation, yaxis = dict( title = 'Percentage of Portfolio (%)'), xaxis = dict( title = 'Vanguard ETFs Ticker Symbols') ) fig = go.Figure(data = data, layout = layout) plot_url = py.plot(fig, filename = 'User-Allocation', auto_open = False)
def fund_performance_graph_plotly(allocation, wealth, hist_period = '10y'): ''' Modified from https://plot.ly/python/line-charts/ Create interactive line graph of performance of user's wealth invested in recommended allocation over past hist_period years using Python package plotly. Inputs: allocation: recommended allocation (ex: 'Aggressive') wealth: starting money that person can invest (ex: 1000) hist_period: string of years for historical portfolio prices (ex: '10y') Output: 'User-Portfolio-Performance': plotly interactive line graph of person's growth in wealth of recommended allocation over past hist_period years ''' connection = sqlite3.connect(DB) c = connection.cursor() # Get portfolio prices for proper hist_period and allocation pf_prices = c.execute("SELECT * FROM " + allocation + "_" + \ TIME_DICT[hist_period] + "_Year_PF;") prices = pf_prices.fetchall() # Get dates in datetime format for label purposes date_list = [datetime.datetime.strptime(x[0], "%Y-%m-%d %H:%M:%S") \ for x in prices] price_list = [x[1] * wealth for x in prices] data = [ go.Scatter( x = date_list, y = price_list, line = dict( color = ('rgb(8,48,107)') ) ) ] layout = go.Layout( title = 'Your Growth in Wealth Over Past 10 Years: ' + allocation, yaxis = dict( title = 'Your Portfolio Value ($)'), xaxis = dict( title = 'Year') ) fig = go.Figure(data = data, layout = layout) plot_url = py.plot(fig, filename = 'User-Portfolio-Performance', \ auto_open = False) annualized_return = (((price_list[-1] / price_list[0]) ** \ (1 / int(hist_period[:-1]))) - 1) * 100 connection.commit() connection.close return str("{0:.2f}".format(annualized_return)) + "%"
def graph_worst_year_plotly(allocation, wealth, worst_year_start_date, worst_year_end_date, hist_period = '10y'): ''' Modified from https://plot.ly/python/line-charts/ Create interative line graph of person's decrease in wealth over worst 12-month period over past hist_period years using Python package plotly. Inputs: allocation: recommended allocation (ex: 'Aggressive') wealth: starting money that person can invest (ex: 1000) worst_year_start_date: Start date of worst 12-month period worst_year_end_date: End date of worst 12-month period hist_period: string of years for historical portfolio prices (ex: '10y') Output: 'User-Worst-Year': Interactive plotly line graph of worst 12-month performance of user's wealth with recommended allocation ''' connection = sqlite3.connect(DB) c = connection.cursor() pf_prices = c.execute("SELECT * FROM " + allocation + "_" + \ TIME_DICT[hist_period] + "_Year_PF WHERE Date >= '" + \ worst_year_start_date + "' AND Date <= '" + worst_year_end_date + "';") prices = pf_prices.fetchall() # Get dates in datetime format date_list = [datetime.datetime.strptime(x[0], "%Y-%m-%d %H:%M:%S") \ for x in prices] price_list = [(x[1] / prices[0][1]) * wealth for x in prices] data = [ go.Scatter( x = date_list, y = price_list, line = dict( color = ('rgb(259, 0, 0)') ) ) ] layout = go.Layout( title = 'Worst 12-Month Performance Over Past 10 Years: ' + allocation, yaxis = dict( title = 'Your Portfolio Value ($)'), xaxis = dict( title = 'Date') ) fig = go.Figure(data = data, layout = layout) plot_url = py.plot(fig, filename = 'User-Worst-Year', auto_open = False) connection.commit() connection.close