def financialplots(filename, plotkind): try: data = pd.read_csv(filename, index_col=0, parse_dates=True) except (FileNotFoundError, IOError): print('Wrong file or file path.') return None if plotkind == 'candlestick': fig = FF.create_candlestick(data['Opening Price'], data['Maximum Price'], data['Minimum Price'], data['Closing Price'], dates=data.index) elif plotkind == 'macd': fig = data['Closing Price'].ta_plot(study='macd', fast_period=12, slow_period=26, signal_period=9, asFigure=True) elif plotkind == 'boll': fig = data['Closing Price'].ta_plot(study='boll', asFigure=True) elif plotkind == 'ohlc': fig = FF.create_ohlc(data['Opening Price'], data['Maximum Price'], data['Minimum Price'], data['Closing Price'], dates=data.index) elif plotkind == 'sma': fig = data['Closing Price'].ta_plot(study='sma', asFigure=True) py.plot(fig, filename='../../plots/' + filename[:-4] + plotkind, validate=False, auto_open=False)
def dist_plot(df, groupby=None, val=None, bin_size=1, title=None, show_hist=True, show_kde=True, show_rug=True, show_legend=True, figsize=None, outfile=None, xlabel=None, ylabel=None): if groupby is None: fig = FF.create_distplot([df[c] for c in df.columns], df.columns.values.tolist(), bin_size=bin_size, show_rug=show_rug, show_curve=show_kde) else: groups = sorted(df[groupby].unique().tolist(), reverse=True) data = [] if val is None: val = df.columns.drop(groupby)[0] # choose first non-groupby column for group in groups: mask = df[groupby] == group data.append(df.loc[mask, val]) fig = FF.create_distplot(data, groups, bin_size=bin_size, show_hist=show_hist, show_rug=show_rug, show_curve=show_kde) fig['layout'].update(showlegend=show_legend) if title: fig['layout'].update(title=title) if xlabel: fig['layout'].update(xaxis=go.XAxis(title=xlabel)) if ylabel: fig['layout'].update(yaxis=go.YAxis(title=ylabel)) if figsize and len(figsize) == 2: fig['layout'].update(width=figsize[0]) fig['layout'].update(height=figsize[1]) ol.iplot(fig, show_link=False) # write figure to HTML file if outfile: print('Exporting copy of figure to %s...' % outfile) ol.plot(fig, auto_open=False, filename=outfile)
def dist_plot(df, groupby=None, val=None, bin_size=1, title=None, show_hist=True, show_kde=True, show_rug=True, show_legend=True, figsize=None, outfile=None, xlabel=None, ylabel=None): if groupby is None: fig = FF.create_distplot([df[c] for c in df.columns], df.columns.values.tolist(), bin_size=bin_size, show_rug=show_rug, show_curve=show_kde) else: groups = sorted(df[groupby].unique().tolist(), reverse=True) data = [] if val is None: val = df.columns.drop(groupby)[ 0] # choose first non-groupby column for group in groups: mask = df[groupby] == group data.append(df.loc[mask, val]) fig = FF.create_distplot(data, groups, bin_size=bin_size, show_hist=show_hist, show_rug=show_rug, show_curve=show_kde) fig['layout'].update(showlegend=show_legend) if title: fig['layout'].update(title=title) if xlabel: fig['layout'].update(xaxis=go.XAxis(title=xlabel)) if ylabel: fig['layout'].update(yaxis=go.YAxis(title=ylabel)) if figsize and len(figsize) == 2: fig['layout'].update(width=figsize[0]) fig['layout'].update(height=figsize[1]) ol.iplot(fig, show_link=False) # write figure to HTML file if outfile: print('Exporting copy of figure to %s...' % outfile) ol.plot(fig, auto_open=False, filename=outfile)
def createFigure(filename, isAutoOpen=True, isUploadToServer=False): # ------------Custom Candlestick Colors------------ # Make increasing ohlc sticks and customize their color and name opens = [datarow.open for datarow in dataRows] highs = [datarow.high for datarow in dataRows] lows = [datarow.low for datarow in dataRows] closes = [datarow.close for datarow in dataRows] fig_increasing = FigureFactory.create_candlestick( opens, highs, lows, closes, dates=datetime_xAxis, direction='increasing', marker=go.Marker(color='rgb(61, 153, 112)'), line=go.Line(color='rgb(61, 153, 112)')) # Make decreasing ohlc sticks and customize their color and name fig_decreasing = FigureFactory.create_candlestick( opens, highs, lows, closes, dates=datetime_xAxis, direction='decreasing', marker=go.Marker(color='rgb(255, 65, 54)'), line=go.Line(color='rgb(255, 65, 54)')) # Initialize the figure fig = fig_increasing fig['data'].extend(fig_decreasing['data']) # ------------Add extensions------------ for index, extensionList in enumerate(extensionLists): addLineExtension(fig, [data[0] for data in extensionList], [data[1] for data in extensionList], extensionNames[index], extensionLineTypes[index], extensionColors[index]) # ------------Update layout And Draw the chart------------ filename = chartTitle fig['layout'].update( title=filename, xaxis=dict(ticktext=[datarow.date for datarow in dataRows], tickvals=datetime_xAxis, showticklabels=False, showgrid=True, showline=True), ) plotly.offline.plot(fig, filename=filename + '.html', auto_open=isAutoOpen) print "----------The chart has been generated----------\n"
def show(self, title): figs = [] for ind_f in range(len(self.frames)): (points, triangles, signals) = self.frames[ind_f] points = array(points) triangles = array(triangles) signals = array(signals) signals_per_triangle = list( (signals[triangles[i, 0]] + signals[triangles[i, 1]] + signals[triangles[i, 2]]) / 3 for i in range(triangles.shape[0])) signals_per_triangle[0] += 0.001 # Validate colormap my_colormap = FF._validate_colors("Portland", 'tuple') newdata = FF._trisurf(x=points[:, 2], y=points[:, 0], z=points[:, 1], colormap=my_colormap, simplices=triangles, color_func=signals_per_triangle, plot_edges=False, edges_color='rgb(50, 50, 50)', show_colorbar=False, data_list=True) figs += newdata axis = dict(showbackground=True, backgroundcolor='rgb(230, 230, 230)', gridcolor='rgb(255, 255, 255)', zerolinecolor='rgb(255, 255, 255)') xaxis = axis.copy() xaxis['range'] = [-0.08, 0.09] yaxis = axis.copy() yaxis['range'] = [-0.11, 0.05] zaxis = axis.copy() zaxis['range'] = [0.02, 0.18] aspectratio = dict(x=1, y=1, z=1) layout = graph_objs.Layout(title=title, width='100%', height=800, scene=graph_objs.Scene( xaxis=graph_objs.XAxis(xaxis), yaxis=graph_objs.YAxis(yaxis), zaxis=graph_objs.ZAxis(zaxis), aspectratio=dict(x=aspectratio['x'], y=aspectratio['y'], z=aspectratio['z']), )) return my_iplot(graph_objs.Figure(data=figs, layout=layout))
def import_data(x, y): data = pd.read_csv('data/overlapping/' + x + ',' + y + '-1,3,4,10,11,13.csv') df = data[0:10] table = FF.create_table(df) plotly.offline.plot(table, filename='point_data_table') return data
def __init__(self, sdf: ss.StockDataFrame): self.StockDataFrame = sdf self._fig = FF.create_candlestick(self.StockDataFrame.open, self.StockDataFrame.high, self.StockDataFrame.low, self.StockDataFrame.close, dates=self.StockDataFrame.index)
def scatter_matrix(self, dataframe, select_columns=None, index_colname=None, diag_kind='scatter', marker_size=10, height=800, width=1000, marker_outline_width=0, marker_outline_color='black'): """ Create a scatter matrix plot from dataframes using Plotly. Args: dataframe: (array) array of the data with column headers select_columns: (list) names/headers of columns to plot from the dataframe index_colname: (str) name of the index column in data array diag_kind: (str) sets the chart type for the main diagonal plots (default='scatter') Choose from 'scatter'/'box'/'histogram' marker_size: (float) sets the marker size (in px) height: (int/float) sets the height of the chart width: (int/float) sets the width of the chart marker_outline_width: (int) thickness of marker outline (currently affects the outline of histograms too when "diag_kind" = 'histogram') marker_outline_color: (str/array) color of marker outline - accepts similar formats as other color variables Returns: a Plotly scatter matrix plot """ df = dataframe[select_columns] if select_columns else dataframe fig = FF.create_scatterplotmatrix(df, index=index_colname, diag=diag_kind, size=marker_size, height=height, width=width) # Add outline to markers for trace in fig['data']: trace['marker']['line'] = dict(width=marker_outline_width, color=marker_outline_color) self.create_plot(fig)
def feature_scatterplot(fset_path, features_to_plot): """Create scatter plot of feature set. Parameters ---------- fset_path : str Path to feature set to be plotted. features_to_plot : list of str List of feature names to be plotted. Returns ------- (fig.data, fig.layout) Returns (fig.data, fig.layout) where `fig` is an instance of `plotly.tools.FigureFactory`. """ with featureset.from_netcdf(fset_path, engine=cfg['xr_engine']) as fset: feat_df = fset.to_dataframe() feat_df = feat_df[features_to_plot] if 'target' in fset: feat_df['target'] = fset.target.values index = 'target' else: index = None # TODO replace 'trace {i}' with class labels fig = FF.create_scatterplotmatrix(feat_df, diag='box', index=index, height=800, width=800) py.plot(fig, auto_open=False, output_type='div') return fig.data, fig.layout
def bitcoinchart(): ''' Use function from cryptofuncs.py to get rows and data frame to create a candlestick chart using plotly module. Creating the layout for the graph. ''' rows = cf.bitcoin( 'https://coinmarketcap.com/currencies/bitcoin/historical-data/' '?start=20180321&end=20190321') data_frame = cf.data(rows) fig = FF.create_candlestick(data_frame['open'], data_frame['high'], data_frame['low'], data_frame['close'], dates=data_frame['date']) fig['layout'].update({ 'title': 'Bitcoin Annual Price Development', 'yaxis': { 'title': 'Bitcoin in USD' } }) pyo.offline.plot(fig)
def plotBarsAndTableCount(HTList, plottype, plotTitle="Plot"): index, counts = zip(*HTList) index = list(index) counts = list(counts) data = [[plottype, 'count']] + HTList figure = FF.create_table(data, height_constant=60) bars = go.Bar(x=index, y=counts, xaxis='x2', yaxis='y2', marker=dict(color='#48C20C'), name=plottype) figure['data'].extend(go.Data([bars])) figure.layout.yaxis.update({'domain': [0, .4]}) figure.layout.yaxis2.update({'domain': [.55, 1]}) figure.layout.xaxis.update({'domain': [.33, .66]}) figure.layout.yaxis2.update({'anchor': 'x2'}) figure.layout.xaxis2.update({'anchor': 'y2'}) figure.layout.margin.update({'t': 75, 'l': 50}) figure.layout.update({'title': plotTitle}) figure.layout.update({'height': 800}) # Make text size larger for i in range(len(figure.layout.annotations)): figure.layout.annotations[i].font.size = 20 return plotly.offline.plot(figure, filename=plotTitle.lower().replace(" ", "_"))
def __init__(self, df, primaryKey="Close"): super(Candlestick, self).__init__( FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index)) self.df = df if Candlestick.CUSTOM_MARKERS_ENABLED: for subplot in self["data"]: subplot["hoverinfo"] = "none" self._addMarkers(primaryKey, df[primaryKey]) self._addMovingAverage(df[primaryKey], 50) self._addMovingAverage(df[primaryKey], 14) if Candlestick.VOLUME_ENABLED: self._addVolumeBars(df["Volume"]) self["layout"].update = { "paper_bgcolor": 'rgba(0,0,0,0)', "plot_bgcolor": 'rgba(0,0,0,0)', "yaxis1": { "domain": [0, 1] }, "yaxis2": { "domain": [0, 0.2], "anchor": "x1" }, }
def statisticplots(filename, plotkind, columns): try: data = pd.read_csv(filename, index_col=0, parse_dates=True) except (FileNotFoundError, IOError): print('Wrong file or file path.') return None if columns is None or not columns: columns = list(data.columns.values) data = data.ix[:, columns] if plotkind == 'scattermatrix': fig = FF.create_scatterplotmatrix( data, diag='box', title='Scattermatrix plot of {}'.format(filename[:-4])) elif plotkind == 'line': fig = data.iplot(theme='pearl', kind='scatter', title='Line plot of {}.'.format(filename[:-4]), subplots=True, asFigure=True) elif plotkind == 'box': fig = data.iplot(theme='pearl', kind='box', title='Box plot of {}.'.format(filename[:-4]), asFigure=True) py.plot(fig, filename='../../plots/' + filename[:-4] + plotkind, validate=False, auto_open=False)
def table(percent_array,years): mat = [years] if not len(percent_array)==0: ro = len(percent_array) col = len(percent_array[0]) index = ["80","60","40","20","5"] for i in range(col): sel = [] sel.append(index[i]) for j in range(ro): sel.append(percent_array[j,i]) mat.append(sel) data_matrix = mat table = FF.create_table(data_matrix) plot_div = plot(table, output_type='div', include_plotlyjs=False) return plot_div
def pretty_table(df, outfile=None): """ Display pandas dataframe as a nicely-formated HTML Parameters ---------- outfile: filepath str If provided, output to an HTML file at provided location Example ------- import pandas as pd animals = pd.DataFrame([ ['cat',10, 'housepet'], ['dog',20,'housepet'], ['fish',5,'housepet'], ['cat',20,'zooanimal'], ['dog',50,'zooanimal'], ['fish',20,'zooanimal'],], columns=['animal','value','group']) pretty_table(animals) """ table = FF.create_table(df) ol.iplot(table, show_link=False) # write figure to HTML file if outfile: print('Exporting copy of figure to %s...' % outfile) ol.plot(table, auto_open=False, filename=outfile)
def plot_Age(path, feature, type_): feature = pd.DataFrame(feature) feature = feature.reset_index() data = folder_reader(path) data.columns = ['temperature', 'wind', 'age', 'total'] aux = data[data.age < 1950] aux = aux.groupby(type_)['total'].sum() aux = aux.reset_index() aux = pd.merge(aux, feature, how='left', left_index=type_, right_index="index") number = aux[aux.columns[1]] * 100 / aux[aux.columns[3]] Temp_1 = np.repeat(aux[aux.columns[0]].values, number.round(0).astype(int)) aux = data[data.age >= 1950] aux = aux[aux.age < 1985] aux = aux.groupby(type_)['total'].sum() aux = aux.reset_index() aux = pd.merge(aux, feature, how='left', left_index=type_, right_index="index") number = aux[aux.columns[1]] * 100 / aux[aux.columns[3]] Temp_2 = np.repeat(aux[aux.columns[0]].values, number.round(0).astype(int)) aux = data[data.age >= 1985] aux = aux.groupby(type_)['total'].sum() aux = aux.reset_index() aux = pd.merge(aux, feature, how='left', left_index="wind", right_index="index") number = aux[aux.columns[1]] * 100 / aux[aux.columns[3]] Temp_3 = np.repeat(aux[aux.columns[0]].values, number.round(0).astype(int)) hist_data = [Temp_1, Temp_2, Temp_3] group_labels = ['Older than 65', 'Betwen 30-65', 'Younger than 30'] colors = ['rgb(0, 0, 100)', 'rgb(0, 200, 200)', 'rgb(0, 300, 300)'] fig = FF.create_distplot(hist_data, group_labels, colors=colors) plot_url = py.offline.plot(fig)
def get_isosurface(grid, limits, args): variable = grid[args.isosurface_variable].reshape(*args.resolution) #assume resolution matches grid if args.isosurface_value is None: vertices, simplices = measure.marching_cubes(variable,variable.mean()) else: vertices, simplices = measure.marching_cubes(variable,args.isosurface_value) x,y,z = zip(*vertices) ni, nj, nk = args.resolution x_ = np.linspace(*limits.x, num = ni) y_ = np.linspace(*limits.y, num = nj) z_ = np.linspace(*limits.z, num = nk) iso_x = np.interp(x, np.arange(ni), x_) iso_y = np.interp(y, np.arange(nj), y_) iso_z = np.interp(z, np.arange(nk), z_) colormap=['rgb(255,105,180)','rgb(255,255,51)','rgb(0,191,255)'] fig = FF.create_trisurf(x=iso_x, y=iso_y, z=iso_z, plot_edges=False, show_colorbar=False, colormap=colormap, simplices=simplices, title="Isosurface", ) trace = fig.data[0] trace.opacity = args.isosurface_opacity return [trace]
def add_surface(self, xyz, triangles, lighting=None, **kwargs): """Add a surface model to the plotly figure. xyz : array, shape (n_vertices, 3) An xyz array defining the position of each vertex in 3-D triangles : array, shape (n_triangles, 3) An ijk array defining triangles for the mesh. Each row indexes 3 rows of in xyz, which together make a triangle. lighting : None | dict A dictionary specifying lighting parameters in plotly """ if lighting is None: lighting = dict(ambient=.4, specular=1) self.xyz = xyz self.x = xyz.T[0] self.y = xyz.T[1] self.z = xyz.T[2] self.triangles = triangles self.xrange = np.array([xyz.min(0)[0], xyz.max(0)[0]]) self.yrange = np.array([xyz.min(0)[1], xyz.max(0)[1]]) self.zrange = np.array([xyz.min(0)[2], xyz.max(0)[2]]) colors = self.cmap(np.repeat(.5, len(triangles))) colors = array_to_plotly(colors) self.surfacedata = ff._trisurf( x=self.x, y=self.y, z=self.z, simplices=self.triangles, color_func=colors, **kwargs) self.surfacedata[0]['lighting'] = lighting self.facecolors = self.surfacedata[0]['facecolor'].copy() self.tri_centroids = xyz[triangles].mean(1) self.layout['scene'].update(dict(xaxis=dict(range=self.xrange), yaxis=dict(range=self.yrange), zaxis=dict(range=self.zrange)))
def plot_Gender(path, feature, type_): feature = pd.DataFrame(feature) feature = feature.reset_index() data = folder_reader(path) data.columns = ['temperature', 'wind', 'gender', 'total'] aux = data[data.gender == 1] aux = aux.groupby(type_)['total'].sum() aux = aux.reset_index() aux = pd.merge(aux, feature, how='left', left_index=type_, right_index="index") number = aux[aux.columns[1]] * 100 / aux[aux.columns[3]] Temp_1 = np.repeat(aux[aux.columns[0]].values, number.round(0).astype(int)) aux = data[data.gender == 2] aux = aux.groupby(type_)['total'].sum() aux = aux.reset_index() aux = pd.merge(aux, feature, how='left', left_index=type_, right_index="index") number = aux[aux.columns[1]] / aux[aux.columns[3]] Temp_2 = np.repeat(aux[aux.columns[0]].values, number.round(0).astype(int)) hist_data = [Temp_1, Temp_2] group_labels = ['Man', 'Woman'] colors = ['rgb(0, 0, 100)', 'rgb(0, 200, 200)'] fig = FF.create_distplot(hist_data, group_labels, colors=colors) plot_url = py.offline.plot(fig)
def pretty_table(df, outfile=None): """ Display pandas dataframe as a nicely-formated HTML Parameters ---------- outfile: filepath str If provided, output to an HTML file at provided location Example ------- import pandas as pd animals = pd.DataFrame([ ['cat',10, 'housepet'], ['dog',20,'housepet'], ['fish',5,'housepet'], ['cat',20,'zooanimal'], ['dog',50,'zooanimal'], ['fish',20,'zooanimal'],], columns=['animal','value','group']) pretty_table(animals) """ table = FF.create_table(df) ol.iplot(table, show_link=False) # write figure to HTML file if outfile: print('Exporting copy of figure to %s...' % outfile) ol.plot(table, auto_open=False, filename=outfile)
def plot_tables(): data_matrix_dem = [ ['Feature', 'Coeff', 'Std Err', 'P Val'], ['State Average Turnout', 0.0048, 0.000, 0.000], ['Rural-urban Continuum Code', 0.0046, 0.000, 0.000], ['Perc Less than Highschool Diploma', 0.0006, 0.000, 0.000], ['Perc with Bachelor\'s', 0.0032, 8.54e-05, 0.000], ['Unemployment Rate', 0.0041, 0.000, 0.000], ['Religion NMF Feature 1', 0.0091, 0.001, 0.000], ['Religion NMF Feature 2', 0.0063, 0.000, 0.000], ['Campaign Expenditure', -4.084e-10, 5.09e-11, 0.000], ['Cook Index', 0.4752, 0.006, 0.000], ['Change in Vote Share 2008->2012', 0.2689, 0.024, 0.000], ['1 Field Office', 0.0088, 0.002, 0.000], ['2+ Field Offices', 0.0257, 0.004, 0.000], ['Field Office - Cook Index Interaction', 0.0348, 0.017, 0.041] ] data_matrix_rep = [ ['Feature', 'Coeff', 'Std Err', 'P Val'], ['State Average Turnout', 0.0060, 0.000, 0.000], ['Rural-urban Continuum Code', 0.0044, 0.000, 0.000], ['Perc Less than Highschool Diploma', -0.0024, 0.000, 0.000], ['Perc with Bachelor\'s', 0.0025, 0.000, 0.000], ['Unemployment Rate', 0.0054, 0.000, 0.000], ['Religion NMF Feature 1', 0.0003, 0.001, 0.700], ['Religion NMF Feature 2', 0.0072, 0.001, 0.000], ['Campaign Expenditure', -4.905e-10, 6.44e-11, 0.000], ['Cook Index', -0.5827, 0.008, 0.000], ['Change in Vote Share 2008->2012', -0.0543, 0.032, 0.000], ['1 Field Office', 0.0087, 0.004, 0.025], ['2+ Field Offices', 0.0143, 0.008, 0.080], ['Field Office - Cook Index Interaction', -.1054, 0.029, 0.000] ] table_dem = FF.create_table(data_matrix_dem) table_dem.layout.update( {'title': 'Democratic Regression<br><i>Adj R2 0.978<i>'}) table_rep = FF.create_table(data_matrix_rep) table_rep.layout.update( {'title': 'Republican Regression<br><i>Adj R2 0.982<i>'}) # fig = tools.make_subplots(rows=1, cols=2, subplot_titles=('Democratic Regression<br><i>Adj R2 0.978<i>', # 'Republican Regression<br><i>Adj R2 0.982<i>')) plot_url = py.plot(table_dem, filename='Dem Regression Results') plot_url = py.plot(table_rep, filename='Rep Regression Results')
def main(stock_name, day_in, future): """Control DataBase and make graph if have stock_name""" year, month, day = 2015, 12, 12 open_data, close_data, high_data, low_data, values_data, dates = [], [], [], [], [], [] while day_in > 0: file_name = "set-history_EOD_" + "%d-%02d-%02d.csv" % step_day(year, month, day) try: temporary = csv.reader(open(file_name, "r")) bug = 1 for row in temporary: if row[0] == stock_name: bug = 0 open_data.append(float(row[2])) high_data.append(float(row[3])) low_data.append(float(row[4])) close_data.append(float(row[5])) values_data.append(float(row[6]) * float(row[5])) dates.append(datetime(year=int(row[1][0:4]), month=int(row[1][4:6]), day=int(row[1][6:]))) if bug == 1: it_bug ## Make error when that day haven't stock_name day_in -= 1 except: day_in -= 1 ## Count All day with have stock or no for can find graph of year from 365 day year, month, day = step_day(year, month, day) open_data, close_data, high_data, low_data, values_data, dates = ( open_data[::-1], close_data[::-1], high_data[::-1], low_data[::-1], values_data[::-1], dates[::-1], ) if len(open_data) == 0: return "%s not found" % stock_name data_sma, date_sma = make_sma(future, close_data, dates) data_ema15, date_ema15 = make_ema(15, close_data, dates) data_ema50, date_ema50 = make_ema(50, close_data, dates) add_sma = Scatter(x=date_sma, y=data_sma, name="SMA%d" % (future), line=Line(color="rgb(166,212,64)")) add_ema15 = Scatter(x=date_ema15, y=data_ema15, name="EMA15", line=Line(color="purple")) add_ema50 = Scatter(x=date_ema50, y=data_ema50, name="EMA50", line=Line(color="blue")) add_high = Scatter(x=dates, y=[max(high_data) for i in range(len(dates))], name="HIGH", line=Line(color="black")) add_low = Scatter(x=dates, y=[min(low_data) for i in range(len(dates))], name="LOW", line=Line(color="black")) fig = FF.create_candlestick(open_data, high_data, low_data, close_data, dates=dates) fig["layout"].update({"title": stock_name, "yaxis": {"title": "Price"}, "xaxis": {"title": "Dates"}}) fig["data"].extend([add_high]) fig["data"].extend([add_low]) fig["data"].extend([add_sma]) fig["data"].extend([add_ema15]) fig["data"].extend([add_ema50]) plot_url = py.plot(fig, filename="candlestick", validate=False) data = [go.Bar(x=dates, y=values_data)] layout = go.Layout(title="Values of %s" % stock_name) fig = go.Figure(data=data, layout=layout) plot_url = py.plot(fig, filename="text-hover-bar") return "Graph %s Success" % stock_name
def getCorrelationOfReturns(country): dir = r"C:\\Users\\michael\\Desktop\\Test_output\\" factors = ['Book Value', 'Div Pay Ratio (N)', 'Div Yld', 'Engs Yld', 'Cfl Yld', 'Sales to Pr', 'EBIT to EV (N)', 'EBITDA to Pr', 'RoA (N)', 'RoE', 'ROIC (N)', 'Sustainable GR', 'Asset Turnover (N)', 'Gross Prof to Assts (N)', 'Sales Gr', 'Inc to Sales', 'Op Prof Marg (N)', 'Gross Prof Marg (N)', 'Ass to Eq (N)'] nfactors = len(factors) arr = [] for j in range(nfactors): factor = factors[j].replace(" (N)","") file = dir + "Michael Style 50-100 " + factor + " " + country + " MC M1 200611 to 201610 Dec.xlsx" book=xlrd.open_workbook(file) sheet=book.sheet_by_name('Return Data') list = [] for i in range(5,123): value = sheet.cell_value(12,i) list.append(value) arr.append([]) arr[j].append(list) results = [] for k in range(nfactors): results.append([]) for l in range(nfactors): corr = np.corrcoef(arr[k],arr[l]) temp = corr[0,1] temp2 = round(temp,2) results[k].append(temp2) fig = FF.create_annotated_heatmap(x = factors, y = factors, z=results) fig['layout'].update( title=country+' (Correlation)', xaxis=dict(ticks='', ticksuffix='', side='bottom'), width=450, height=300, margin=go.Margin( l=180, r=80, b=100, t=100, pad=4 ), autosize=False ) py.iplot(fig, filename=country+'-Correlation')
def financialplots(filename, plotkind): try: data = pd.read_csv(filename,index_col=0, parse_dates=True) except(FileNotFoundError, IOError): print('Wrong file or file path.') return None if plotkind == 'candlestick': fig = FF.create_candlestick(data['Opening Price'], data['Maximum Price'], data['Minimum Price'], data['Closing Price'],dates=data.index) elif plotkind == 'macd': fig = data['Closing Price'].ta_plot(study='macd', fast_period=12, slow_period=26, signal_period=9, asFigure=True) elif plotkind == 'boll': fig = data['Closing Price'].ta_plot(study='boll',asFigure=True) elif plotkind == 'ohlc': fig = FF.create_ohlc(data['Opening Price'], data['Maximum Price'], data['Minimum Price'], data['Closing Price'],dates=data.index) elif plotkind == 'sma': fig = data['Closing Price'].ta_plot(study='sma', asFigure=True) py.plot(fig,filename='../../plots/'+filename[:-4]+plotkind,validate=False,auto_open=False)
def plot2D(days=1): df = studyUpdate(days) fig = FF.create_ohlc(df.quoteOpen, df.quoteHigh, df.quoteLow, df.quoteClose, dates=df.index) py.iplot(fig, filename='ubio-update-ohlc')
def slack_stock_price(msg: CommandMessage, config: Dict): if len(msg.text) > 1: try: response = BarchartClient(config.get('api_key', '')) \ .get_history(msg.text.upper()) except: # API request error # Already logged in BarchartClient, so just return error message. return "Something went wrong with %s" % msg.text else: try: open_prices = [] high_prices = [] low_prices = [] close_prices = [] dates = [] volumes = [] for result in response['results']: open_prices.append(result['open']) high_prices.append(result['high']) low_prices.append(result['low']) close_prices.append(result['close']) dates.append(result['tradingDay']) volumes.append(result['volume']) candle_graph_url = plotly.plot( FigureFactory.create_candlestick(open_prices, high_prices, low_prices, close_prices, dates=dates), filename="barchart/price_" + dates[-1], vadlidate=False) volume_graph_url = plotly.plot( Data([Scatter(x=dates, y=volumes)])) attachments = [ MessageAttachment(fallback="stock price history", title="stock price history", title_link=candle_graph_url, image_url=candle_graph_url + ".png"), MessageAttachment(fallback="volume history", title="volume history", title_link=volume_graph_url, image_url=volume_graph_url + ".png")] return SlackMessage( text="Stock price history for %s" % msg.text, attachments=attachments) except Exception as e: # Response handling error logging.error(e) return "Something went wrong with %s" % msg.text else: return "Please enter ticker symbol."
def plot_tables(): data_matrix_dem = [ ['Feature', 'Coeff', 'Std Err', 'P Val'], ['State Average Turnout', 0.0048, 0.000, 0.000], ['Rural-urban Continuum Code', 0.0046, 0.000, 0.000], ['Perc Less than Highschool Diploma', 0.0006, 0.000, 0.000], ['Perc with Bachelor\'s', 0.0032, 8.54e-05, 0.000], ['Unemployment Rate', 0.0041, 0.000, 0.000], ['Religion NMF Feature 1', 0.0091, 0.001, 0.000], ['Religion NMF Feature 2', 0.0063, 0.000, 0.000], ['Campaign Expenditure', -4.084e-10, 5.09e-11, 0.000], ['Cook Index', 0.4752, 0.006, 0.000], ['Change in Vote Share 2008->2012', 0.2689, 0.024, 0.000], ['1 Field Office', 0.0088, 0.002, 0.000], ['2+ Field Offices', 0.0257, 0.004, 0.000], ['Field Office - Cook Index Interaction', 0.0348, 0.017, 0.041] ] data_matrix_rep =[ ['Feature', 'Coeff', 'Std Err', 'P Val'], ['State Average Turnout', 0.0060, 0.000, 0.000], ['Rural-urban Continuum Code', 0.0044, 0.000, 0.000], ['Perc Less than Highschool Diploma', -0.0024, 0.000, 0.000], ['Perc with Bachelor\'s', 0.0025, 0.000, 0.000], ['Unemployment Rate', 0.0054, 0.000, 0.000], ['Religion NMF Feature 1', 0.0003, 0.001, 0.700], ['Religion NMF Feature 2', 0.0072, 0.001, 0.000], ['Campaign Expenditure', -4.905e-10, 6.44e-11, 0.000], ['Cook Index', -0.5827, 0.008, 0.000], ['Change in Vote Share 2008->2012', -0.0543, 0.032, 0.000], ['1 Field Office', 0.0087, 0.004, 0.025], ['2+ Field Offices', 0.0143, 0.008, 0.080], ['Field Office - Cook Index Interaction', -.1054, 0.029, 0.000] ] table_dem = FF.create_table(data_matrix_dem) table_dem.layout.update({'title': 'Democratic Regression<br><i>Adj R2 0.978<i>'}) table_rep = FF.create_table(data_matrix_rep) table_rep.layout.update({'title': 'Republican Regression<br><i>Adj R2 0.982<i>'}) # fig = tools.make_subplots(rows=1, cols=2, subplot_titles=('Democratic Regression<br><i>Adj R2 0.978<i>', # 'Republican Regression<br><i>Adj R2 0.982<i>')) plot_url = py.plot(table_dem, filename='Dem Regression Results') plot_url = py.plot(table_rep, filename='Rep Regression Results')
def filter_data(): # import data data = pd.read_csv('data/ink-2.7mm-1m-length/0.01m-7.csv') df = data[0:10] table = FF.create_table(df) #plotly.offline.plot(table, filename='data-sample') # plot data trace1 = go.Scatter( x=np.arange(len(list(data['time']))), y=list(data['value']), mode='lines', name='Test Data' ) layout = go.Layout( showlegend=True ) trace_data = [trace1] fig = go.Figure(data=trace_data, layout=layout) #plotly.offline.plot(fig, filename='raw-data-plot') # low-pass filter fc = 0.1 b = 0.08 N = int(np.ceil((4 / b))) if not N % 2: N += 1 n = np.arange(N) sinc_func = np.sinc(2 * fc * (n - (N - 1) / 2.)) window = 0.42 - 0.5 * np.cos(2 * np.pi * n / (N - 1)) + 0.08 * np.cos(4 * np.pi * n / (N - 1)) sinc_func = sinc_func * window sinc_func = sinc_func / np.sum(sinc_func) s = list(data['value']) new_signal = np.convolve(s, sinc_func) trace1 = go.Scatter( x=np.arange(len(new_signal)), y=new_signal, mode='lines', name='Low-Pass Filter', marker=dict( color='#C54C82' ) ) layout = go.Layout( title='Low-Pass Filter', showlegend=True ) trace_data = [trace1] fig = go.Figure(data=trace_data, layout=layout) plotly.offline.plot(fig, filename='low-pass-filter')
def build_plotly_table(plotly_df): plotly_df = plotly_df[['Passer_Name', 'Receiver_Name', 'Assists']] plotly_df = plotly_df.sort_values(by='Assists', ascending=False) plotly_df = plotly_df.head(25) plotly_df.columns = ['Passer', 'Receiver', 'Assists'] plotly_df = plotly_df.as_matrix() plotly_df = np.insert(plotly_df, 0, np.array(('Passer', 'Receiver', 'Assists')), 0) table = FF.create_table(plotly_df) py.iplot(table, filename='assist_pairs')
def makeCandlestick(self, filename): self.table = self.importCSV(filename); self.dates = self.makeDateList(0, self.table); self.openPrice = self.makeList(2, self.table); self.highPrice = self.makeList(3, self.table); self.lowPrice = self.makeList(4, self.table); self.closePrice = self.makeList(5, self.table); fig = FF.create_candlestick(self.openPrice, self.highPrice, self.lowPrice, self.closePrice, dates=self.dates); py.plot(fig, filename='finance/aapl-candlestick', validate=False)
def display_scatter_matrix(data): ''' plot a scattter mtrix of data Parameters - data : Dataframe containing the data. ''' fig = FF.create_scatterplotmatrix(data, height=800, width=800) plotly.offline.iplot(fig) return
def __init__(self, args): self.graphs = [] self.sc = SparkContext(conf=SparkConf().setAppName('dashboard')) self.sqlContext = SQLContext(self.sc) self.parse_arguments(args) self.months = self.get_months() self.create_as_tables() self.set_session_counts() self.create_daily_event_graphs() self.create_nav_only_graphs() self.graphs.append(py.plot(FF.create_table(self.metric_table), filename="table", auto_open=False))
def draw(candles): # build the OCHL graph times = candles['time'].apply(parser.parse) fig_increasing = ff.create_candlestick(candles['open'], candles['high'], candles['low'], candles['close'], dates=times, direction='increasing', marker=Marker(color='#ff6666'), line=Line(color='#ff6666')) fig_decreasing = ff.create_candlestick(candles['open'], candles['high'], candles['low'], candles['close'], dates=times, direction='decreasing', marker=Marker(color='#39ac73'), line=Line(color='#39ac73')) fig = fig_increasing fig['data'].extend(fig_decreasing['data']) # build the volume graph volume_chart = Bar(x=times, y=candles['volume'], marker=Marker(color="#ff9933")) g = tools.make_subplots(rows=2, cols=1, shared_xaxes=True) g.append_trace(fig['data'][0], 1, 1) g.append_trace(fig['data'][1], 1, 1) g.append_trace(volume_chart, 2, 1) g.layout['yaxis2']['domain'] = [0.0, 0.25] g.layout['yaxis1']['domain'] = [0.3, 1.0] g.layout['paper_bgcolor'] = '#1E2022' g.layout['plot_bgcolor'] = "#3E3E3E" g.layout['font'] = Font(color="#3E3E3E") g.layout['xaxis1']['tickfont'] = Font(color="#B3A78C") g.layout['yaxis1']['tickfont'] = Font(color="#B3A78C") g.layout['yaxis2']['tickfont'] = Font(color="#B3A78C") g.layout['showlegend'] = False iplot(g)
def make_candlestick(gamelog, column, reduce_func=None): base_series = gamelog.set_index("Date")[column] if reduce_func: series = reduce_func(base_series).dropna() else: series = base_series.cumsum().dropna() weekdata = list(create_weekly(series)) weeks = pd.DataFrame(weekdata).dropna() fig = FF.create_candlestick(weeks.Open, weeks.High, weeks.Low, weeks.Close, dates=weeks.start_date) fig['layout'].update(height=600, width=600, title=column) return fig
def drawOHLCChart(df, ticker, start_date, end_date): trace = go.Candlestick(x=df['Date'], open=df['AAPL.Open'], high=df['AAPL.High'], low=df['AAPL.Low'], close=df['AAPL.Close']) data = [trace] fig = FF.create_ohlc(df.Open, df.High, df.Low, df.Close, dates=df.index) plotly.offline.plot(fig, filename='OHLC/{}-ohlc-{}-{}.html'.format( ticker, start_date, end_date))
def makeCandleStickGraph(stockSymbol, Webster): # Makes a Candle Stick Graph old_stdout = sys.stdout sys.stdout = open(os.devnull, "w") fig = FF.create_candlestick(Webster.Open, Webster.High, Webster.Low, Webster.Close, dates=Webster.index) # Past Data py.plot(fig, filename=stockSymbol + '_Candle', validate=False) sys.stdout.close() sys.stdout = old_stdout
def dr_cdl(df, m_title='分时数据K线图', ftg='tmp/tmp_plotly.html', m_tkAng=-20, m_dtick=5, m_y2k=3): fig = pyff.create_candlestick(df.open, df.high, df.low, df.close, dates=df.index) fig['layout'].update( title=m_title, xaxis=pygo.XAxis( autorange=True, gridcolor='rgb(180, 180, 180)', mirror='all', showgrid=True, showline=True, ticks='outside', tickangle=m_tkAng, dtick=m_dtick, type='category', categoryarray=df.index, ), yaxis=pygo.YAxis( autorange=True, gridcolor='rgb(180, 180, 180)', ), yaxis2=pygo.YAxis( side='right', overlaying='y', range=[0, max(df['volume']) * m_y2k], ), ) # fig.update r_vol = pygo.Bar( x=df.index, #df['xtim'], y=df['volume'], name='volume', yaxis='y2', opacity=0.6, marker=dict( color='rgb(158,202,225)', line=dict( color='rgb(8,48,107)', width=1.5, ), ), ) #r_vol # fig['data'].extend([r_vol]) # pyplt(fig, filename=ftg, show_link=False)
def plotly_3d(verts, faces): x,y,z = zip(*verts) colormap=['rgb(236, 236, 212)','rgb(236, 236, 212)'] fig = FF.create_trisurf(x=x, y=y, z=z, plot_edges=False, colormap=colormap, simplices=faces, backgroundcolor='rgb(64, 64, 64)', title="Interactive Visualization") iplot(fig)
def draw(name, open, close, max, min, dates): py.get_credentials() open = sql.search(name, "open") close = sql.search(name, "close") max = sql.search(name, "max") min = sql.search(name, "min") dates = sql.search(name, "date") fig = FF.create_candlestick(open, max, min, close, dates) py.iplot( fig, filename=name, validate=False, )
def create_plotly_candlestick(symbol, start_date, end_date, filename=None): if filename is None: filename = os.path.join( CHARTS_DIR, '{}-{}-{}.html'.format(symbol, start_date.date(), end_date.date())) df = web.DataReader(symbol, 'yahoo', start_date, end_date) fig = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index) plotly.offline.plot(fig, filename=filename) return filename
def search(self, data_rows, key): data = data_rows[0] if key == 'searchUser': data_matrix = [['Username', 'Count'], [data[0], data[1]]] elif key == 'searchSubred': data_matrix = [['Subreddit', 'Count'], [data[0], data[1]]] colorscale = [[0, '#4d004c'], [.5, '#ffffff'], [1, '#f2e5ff']] table = FF.create_table(data_matrix, colorscale=colorscale) plotted = plotly.offline.plot(table, filename='../graphs.html') return plotted
def scatter_matrix(self, dataframe, select_columns=None, index_colname=None, diag_kind='scatter', marker_size=10, height=800, width=1000, marker_outline_width=0, marker_outline_color='black'): """ Create a scatter matrix plot from dataframes using Plotly. Args: dataframe: (array) array of the data with column headers select_columns: (list) names/headers of columns to plot from the dataframe index_colname: (str) name of the index column in data array diag_kind: (str) sets the chart type for the main diagonal plots (default='scatter') Choose from 'scatter'/'box'/'histogram' marker_size: (float) sets the marker size (in px) height: (int/float) sets the height of the chart width: (int/float) sets the width of the chart marker_outline_width: (int) thickness of marker outline (currently affects the outline of histograms too when "diag_kind" = 'histogram') marker_outline_color: (str/array) color of marker outline - accepts similar formats as other color variables Returns: a Plotly scatter matrix plot """ df = dataframe[select_columns] if select_columns else dataframe fig = FF.create_scatterplotmatrix(df, index=index_colname, diag=diag_kind, size=marker_size, height=height, width=width) # Add outline to markers for trace in fig['data']: trace['marker']['line'] = dict(width=marker_outline_width, color=marker_outline_color) if self.plot_mode == 'offline': if self.filename: plotly.offline.plot(fig, filename=self.filename) else: plotly.offline.plot(fig) elif self.plot_mode == 'notebook': plotly.offline.init_notebook_mode() # run at the start of every notebook; version 1.9.4 required plotly.offline.iplot(fig) elif self.plot_mode == 'online': plotly.tools.set_credentials_file(username=self.username, api_key=self.api_key) if self.filename: plotly.plotly.plot(fig, filename=self.filename, sharing='public') else: plotly.plotly.plot(fig, sharing='public') elif self.plot_mode == 'static': plotly.plotly.image.save_as(fig, filename=self.filename, height=self.height, width=self.width, scale=self.scale)
def readAssetReturnsCSV(asset): df = pandas.read_csv('../../Daily/' + asset + '.csv') df['Index'] = pandas.to_datetime(df['Index']) # convert dates to Datetime objects df = df.set_index('Index') # set Index df = df.sort_index() # sort by date # Will store the weekly data df_calcs = pandas.DataFrame(columns=['period_ended', 'open', 'high', 'low', 'close', 'volume', 'adj.', 'weekly_return']) # compute weekly returns only across full trading weeks (Monday -> Friday) for index, row in df.iterrows(): date = pandas.to_datetime(index) if date.weekday() == 4: # is Friday thur = date + datetime.timedelta(days=-1) wed = date + datetime.timedelta(days=-2) tues = date + datetime.timedelta(days=-3) mon = date + datetime.timedelta(days=-4) # trading occurred on corresponding Friday if thur in df.index and wed in df.index and tues in df.index and mon in df.index: period_ended = date open = df.loc[mon]['open'] close = row['close'] low = min(df.loc[mon]['low'], df.loc[tues]['low'], df.loc[wed]['low'], df.loc[thur]['low'], row['close']) high = max(df.loc[mon]['high'], df.loc[tues]['high'], df.loc[wed]['high'], df.loc[thur]['high'], row['high']) volume = df.loc[mon]['volume'] + df.loc[tues]['volume'] + df.loc[wed]['volume'] + \ df.loc[thur]['volume'] + row['volume'] adj = row['adj.'] weekly_return = (row['adj.'] - df.loc[mon]['adj.']) / df.loc[mon]['adj.'] #print(mon, '(', df.loc[mon]['adj.'], ') ->', date, '(', row['adj.'], '):', weekly_return*100, '%') week = pandas.Series([period_ended, open, high, low, close, volume, adj, weekly_return]) week = week.rename({0: 'period_ended', 1: 'open', 2: 'high', 3: 'low', 4: 'close', 5: 'volume', 6: 'adj.', 7: 'weekly_return'}) df_calcs = df_calcs.append(week, ignore_index=True) df_calcs = df_calcs.set_index('period_ended') # set index to period_ended # standardize weekly returns df_calcs['std_return'] = (df_calcs['weekly_return'] - df_calcs['weekly_return'].mean()) / \ df_calcs['weekly_return'].std() # save OHLC data ohlc = FF.create_ohlc(df_calcs.open, df_calcs.high, df_calcs.low, df_calcs.close, dates=df_calcs.index) return Asset.Asset(asset, df_calcs, ohlc)
def make_plotly_figs(): from plotly.tools import FigureFactory as FF import numpy as np x1 = np.random.randn(200) hist_data = [x1] group_labels = ['Group 1'] # Create distplot with curve_type set to 'normal' fig = FF.create_distplot(hist_data, group_labels, bin_size=.25, show_curve=False) # Add title fig['layout'].update(title='Plot') return [fig]
def statisticplots(filename, plotkind,columns): try: data = pd.read_csv(filename,index_col=0, parse_dates=True) except(FileNotFoundError, IOError): print('Wrong file or file path.') return None if columns is None or not columns: columns = list(data.columns.values) data = data.ix[:,columns] if plotkind == 'scattermatrix': fig = FF.create_scatterplotmatrix(data,diag='box',title='Scattermatrix plot of {}'.format(filename[:-4])) elif plotkind == 'line': fig = data.iplot(theme='pearl',kind='scatter',title='Line plot of {}.'.format(filename[:-4]),subplots=True,asFigure=True) elif plotkind == 'box': fig = data.iplot(theme='pearl',kind='box',title='Box plot of {}.'.format(filename[:-4]),asFigure=True) py.plot(fig,filename='../../plots/'+filename[:-4]+plotkind,validate=False,auto_open=False)
def make_test_plot(): # Add table data table_data = [['Team', 'Wins', 'Losses', 'Ties'], ['Montreal<br>Canadiens', 18, 4, 0], ['Dallas Stars', 18, 5, 0], ['NY Rangers', 16, 5, 0], ['Boston<br>Bruins', 13, 8, 0], ['Chicago<br>Blackhawks', 13, 8, 0], ['Ottawa<br>Senators', 12, 5, 0]] # Initialize a figure with FF.create_table(table_data) figure = FF.create_table(table_data, height_constant=60) # Add graph data teams = ['Montreal Canadiens', 'Dallas Stars', 'NY Rangers', 'Boston Bruins', 'Chicago Blackhawks', 'Ottawa Senators'] GFPG = [3.54, 3.48, 3.0, 3.27, 2.83, 3.18] GAPG = [2.17, 2.57, 2.0, 2.91, 2.57, 2.77] # Make traces for graph trace1 = go.Bar(x=teams, y=GFPG, xaxis='x2', yaxis='y2', marker=dict(color='#0099ff'), name='Goals For<br>Per Game') trace2 = go.Bar(x=teams, y=GAPG, xaxis='x2', yaxis='y2', marker=dict(color='#404040'), name='Goals Against<br>Per Game') # Add trace data to figure figure['data'].extend(go.Data([trace1, trace2])) # Edit layout for subplots figure.layout.yaxis.update({'domain': [0, .45]}) figure.layout.yaxis2.update({'domain': [.6, 1]}) # The graph's yaxis2 MUST BE anchored to the graph's xaxis2 and vice versa figure.layout.yaxis2.update({'anchor': 'x2'}) figure.layout.xaxis2.update({'anchor': 'y2'}) figure.layout.yaxis2.update({'title': 'Goals'}) # Update the margins to add a title and see graph x-labels. figure.layout.margin.update({'t':75, 'l':50}) figure.layout.update({'title': '2016 Hockey Stats'}) # Update the height because adding a graph vertically will interact with # the plot height calculated for the table figure.layout.update({'height':800}) # Plot! return plotly.offline.plot(figure, output_type='div')
def query_stock(stock, start='2011.01.01', end='today'): stock+=".TW" st = datetime.date(datetime.strptime(start, '%Y.%m.%d')) if end == 'today': en = datetime.date(datetime.today()) else: en = datetime.date(datetime.strptime(end, '%Y.%m.%d')) df = data.get_data_yahoo(stock, st, en) fig = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index) SMA20=moving_average(df.Close.values, 20) add_line = Scatter( x=df.index[-len(SMA20):], y=SMA20, name= '20SMA', line=Line(color='rgb(200, 200, 250)') ) fig['data'].extend([add_line]) plotly.offline.plot(fig, validate=False)
def plot(self): fig = FF.create_candlestick(df.O, df.H, df.L, df.C, dates=df.index) fig['layout'].update({'title': 'AMD', 'yaxis': {'title': 'Price'}, 'xaxis': {'title': 'Date'}, 'shapes': [{'x0': '2016-09-01', 'x1': '2016-09-01','y0':0,'y1':1, 'xref':'x','yref':'paper', 'line':{'color': 'rgb(60,60,60)','width':1} } ] }) sma5_line = Scatter(x=df.index, y=df.Sma5,name='SMA5',line={'shape':'spline','dash':'dot'}) sma10_line = Scatter(x=df.index, y=df.Sma10,name='SMA10', line={'shape':'spline','dash':'dot'}) sma15_line = Scatter(x=df.index, y=df.Sma15,name='SMA15', line={'shape':'spline','dash':'dot'}) volume = Bar(x = df.index, y = df.Volume/50000000.0, name='Volume') fig['data'].extend([sma5_line,sma10_line,sma15_line,volume]) return fig
def generate(): # Add table data global data global internet_data_list data = lp.all_running_process() #print data table_data = transform_program_data('table') # Initialize a figure with FF.create_table(table_data) figure = FF.create_table(table_data, height_constant=60, index=True) pyChart1 = go.Pie(labels = ['tumblr', 'twitter', 'facebook', 'pinterest'], values = internet_data_list, name = 'Internet', hole = .4, domain = {'x':[0, .48], 'y':[1, .55]}) pyChart2 = go.Pie(labels = transform_program_data('pyName'), values = transform_program_data('pyData'), name = 'Programs', hole = .4, domain = {'x':[.52, 1], 'y':[1, .55]}) # Add trace data to figure figure['data'].extend(go.Data([pyChart1])) figure['data'].extend(go.Data([pyChart2])) #Table goes at bottom of plot figure.layout.yaxis.update({'domain': [0, .45]}) # Update the margins to add a title and see graph x-labels. figure.layout.margin.update({'t':75, 'l':50}) figure.layout.update({'title': 'How I\'m Spending My Time'}) figure.layout.update({'height':800, 'width':1000}) # Plot! plotly.offline.plot(figure, filename='pyChartWithTable.html', auto_open=False)
def distPlot(request): df = cPickle.loads(str(request.session['dataframe'])) x0 = df[request.GET['x_axis']] x1 = df[request.GET['y_axis']] x2 = df[request.GET['z_axis']] data=[x0,x1,x2] labels = [request.GET['x_axis'],request.GET['y_axis'],request.GET['z_axis']] colors = ['rgb(0, 0, 100)', 'rgb(0, 200, 200)','rgb(0, 200, 100)'] fig = FF.create_distplot( data, labels, bin_size=.2, colors=colors) fig['layout'].update(title='Distplot of {} vs {} vs {}'.format(request.GET['x_axis'], \ request.GET['y_axis'], \ request.GET['z_axis'])) url = py.plot(fig, filename='Distplot with Normal Curve', auto_open=False) dashboard_json = { "rows": [ [{"plot_url": url}] ], "banner": { "visible": False, "backgroundcolor": "#2A3F54", "textcolor": "white", # "title": "{} group_by {}".format(request.GET['x_axis'], request.GET['y_axis']), "links": [] }, "requireauth": False, "auth": { "username": "******", "passphrase": "" } } response = requests.post('https://dashboards.ly/publish', data={'dashboard': json.dumps(dashboard_json)}, headers={'content-type': 'application/x-www-form-urlencoded'}) response.raise_for_status() dashboard_url = response.json()['url'] return dashboard_url
def _plot_accuracy_distribution(dsets_kpis): """ Generate plotly histogram of accuracy distributions, overlaid Return a plot structure """ data = [] for key in dsets_kpis.keys(): accuracy = dsets_kpis[key]['accuracy'] data.append(accuracy) group_labels = dsets_kpis.keys() fig = FF.create_distplot(data, group_labels, show_hist=False) fig['layout']['xaxis'].update(title='Accuracy') fig['layout']['xaxis']['tickfont'].update(size=24) fig['layout']['xaxis']['titlefont'].update(size=24) fig['layout']['yaxis'].update(title='density') fig['layout']['yaxis']['tickfont'].update(size=24) fig['layout']['yaxis']['titlefont'].update(size=24) fig['layout']['font']['size'] = 20 return fig
def __init__(self, df, primaryKey="Close"): super(Candlestick, self).__init__( FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index)) self.df = df if Candlestick.CUSTOM_MARKERS_ENABLED: for subplot in self["data"]: subplot["hoverinfo"] = "none" self._addMarkers(primaryKey, df[primaryKey]) self._addMovingAverage(df[primaryKey], 50) self._addMovingAverage(df[primaryKey], 14) if Candlestick.VOLUME_ENABLED: self._addVolumeBars(df["Volume"]) self["layout"].update = { "paper_bgcolor": 'rgba(0,0,0,0)', "plot_bgcolor": 'rgba(0,0,0,0)', "yaxis1": {"domain": [0, 1]}, "yaxis2": {"domain": [0, 0.2], "anchor": "x1"}, }
def plot_Gender(path, feature, type_): feature = pd.DataFrame(feature) feature = feature.reset_index() data = folder_reader(path) data.columns = ['temperature', 'wind', 'gender', 'total'] aux = data[data.gender == 1] aux = aux.groupby(type_)['total'].sum() aux = aux.reset_index() aux = pd.merge(aux, feature, how='left', left_index=type_, right_index="index") number = aux[aux.columns[1]] * 100 / aux[aux.columns[3]] Temp_1 = np.repeat(aux[aux.columns[0]].values, number.round(0).astype(int)) aux = data[data.gender == 2] aux = aux.groupby(type_)['total'].sum() aux = aux.reset_index() aux = pd.merge(aux, feature, how='left', left_index=type_, right_index="index") number = aux[aux.columns[1]] / aux[aux.columns[3]] Temp_2 = np.repeat(aux[aux.columns[0]].values, number.round(0).astype(int)) hist_data = [Temp_1, Temp_2] group_labels = ['Man', 'Woman'] colors = ['rgb(0, 0, 100)', 'rgb(0, 200, 200)'] fig = FF.create_distplot(hist_data, group_labels, colors=colors) plot_url = py.offline.plot(fig)
def isoSurface3d(image, level=1, fn='./plots/isosurf3d.html'): ''' wrapper function for plotly's isosurface 3d plots args: @a image: 3d array of numbers @a level: level at which to make an isocontour @a fn: string, filename to save plot as ''' vertices, simplices = measure.marching_cubes(image, level) x,y,z = zip(*vertices) fig = FF.create_trisurf( x=x, y=y, z=z, simplices=simplices ) py.offline.plot(fig, filename=fn)
import plotly import plotly.plotly as py from plotly.tools import FigureFactory as FF import numpy as np import pandas as pd print(plotly.__version__) dataframe = pd.DataFrame(np.random.randn(100, 3), columns=['A', 'B', 'C']) fig = FF.create_scatterplotmatrix(dataframe, diag='histogram', index='A', colormap=['rgb(200, 150, 90)', '#3012CF', 'rgb(20, 124, 246)'], colormap_type='seq', height=700, width=700) py.plot(fig, filename = 'Customized Colormap')
def plotlyData(self, i_destDictKey, i_freq='d', i_debug=False, i_out=None): l_data = self.m_data[i_destDictKey]['data'][i_freq] l_data['Date_tmp'] = l_data['Date'].apply(lambda d: mdates.date2num(d.to_pydatetime())) # idxLocalMins = self.m_data[i_destDictKey]['analysis'][i_freq]['localMins'] # idxLocalMaxs = self.m_data[i_destDictKey]['analysis'][i_freq]['localMaxs'] minIdx = self.m_data[i_destDictKey]['analysis'][i_freq]['imin'] maxIdx = self.m_data[i_destDictKey]['analysis'][i_freq]['imax'] fig = FF.create_ohlc(l_data['Open'], l_data['High'], l_data['Low'], l_data['Close'], dates=l_data['Date'], line=Line(color='black')) if self.m_data[i_destDictKey]['analysis'][i_freq]['trendType'] == 2: trend = 'Up-Trend' elif self.m_data[i_destDictKey]['analysis'][i_freq]['trendType'] == 1: trend = 'Down-Trend' else: trend = 'None' # Update the fig - all options here: https://plot.ly/python/reference/#Layout fig['layout'].update({ # 'title': self.generalData['name'] + ' [' + trend + ']', 'title': i_destDictKey + ' [' + trend + ']', 'yaxis': {'title': 'Stock Price [$]'}, 'xaxis': {'title': 'Date'}, # 'shapes': [{ # 'x0': '2008-09-15', 'x1': '2008-09-15', 'type': 'line', # 'y0': 0, 'y1': 1, 'xref': 'x', 'yref': 'paper', # 'line': {'color': 'rgb(40,40,40)', 'width': 0.5} # }], # 'annotations': [{ # 'text': "the fall of Lehman Brothers", # 'x': '2008-09-15', 'y': 1.02, # 'xref': 'x', 'yref': 'paper', # 'showarrow': False, 'xanchor': 'left' # }] }) # plotly.offline.iplot(fig, filename='finance/aapl-recession-ohlc', validate=False) if len(self.m_data[i_destDictKey]['analysis'][i_freq]['ema34']) > len(l_data['Date']): idx = len(self.m_data[i_destDictKey]['analysis'][i_freq]['ema34']) - len(l_data['Date']) + 1 add_mins = Scatter( x=l_data['Date'][minIdx], y=l_data['Low'][minIdx], name='min', mode='markers') add_maxs = Scatter( x=l_data['Date'][maxIdx], y=l_data['High'][maxIdx], name='max', mode='markers') add_ema34 = Scatter( x=l_data['Date'], y=self.m_data[i_destDictKey]['analysis'][i_freq]['ema34'][idx:], name='EMA-34', mode='line') add_ema14 = Scatter( x=l_data['Date'], y=self.m_data[i_destDictKey]['analysis'][i_freq]['ema14'][idx:], name='EMA-14', mode='line') add_ema200 = Scatter( x=l_data['Date'], y=self.m_data[i_destDictKey]['analysis'][i_freq]['ema200'][idx:], name='EMA-200', mode='line') add_ema50 = Scatter( x=l_data['Date'], y=self.m_data[i_destDictKey]['analysis'][i_freq]['ema50'][idx:], name='EMA-50', mode='line') fig['data'].extend([add_mins, add_maxs, add_ema34, add_ema14, add_ema200, add_ema50]) plotly.offline.plot(fig, filename=str(i_destDictKey) + '.html')
width =1 ), opacity=0.8 ) ) dataList.append(name) layout = go.Layout(scene = go.Scene(xaxis=dict(title='Heel Height (mm)'), yaxis=dict(title='Forefoot Height (mm)'),zaxis=dict(title ='Weight (oz)')), margin=dict(l=0,r=0,b=0,t=0)) fig = go.Figure(data = dataList, layout=layout) plot_url = py.plot(fig, filename="3d-scatter-test") #%% hist_data= [] for x in brands: data = rwDF[(rwDF.Brand==x)]['Price'] data.dropna(inplace=True) hist_data.append(data) fig = FF.create_distplot(hist_data, brands, bin_size=20) plot_url=py.plot(fig, filename="Multiple Hists") #%% # Average Price, Forefoot Height, Heel Height, Weight by different Runningwarehouse shoe type rwDF.groupby('Shoe Type').mean().T.plot(kind = 'bar') sns.pairplot(rwDF,vars=['Forefoot Height (mm)', 'Heel Height (mm)', 'Weight (oz)', 'Drop (mm)'], hue="Brand", palette="husl") sns.pairplot(rwDF,vars=['Forefoot Height (mm)', 'Heel Height (mm)', 'Weight (oz)', 'Drop (mm)'], hue="Shoe Type")
def violin_plot(self, data, data_col=None, group_col=None, title=None, height=800, width=1000, colors=None, use_colorscale=False, groups=None): """ Create a violin plot using Plotly. Args: data: (list/array) accepts either a list of numerical values, a list of dictionaries all with identical keys and at least one column of numeric values, or a pandas dataframe with at least one column of numbers data_col: (str) the header of the data column to be used from an inputted pandas dataframe. Not applicable if 'data' is a list of numeric values group_col: (str) applicable if grouping data by a variable. 'group_header' must be set to the name of the grouping variable title: (str) the title of the violin plot height: (float) the height of the violin plot width: (float) the width of the violin plot colors: (str/tuple/list/dict) either a plotly scale name (Greys, YlGnBu, Greens, YlOrRd, Bluered, RdBu, Reds, Blues, Picnic, Rainbow, Portland, Jet, Hot, Blackbody, Earth, Electric, Viridis), an rgb or hex color, a color tuple, a list of colors or a dictionary. An rgb color is of the form 'rgb(x, y, z)' where x, y and z belong to the interval [0, 255] and a color tuple is a tuple of the form (a, b, c) where a, b and c belong to [0, 1]. If colors is a list, it must contain valid color types as its members. If colors is a dictionary, its keys must represent group names, and corresponding values must be valid color types (str). use_colorscale: (bool) Only applicable if grouping by another variable. Will implement a colorscale based on the first 2 colors of param colors. This means colors must be a list with at least 2 colors in it (Plotly colorscales are accepted since they map to a list of two rgb colors) groups: (list) list of group names (strings). This field is used when the same colorscale is required to be used for all violins. Returns: a Plotly violin plot """ if groups and isinstance(data, pd.DataFrame): use_colorscale = True group_stats = {} groupby_data = data.groupby([group_col]) for group in groups: data_from_group = groupby_data.get_group(group)[data_col] stat = np.median(data_from_group) group_stats[group] = stat else: group_stats = None # Filter out groups from dataframe that have only 1 row. if isinstance(data, pd.DataFrame): group_value_counts = data[group_col].value_counts().to_dict() for j in group_value_counts: if group_value_counts[j] == 1: data = data[data[group_col] != j] warnings.warn('Omitting rows with group = ' + str( j) + ' which have only one row in the dataframe.') fig = FF.create_violin(data=data, data_header=data_col, group_header=group_col, title=title, height=height, width=width, colors=colors, use_colorscale=use_colorscale, group_stats=group_stats) # Cannot add x-axis title as the above object populates it with group names. fig.update(dict( layout=dict( title=self.title, titlefont=dict(size=self.textsize, family=self.fontfamily), yaxis=dict(title=self.y_title, titlefont=dict(size=self.textsize, family=self.fontfamily), tickfont=dict(size=self.ticksize, family=self.fontfamily)), ) )) # Change sizes in all x-axis for item in fig['layout']: if item.startswith('xaxis'): fig['layout'][item].update( dict( titlefont=dict(size=self.textsize, family=self.fontfamily), tickfont=dict(size=self.ticksize, family=self.fontfamily) ) ) self.create_plot(fig)