def setUp(self): _future_flags.add('v4_subplots') fig = make_subplots( rows=3, cols=3, specs=[[{}, {'type': 'scene'}, {}], [{'secondary_y': True}, {'type': 'polar'}, {'type': 'polar'}], [{'type': 'xy', 'colspan': 2}, None, {'type': 'ternary'}]] ).update(layout={'height': 800}) fig.layout.xaxis.title.text = 'A' fig.layout.xaxis2.title.text = 'A' fig.layout.xaxis3.title.text = 'B' fig.layout.xaxis4.title.text = 'B' fig.layout.yaxis.title.text = 'A' fig.layout.yaxis2.title.text = 'B' fig.layout.yaxis3.title.text = 'A' fig.layout.yaxis4.title.text = 'B' fig.layout.polar.angularaxis.rotation = 45 fig.layout.polar2.angularaxis.rotation = 45 fig.layout.polar.radialaxis.title.text = 'A' fig.layout.polar2.radialaxis.title.text = 'B' fig.layout.scene.xaxis.title.text = 'A' fig.layout.scene.yaxis.title.text = 'B' fig.layout.ternary.aaxis.title.text = 'A' self.fig = fig self.fig_no_grid = go.Figure(self.fig.to_dict())
def test_get_subplot_out_of_bounds(self): fig = subplots.make_subplots(rows=4, cols=2) self.assertRaises(ValueError, lambda: fig.get_subplot(0, 1)) self.assertRaises(ValueError, lambda: fig.get_subplot(5, 1)) self.assertRaises(ValueError, lambda: fig.get_subplot(1, 0)) self.assertRaises(ValueError, lambda: fig.get_subplot(1, 3))
def runRegular(recDays, Pop, avgInfections, initialInfections): global fig, stringy # Number of people self-isolating selfIsolating = Pop # Number of people susceptible S_0 = Pop - initialInfections # Number of people infected I_0 = initialInfections print('Initial Number of infected {}'.format(I_0 / S_0)) # Number of people recovered R_0 = 0 # Days to recovery recovDays = recDays # How many people the person infects every 2 weeks gamma = avgInfections / recDays # period of infectiousness is atleast 14 days (recovery) beta = 1 / recDays # tau (time step) tau = 1 t_max = 1000 S = [S_0] I = [I_0] R = [R_0] t = [0] def function(S_0, I_0, R_0): S_dot = -gamma * S_0 / Pop * I_0 I_dot = gamma * S_0 / Pop * I_0 - beta * I_0 R_dot = beta * I_0 return S_dot, I_dot, R_dot t_0 = 0 while t_0 <= t_max: S_1 = S_0 + tau * function(S_0, I_0, R_0)[0] I_1 = I_0 + tau * function(S_0, I_0, R_0)[1] R_1 = R_0 + tau * function(S_0, I_0, R_0)[2] if I_1 < 1: break S.append(S_1) I.append(I_1) R.append(R_1) t.append(t_0) S_0 = S_1 I_0 = I_1 R_0 = R_1 t_0 += tau print('Peak infection at {} days with {} people infected at once'. format(round(np.argmax(I) * tau, 3), round(max(I)), 3)) stringy = 'On average, peak infection at {} days with {} people infected at once'.format( round(np.argmax(I) * tau, 3), round(max(I))) df = pd.DataFrame(data=list(zip(t, I)), columns=['Time', 'Infected']) df['Susceptible'] = S df['Recovered'] = R fig = subplots.make_subplots() fig['layout'].update(height=500, title='SIR Model evolution of Virus', title_x=0.5, xaxis_title="Days", yaxis_title="Population") fig['layout']['margin'] = {'l': 20, 'b': 30, 'r': 10, 't': 50} fig.append_trace( { 'x': df['Time'], 'y': df['Susceptible'], 'type': 'scatter', 'name': 'Susceptible' }, 1, 1) fig.append_trace( { 'x': df['Time'], 'y': df['Infected'], 'type': 'scatter', 'name': 'Infected' }, 1, 1) fig.append_trace( { 'x': df['Time'], 'y': df['Recovered'], 'type': 'scatter', 'name': 'Recovered' }, 1, 1)
def plot(self): """Make the interactive subplots, including the solar plot, based on how many locations (regions) are given. Write out the file to the correct outpath.""" # make the interactive plots with sub-solar plots if self.multi: rows = len(self.location) + 1 self.sub_names += ["Solar Radio Flux"] titles = tuple(self.sub_names) else: # only one region means two subplots rows = 2 titles = (self.name, "Solar Radio Flux") fig_height = 750 delta = 250 if rows > 3: fig_height = delta * rows pio.templates.default = "simple_white" self.figure = make_subplots(rows=rows, cols=1, shared_xaxes=True, subplot_titles=titles, x_title="Year", vertical_spacing=0.05) self.figure.update_layout(height=fig_height, width=1200, title_text=self.name) if self.multi: # prime the pump again region_x_data = self.data[self.x].where(self.data["region"] == 0) region_y_data = self.data[self.y].where(self.data["region"] == 0) self.figure.add_trace(go.Scatter(x=region_x_data, y=region_y_data, mode="markers", marker=dict(color="black", size=5), hovertext=self.labels, hoverinfo="x+y+text", name="Mean Dark Rate"), row=1, col=1) self.figure.update_yaxes( title_text="Mean Dark Rate<br>(counts/pix/sec)", row=1, col=1) for index, location in enumerate(self.location[1:]): index = index + 1 region_x_data = self.data[self.x].where( self.data["region"] == index) region_y_data = self.data[self.y].where( self.data["region"] == index) self.figure.add_trace(go.Scatter(x=region_x_data, y=region_y_data, showlegend=False, mode="markers", marker=dict(color="black", size=5), hovertext=self.labels, hoverinfo="x+y+text", name="Mean Dark Rate"), row=index + 1, col=1) self.figure.update_yaxes( title_text="Mean Dark Rate<br>(counts/pix/sec)", row=index + 1, col=1) else: # single plot self.figure.add_trace(go.Scatter(x=self.data[self.x], y=self.data[self.y], mode="markers", marker=dict(color="black", size=5), hovertext=self.labels, hoverinfo="x+y+text", name="Mean Dark Rate"), row=1, col=1) self.figure.update_yaxes( title_text="Mean Dark Rate<br>(counts/pix/sec)", row=1, col=1) ## this is solar stuff only until the next ## datemin = self.data[self.x].min() datemax = self.data[self.x].max() # sunpy_data = sunpy_retriever(date_min, date_max) solar_data = get_solar_data(NOAA_URL, datemin, datemax) solar_time = solar_data.index solar_flux = solar_data["f10.7"] solar_flux_smooth = solar_data["box_convolved_f10.7"] self.figure.add_trace(go.Scatter(x=solar_time, y=solar_flux, mode="lines", line=dict(dash="longdash", color="#0F2080"), name="10.7 cm"), row=rows, col=1) self.figure.add_trace(go.Scatter(x=solar_time, y=solar_flux_smooth, mode="lines", line=dict(color="#85C0F9"), name="10.7 cm Smoothed"), row=rows, col=1) self.figure.update_yaxes(title_text="Solar Radio Flux", row=rows, col=1) ## self.figure.update_xaxes(showgrid=True, showline=True, mirror=True) self.figure.update_yaxes(showgrid=True, showline=True, mirror=True)
def make_plotly_graph(): # Make Figure theme = pio.templates['plotly_dark'] # Create subplots and mention plot grid size fig = make_subplots(shared_xaxes=True, vertical_spacing=0.03, rows=2, cols=1, row_width=[0.2, 0.7]) def plot_levels(where, lvl, only_good=False, n=2): if lvl: for i in lvl: if isinstance(i, float): where.add_hline(y=i, line=dict(color='rgba(158,160,162, 1)', width=2), line_width=0.1, line_dash='solid', annotation_position='bottom right', annotation_text=Formatter.float(value=i, place=n)) elif isinstance(i, dict): if 'score' in i.keys(): if only_good and i['score'] < 0: continue color = 'rgba(224,41,74, 1)' if i['score'] < 0 else 'rgba(46,189,133, 1)' name_is = 'Resistance' if i['score'] < 0 else 'Support' where.add_hline(y=i['price'], color=color, line_dash='solid', line_width=0.1 * abs(i['score']), name=name_is, annotation_position='bottom right', annotation_text=Formatter.float(value=i['price'], place=n)) else: where.add_hline(y=i['price'], name='Support', line=dict(color='rgba(158,160,162, 1)', width=2), line_dash='solid', annotation_position='bottom right', annotation_text=Formatter.float(value=i['price'], place=n)) # Add Candle-sticks to figure fig.add_trace(go.Candlestick(x=klines.date, open=klines.open, high=klines.high, low=klines.low, close=klines.close, increasing={'line': {'color': 'rgba(46,189,133, 1)'}}, decreasing={'line': {'color': 'rgba(224,41,74, 1)'}}, name='Candlesticks'), row=1, col=1) # Add close values with curved line to figure fig.add_trace(go.Scatter(x=klines.date[pivots != 0], y=klines.close[pivots != 0], mode='lines', name='Price', line=dict(shape='spline', width=2, color='rgba(240,185,11, 1)')), # or 240,185,11 row=1, col=1) # Plot horizontal line of support and resistance plot_levels(fig, levels, only_good=True, n=precision) # Add the 20 moving average to figure fig.add_trace(go.Scatter(x=klines.date, y=klines.ma20, name='Short', text='20 MA', mode='lines', line=dict(color='rgba(9,9,177, 1)', width=2, shape='spline')), row=1, col=1) # Add the 50 moving average to figure fig.add_trace(go.Scatter(x=klines.date, y=klines.ma50, name='Medium', text='50 MA', mode='lines', line=dict(color='rgba(54,12,158, 1)', width=2, shape='spline')), row=1, col=1) # Add the 200 moving average to figure fig.add_trace(go.Scatter(x=klines.date, y=klines.ma200, name='Trend', text='200 MA', mode='lines', line=dict(color='rgba(85,14,136, 1)', width=2, shape='spline')), row=1, col=1) # Add profit made from set price fig.add_trace(go.Scatter(x=klines.date, y=total.worth, name='Investment', text='P&L', line=dict(color='rgba(46,189,133, 1)', width=2, shape='spline'), # 46,189,133 showlegend=True), row=2, col=1) fig.layout.font.family = 'Share Tech' fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='rgba(25,27,32, 1)') # 43,47,54,255 fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='rgba(25,27,32, 1)') # 43,47,54,255 fig.update_layout(template=theme, xaxis_rangeslider_visible=False, plot_bgcolor='rgba(25,27,32, 1)', paper_bgcolor='rgba(25,27,32, 1)', xaxis_tickformat='%d %B (%a)<br>%Y', yaxis_tickformat=f'{precision}.f', title=f'Plotly Graph for Market: {selected_symbol} (Binance)'), # annotations=annotations) fig.show()
graphFXD = add_dilation_to_fxd(graphGZD, graphFXD) treeFXD = add_dilation_to_fxd(treeGZD, treeFXD) graphFXD, treeFXD = add_angles_to_fxd(graphFXD, treeFXD) graphFXD['text'] = hover(graphFXD) treeFXD['text'] = hover(treeFXD) graphFXD = dilation_color(graphFXD) treeFXD = dilation_color(treeFXD) sizeref = graphFXD['dilation'].max()/1000 #Initializing subplots schema fig = make_subplots(rows=2, cols=1, subplot_titles=("Graph Absolute vs Relative Angle with Pupil Dilation", "Tree Absolute vs Relative Angle with Pupil Dilation")) #populating scatterplot with informations fig.add_trace(go.Scatter(showlegend=False, x=graphFXD['relative_angle'], y=graphFXD['absolute_angle'], text = graphFXD['text'], marker_color=graphFXD['color'], marker_size=10), row=1,col=1) fig.add_trace(go.Scatter(showlegend=False, x=treeFXD['relative_angle'], y=treeFXD['absolute_angle'], text = treeFXD['text'], marker_color=treeFXD['color'], marker_size=10), row=2,col=1) fig.update_traces(mode='markers', marker= dict(sizemode='area', sizeref=sizeref, line_width=1)) #title to each axis fig.update_xaxes(title_text='Relative Angle (degrees)', row=1, col=1,) fig.update_xaxes(title_text='Relative Angle (degrees)', row=2, col=1,)
def generate_candlestick_graph( pair: str, data: pd.DataFrame, trades: pd.DataFrame = None, *, indicators1: List[str] = [], indicators2: List[str] = [], plot_config: Dict[str, Dict] = {}, ) -> go.Figure: """ Generate the graph from the data generated by Backtesting or from DB Volume will always be ploted in row2, so Row 1 and 3 are to our disposal for custom indicators :param pair: Pair to Display on the graph :param data: OHLCV DataFrame containing indicators and buy/sell signals :param trades: All trades created :param indicators1: List containing Main plot indicators :param indicators2: List containing Sub plot indicators :param plot_config: Dict of Dicts containing advanced plot configuration :return: Plotly figure """ plot_config = create_plotconfig(indicators1, indicators2, plot_config) rows = 2 + len(plot_config['subplots']) row_widths = [1 for _ in plot_config['subplots']] # Define the graph fig = make_subplots( rows=rows, cols=1, shared_xaxes=True, row_width=row_widths + [1, 4], vertical_spacing=0.0001, ) fig['layout'].update(title=pair) fig['layout']['yaxis1'].update(title='Price') fig['layout']['yaxis2'].update(title='Volume') for i, name in enumerate(plot_config['subplots']): fig['layout'][f'yaxis{3 + i}'].update(title=name) fig['layout']['xaxis']['rangeslider'].update(visible=False) fig.update_layout(modebar_add=["v1hovermode", "toggleSpikeLines"]) # Common information candles = go.Candlestick(x=data.date, open=data.open, high=data.high, low=data.low, close=data.close, name='Price') fig.add_trace(candles, 1, 1) if 'buy' in data.columns: df_buy = data[data['buy'] == 1] if len(df_buy) > 0: buys = go.Scatter(x=df_buy.date, y=df_buy.close, mode='markers', name='buy', marker=dict( symbol='triangle-up-dot', size=9, line=dict(width=1), color='green', )) fig.add_trace(buys, 1, 1) else: logger.warning("No buy-signals found.") if 'sell' in data.columns: df_sell = data[data['sell'] == 1] if len(df_sell) > 0: sells = go.Scatter(x=df_sell.date, y=df_sell.close, mode='markers', name='sell', marker=dict( symbol='triangle-down-dot', size=9, line=dict(width=1), color='red', )) fig.add_trace(sells, 1, 1) else: logger.warning("No sell-signals found.") # Add Bollinger Bands fig = plot_area(fig, 1, data, 'bb_lowerband', 'bb_upperband', label="Bollinger Band") # prevent bb_lower and bb_upper from plotting try: del plot_config['main_plot']['bb_lowerband'] del plot_config['main_plot']['bb_upperband'] except KeyError: pass # main plot goes to row 1 fig = add_indicators(fig=fig, row=1, indicators=plot_config['main_plot'], data=data) fig = add_areas(fig, 1, data, plot_config['main_plot']) fig = plot_trades(fig, trades) # sub plot: Volume goes to row 2 volume = go.Bar(x=data['date'], y=data['volume'], name='Volume', marker_color='DarkSlateGrey', marker_line_color='DarkSlateGrey') fig.add_trace(volume, 2, 1) # add each sub plot to a separate row for i, label in enumerate(plot_config['subplots']): sub_config = plot_config['subplots'][label] row = 3 + i fig = add_indicators(fig=fig, row=row, indicators=sub_config, data=data) # fill area between indicators ( 'fill_to': 'other_indicator') fig = add_areas(fig, row, data, sub_config) return fig
def update_table(cutoff, show_date, num_features): #Call model fucntion closer_predict_display, score_table, cutoff_change_msg, feature_names, feature_imps, feature_desc_df = call_model( show_date, cutoff, num_features) #Create a figure with three subplots to show feature importance graph, description of features, and accuracy scores figure = make_subplots(rows=1, cols=3, column_widths=[0.25, 0.5, 0.25], specs=[[{ "type": "bar" }, { "type": "table" }, { "type": "table" }]]) figure.add_trace(go.Bar(x=feature_names, y=feature_imps), row=1, col=1) figure.update_yaxes(title_text="Feature Importance", row=1, col=1) figure.update_layout(paper_bgcolor="#2C6E91", margin=dict(l=40, r=40, t=40, b=40), height=400), figure.update_layout( font=dict(family="Arial, monospace", size=14, color='#F15A50')) figure.update_traces(marker_color='#F15A50', marker_line_color="#2C6E91", marker_line_width=1.5, opacity=0.6) figure.add_trace(go.Table( columnwidth=[1, 3], header=dict(values=["Feature Label", "Feature Description"], font=dict(size=14, family="Arial, monospace"), line_color='#2C6E91', align="left"), cells=dict(values=[ feature_desc_df[k].tolist() for k in feature_desc_df.columns ], align="left", font=dict(size=14, family="Arial, monospace"), line_color='#2C6E91', height=40)), row=1, col=2) figure.add_trace(go.Table( columnwidth=[2, 1], header=dict(values=["Score Type", "Accuracy"], font=dict(size=14, family="Arial, monospace"), line_color='#2C6E91', align="center"), cells=dict( values=[score_table[k].tolist() for k in score_table.columns], align="center", font=dict(size=14, family="Arial, monospace"), line_color='#2C6E91', height=30)), row=1, col=3) #Split prediction and input table into sets for merged_df = closer_predict_display merged_df1 = merged_df[merged_df['set'] == '1'].copy() merged_df2 = merged_df[merged_df['set'] == '2'].copy() merged_df3 = merged_df[merged_df['set'] == '3'].copy() merged_dfe = merged_df[(merged_df['set'] == 'E') | (merged_df['set'] == 'E2')].copy() #Pass all info back into app return generate_table(merged_df1), generate_table( merged_df2), generate_table(merged_df3), generate_table( merged_dfe), cutoff_change_msg, figure
def accvelposplot(data): fig = make_subplots(3, 1) fig.add_trace(go.Scatter( x=data.TIME, y=data.ACC_X, mode='lines', name=f'ACC X', ), row=1, col=1) fig.add_trace(go.Scatter( x=data.TIME, y=data.ACC_Y, mode='lines', name=f'ACC Y', ), row=1, col=1) fig.add_trace(go.Scatter( x=data.TIME, y=data.ACC_Z, mode='lines', name=f'ACC Z', ), row=1, col=1) fig.add_trace(go.Scatter( x=data.TIME, y=data.V_X, mode='lines', name=f'VX', ), row=2, col=1) fig.add_trace(go.Scatter( x=data.TIME, y=data.V_Y, mode='lines', name=f'VY', ), row=2, col=1) fig.add_trace(go.Scatter( x=data.TIME, y=data.V_Z, mode='lines', name=f'VZ', ), row=2, col=1) fig.add_trace(go.Scatter( x=data.TIME, y=data.P_X, mode='lines', name=f'PX', ), row=3, col=1) fig.add_trace(go.Scatter( x=data.TIME, y=data.P_Y, mode='lines', name=f'PY', ), row=3, col=1) fig.add_trace(go.Scatter( x=data.TIME, y=data.P_Z, mode='lines', name=f'PZ', ), row=3, col=1) return fig
def plotAcc_Kalib(data1, data2, data3, data4, data5, data6): figure = make_subplots(3, 2) figure.add_trace(go.Scatter( x=data1.TIME, y=data1.ACC_X, mode='lines', name=f'ACC X where X = 9.81', ), row=1, col=1) figure.add_trace(go.Scatter( x=data1.TIME, y=data1.ACC_Y, mode='lines', name=f'ACC Y where X = 9.81', ), row=1, col=1) figure.add_trace(go.Scatter( x=data1.TIME, y=data1.ACC_Z, mode='lines', name=f'ACC Z where X = 9.81', ), row=1, col=1) figure.add_trace(go.Scatter( x=data2.TIME, y=data2.ACC_X, mode='lines', name=f'ACC X where X = -9.81', ), row=1, col=2) figure.add_trace(go.Scatter( x=data2.TIME, y=data2.ACC_Y, mode='lines', name=f'ACC Y where X = -9.81', ), row=1, col=2) figure.add_trace(go.Scatter( x=data2.TIME, y=data2.ACC_Z, mode='lines', name=f'ACC Z where X = -9.81', ), row=1, col=2) figure.add_trace(go.Scatter( x=data3.TIME, y=data3.ACC_X, mode='lines', name=f'ACC X where Y = 9.81', ), row=2, col=1) figure.add_trace(go.Scatter( x=data3.TIME, y=data3.ACC_Y, mode='lines', name=f'ACC Y where Y = 9.81', ), row=2, col=1) figure.add_trace(go.Scatter( x=data3.TIME, y=data3.ACC_Z, mode='lines', name=f'ACC Z where Y = 9.81', ), row=2, col=1) figure.add_trace(go.Scatter( x=data4.TIME, y=data4.ACC_X, mode='lines', name=f'ACC X where Y = -9.81', ), row=2, col=2) figure.add_trace(go.Scatter( x=data4.TIME, y=data4.ACC_Y, mode='lines', name=f'ACC Y where Y = -9.81', ), row=2, col=2) figure.add_trace(go.Scatter( x=data4.TIME, y=data4.ACC_Z, mode='lines', name=f'ACC Z where Y = -9.81', ), row=2, col=2) figure.add_trace(go.Scatter( x=data5.TIME, y=data5.ACC_X, mode='lines', name=f'ACC X where Z = 9.81', ), row=3, col=1) figure.add_trace(go.Scatter( x=data5.TIME, y=data5.ACC_Y, mode='lines', name=f'ACC Y where Z = 9.81', ), row=3, col=1) figure.add_trace(go.Scatter( x=data5.TIME, y=data5.ACC_Z, mode='lines', name=f'ACC Z where Z = 9.81', ), row=3, col=1) figure.add_trace(go.Scatter( x=data6.TIME, y=data6.ACC_X, mode='lines', name=f'ACC X where Z = -9.81', ), row=3, col=2) figure.add_trace(go.Scatter( x=data6.TIME, y=data6.ACC_Y, mode='lines', name=f'ACC Y where Z = -9.81', ), row=3, col=2) figure.add_trace(go.Scatter( x=data6.TIME, y=data6.ACC_Z, mode='lines', name=f'ACC Z where Z = -9.81', ), row=3, col=2) return figure
def Ks2(data, flag): # Bin que vai permitir agrupar os valores por score bin = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100] #Contar o total de ocorrências de bons e mals pagadores dfTotal = data.ALVO.value_counts().to_frame() dfTotal = dfTotal.rename(index={0: "Bom", 1: "Mal"}) totais = dfTotal.T #totais.at['ALVO','Bom'] e totais.at['ALVO','Mal'] #Contando por percentual de decil df = data.groupby(pd.cut(data.SCORE, bins=bin))['ALVO'].value_counts() # Desempilhando o Dataframe df = df.unstack() # Calculando o percentual do decil comparado ao valor total df["PercentualBom"] = (df[0] / totais.at['ALVO', 'Bom']) df["PercentualMal"] = (df[1] / totais.at['ALVO', 'Mal']) # Calculando o Cumulativo df["PercentualBomAcc"] = df['PercentualBom'].rolling(min_periods=1, window=10).sum() df["PercentualMalAcc"] = df['PercentualMal'].rolling(min_periods=1, window=10).sum() # Calculando o ks2 por decil df['KS2'] = abs(df["PercentualBomAcc"] - df["PercentualMalAcc"]) # Label label = [ "0-10", "10-20", "20-30", "30-40", "40-50", "50-60", "60-70", "70-80", "80-90", "90-100" ] flag = flag.strip().lower() if flag == "no": # Texto da Tabela descrevendo gráfico desc = """O KS2 é uma métrica utilizada para sabermos quanto o modelo discrimina os bons dos maus clientes. Seu valor é a maior diferença das distribuições acumuladas dos dois públicos analisados. Quanto maior o KS2, melhor será a discriminação dos dois públicos pelo modelo em análise.""" #fig = go.Figure() # Fazendo os subplots para colocar a descrição e o gráfico na mesma imagem fig = make_subplots(rows=1, cols=3, specs=[[{ "type": "table" }, { "colspan": 2 }, None]]) # Add Table fig.add_trace(go.Table(header=dict(values=["Descrição"], font=dict(size=10), align="left"), cells=dict(values=[desc], align="left")), row=1, col=1) # Add linha de Bom fig.add_trace( go.Scatter(x=label, y=df.PercentualBomAcc, hovertemplate="%{x},%{y}", name="Bom")) # Add linha de Mal fig.add_trace(go.Scatter(x=label, y=df.PercentualMalAcc, hovertemplate="%{x},%{y}", name="Mal"), row=1, col=2) # Add linha de KS2 fig.add_trace(go.Scatter(x=label, y=df.KS2, name="KS2"), row=1, col=2) fig.update_xaxes(title_text="Faixa de Score", row=1, col=2) fig.update_yaxes(title_text="% População (AC)", row=1, col=2) fig.update_yaxes(tickformat=".3%", row=1, col=2) #Add Formatação do Gráfico fig.update_layout(title={ 'text': "KS2", 'y': 0.9, 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top' }, ) fig.show() return fig elif flag == "yes": pre_fig = make_subplots(rows=1, cols=2, specs=[[{"colspan": 2}, None]]) # Add linha de Bom pre_fig.add_trace( go.Scatter(x=label, y=df.PercentualBomAcc, hovertemplate="%{x},%{y}", name="Bom")) # Add linha de Mal pre_fig.add_trace(go.Scatter(x=label, y=df.PercentualMalAcc, hovertemplate="%{x},%{y}", name="Mal"), row=1, col=1) # Add linha de KS2 pre_fig.add_trace(go.Scatter(x=label, y=df.KS2, name="KS2"), row=1, col=1) pre_fig.update_xaxes(title_text="Faixa de Score", row=1, col=1) pre_fig.update_yaxes(title_text="% População (AC)", row=1, col=1) pre_fig.update_yaxes(tickformat=".3%", row=1, col=1) #Add Formatação do Gráfico pre_fig.update_layout(title={ 'text': "KS2", 'y': 0.9, 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top' }, ) pre_fig.show() desc = str(input("digite a descrição desejada: ")) # Fazendo os subplots para colocar a descrição e o gráfico na mesma imagem fig = make_subplots(rows=1, cols=3, specs=[[{ "type": "table" }, { "colspan": 2 }, None]]) # Add Table fig.add_trace(go.Table(header=dict(values=["Descrição"], font=dict(size=10), align="left"), cells=dict(values=[desc], align="left")), row=1, col=1) # Add linha de Bom fig.add_trace( go.Scatter(x=label, y=df.PercentualBomAcc, hovertemplate="%{x},%{y}", name="Bom")) # Add linha de Mal fig.add_trace(go.Scatter(x=label, y=df.PercentualMalAcc, hovertemplate="%{x},%{y}", name="Mal"), row=1, col=2) # Add linha de KS2 fig.add_trace(go.Scatter(x=label, y=df.KS2, name="KS2"), row=1, col=2) fig.update_xaxes(title_text="Faixa de Score", row=1, col=2) fig.update_yaxes(title_text="% População (AC)", row=1, col=2) fig.update_yaxes(tickformat=".3%", row=1, col=2) #Add Formatação do Gráfico fig.update_layout(title={ 'text': "KS2", 'y': 0.9, 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top' }, ) fig.show() return fig else: raise " Flag invalida, por favor digite 'YES' ou 'NO' "
def build_tree_map(df, average_score = 0.5, maxdepth = None, column_nm = { 'id':'id', 'label':'labels', 'parent':'parent', 'value':'value', 'color':'color' }, value_name = '# docs', color_name = 'Avg. Similarity'): """ Can demonstrate a single or a number of dataframes as a hierarchical treemap and choose the depth showed at any time. Optionality: - When fed a list of dataframse it can show them as treemaps side-by-side (note that space can be insufficient for more than 2 or 3 at a time) - maxdepth = None shows all the data but can be used as parameter to restrict only so many layers at a time Args: df (dataframe or list of dataframes): Mandatory columns must match spec in column_nm. Need columns for: id, label, parent, value, color average_score (float, optional): Score used as midpoint for plot colors, defaults to 0.5 maxdepth (int, optional): Number of levels of hierarchy to show, min 2, defaults to None column_nm (dict, optional): Set of column mappings for the mandatory tree map fields. Need columns for: id, label, parent, value, color value_name (string, optional): Hovertext label for 'value' values from dataframe, defaults to 'Label' color_name (string, optional): Hovertext label for 'color' values from dataframe, defaults to 'Color' Returns: Interactive plotly treemap """ if isinstance(df,list): pass elif isinstance(df,pd.core.frame.DataFrame): df=[df] else: print('df of not expected format') # Assert mandatory columns are present in dataframe for (i, df_i) in enumerate(df): for m in column_nm: assert(column_nm[m] in df_i.columns) fig = make_subplots(1, len(df), specs=[[{"type": "domain"}]*len(df)],) for (i, df_all_trees) in enumerate(df): fig.add_trace(go.Treemap( ids=df_all_trees[column_nm['id']], labels=df_all_trees[column_nm['label']], parents=df_all_trees[column_nm['parent']], values=df_all_trees[column_nm['value']], branchvalues='total', marker=dict( colors=df_all_trees[column_nm['color']], colorscale='RdBu', cmid=average_score), hovertemplate='<b>%{label} </b> <br> '+value_name+': %{value}<br>'+color_name+': %{color:.2f}', name='' ), 1, i+1) if maxdepth: if maxdepth < 2: print('try maxdepth > 1') fig.update_traces(maxdepth=maxdepth) fig.update_layout(margin=dict(t = 30, b = 10, r = 10, l = 10)) #uniformtext_minsize=12, uniformtext_mode='show') fig.show()
def hdf5_gdf_plot_spike_raster(spike_recorders, input_region=None, fig=None, show=True): """ Create a spike raster plot from an HDF5 group of spike recorders saved from NEST gdf files. Each HDF5 dataset includes the spike timings of the recorded cell populations, with spike times in the first row and neuron IDs in the second row. """ cell_ids = [ np.unique(spike_recorders[k][:, 1]) for k in spike_recorders.keys() ] x = {} y = {} colors = {} ids = {} for cell_id, dataset in spike_recorders.items(): data = dataset[:, 0] neurons = dataset[:, 1] attrs = dict(dataset.attrs) label = attrs["label"] colors[label] = attrs["color"] if not label in x: x[label] = [] if not label in y: y[label] = [] if not label in colors: colors[label] = attrs["color"] if not label in ids: ids[label] = 0 cell_id = ids[label] ids[label] += 1 # Add the spike timings on the X axis. x[label].extend(data) # Set the cell id for the Y axis of each added spike timing. y[label].extend(neurons) subplots_fig = make_subplots(cols=1, rows=len(x), subplot_titles=list(x.keys())) _min = float("inf") _max = -float("inf") for i, (c, t) in enumerate(x.items()): _min = min(_min, np.min(np.array(t))) _max = max(_max, np.max(np.array(t))) subplots_fig.update_xaxes(range=[_min, _max]) # Overwrite the layout and grid of the single plot that is handed to us # to turn it into a subplots figure. fig._grid_ref = subplots_fig._grid_ref fig._layout = subplots_fig._layout for i, l in enumerate((x.keys())): plot_spike_raster(x[l], y[l], label=l, fig=fig, row=i + 1, col=1, show=False, color=colors[l], input_region=input_region, **kwargs) if show: fig.show() return fig
def _gear_facet_grid(df, x, y, facet_row, facet_col, color_name, colormapping, color_type, num_of_rows, num_of_cols, facet_row_labels, facet_col_labels, trace_type, flipped_rows, flipped_cols, SUBPLOT_SPACING, marker_color, text_name, jitter, kwargs_trace, kwargs_marker): print( "DEBUG check: within _facet_grid, trace type is {}".format(trace_type), file=sys.stderr) fig = make_subplots(rows=num_of_rows, cols=num_of_cols, shared_xaxes=True, shared_yaxes=True, horizontal_spacing=SUBPLOT_SPACING, vertical_spacing=SUBPLOT_SPACING, print_grid=False) if PLOT_LOGGING: print("DEBUG: facet_row:{0} facet_col:{1}".format( facet_row, facet_col), file=sys.stderr) else: print("DEBUG: plot debugging appears to be off", file=sys.stderr) # 'color_name' will be omitted if 'None' # Do not add color_name for 'numerical' data since that will blow up the number of traces priority_groups = _build_priority_groups(facet_row, facet_col, None, x) if color_type == "numerical" \ else _build_priority_groups(facet_row, facet_col, color_name, x) # If replicates are present, use mean and stdev of expression data as datapoints if 'replicate' in df.columns and trace_type != 'violin': grouped = df.groupby(priority_groups) if color_type == "numerical": df = grouped.agg({ color_name: ['mean'], "raw_value": ['mean', 'std'] }) \ .dropna() \ .reset_index() else: df = grouped.agg({ "raw_value": ['mean', 'std'] }) \ .dropna() \ .loc[:, 'raw_value'] \ .rename(columns=dict(mean='raw_value')) \ .reset_index() # When doing groupby for iteration purposes, we do not want the "x" column grouped # Violins will keep x, so they will be processed in the 'groupby' block to create multiple traces, instead of one if trace_type != "violin" or len(priority_groups) > 1: priority_groups.pop() # Assumes 'x' group is always the last item. # Map indexes for subplot ordering. Indexes start at 1 since plotting rows/cols start at 1 facet_row_groups = list(df.groupby(facet_row)) if facet_row else [] facet_row_indexes = { group[0]: idx for idx, group in enumerate(facet_row_groups, start=1) } facet_col_groups = list(df.groupby(facet_col)) if facet_col else [] facet_col_indexes = { group[0]: idx for idx, group in enumerate(facet_col_groups, start=1) } traces = [] row_idxs = [] col_idxs = [] annotations = [] names_in_legend = {} # Update kwargs_marker with default values, but higher-level assignments should supercede if color_type == None: kwargs_marker.setdefault('color', marker_color) # If there is nothing to group by there will only be one trace # Essentially no facet rows or columns, and no replicates if not len(priority_groups): trace = _create_trace(df, x, y, trace_type, kwargs_trace, kwargs_marker) # 'categorical' colortypes will always have the 'color_name' priority group in the list if color_type == "numerical": trace = _append_to_numerical_trace(trace, df, color_name, x, y, trace_type) # If applicable, add some jitter to the datapoints. # Attempting to replicate the "strip" plot function in plotly express if trace_type == "scatter" and _is_categorical(df[x]) and jitter: trace = _adjust_plot_for_jitter(trace, jitter, None) if text_name and text_name in df: trace['text'] = df[text_name] traces.append(trace) row_idxs.append( 1) # Do not need, but is a safeguard in case annotations are added col_idxs.append(1) else: # https://pandas.pydata.org/docs/user_guide/groupby.html#iterating-through-groups # Worth noting. If number of groups = 1 then 'name' is a string, else is a tuple for name, group in df.groupby(priority_groups): trace = _create_trace(group, x, y, trace_type, kwargs_trace, kwargs_marker) curr_color = None if color_type == "categorical": # If name is a tuple, color_name is last element curr_color = name if isinstance(name, tuple): curr_color = name[-1] # Making the assumption that kwargs_marker should not overwrite these marker attributes trace['marker']['color'] = colormapping[curr_color] trace['name'] = str(curr_color) elif color_type == "numerical": trace = _append_to_numerical_trace(trace, group, color_name, x, y, trace_type) # If applicable, add some jitter to the datapoints. # Attempting to replicate the "strip" plot function in plotly express if trace_type == "scatter" and _is_categorical( group[x]) and jitter: trace = _adjust_plot_for_jitter(trace, jitter, curr_color) if not facet_row and not facet_col: if text_name and text_name in group: trace['text'] = group[text_name] # Plotly workaround: # Once a trace name has been added to the legend, # we don't want to include future traces in the # legend with that name, otherwise we get legend # label explosion. (mostly with 'categorical' color_type) legend_key = name if color_type == "categorical": legend_key = curr_color if legend_key in names_in_legend: trace['showlegend'] = False else: names_in_legend[legend_key] = True traces.append(trace) # Now determine which plot this trace should go to. Facet column is first if row does not exist. if isinstance(name, tuple): row_idxs.append(facet_row_indexes[name[0]] if facet_row else 1) if facet_row: col_idxs.append( facet_col_indexes[name[1]] if facet_col else 1) else: col_idxs.append( facet_col_indexes[name[0]] if facet_col else 1) else: row_idxs.append(facet_row_indexes[name] if facet_row else 1) col_idxs.append(facet_col_indexes[name] if facet_col else 1) # Create annotations for each facet row or column for rowname in facet_row_indexes: label = _return_label(rowname, facet_row_labels, facet_row) annotations.append( _annotation_dict( label, (num_of_rows - facet_row_indexes[rowname]) + 1, # Order from top to bottom num_of_rows, # Adds to right of plot SUBPLOT_SPACING, 'row', flipped_rows)) for colname in facet_col_indexes: label = _return_label(colname, facet_col_labels, facet_col) annotations.append( _annotation_dict( label, facet_col_indexes[colname], num_of_cols, # Adds to bottom of plot SUBPLOT_SPACING, 'col', flipped_cols)) # Traces, row, and col lists should be 1-to-1-to-1 fig.add_traces(traces, rows=row_idxs, cols=col_idxs) return fig, annotations
l3 = arr3.tolist() # In[15]: from itertools import chain list1 = list(chain.from_iterable(l1)) print(list1) list2 = list(chain.from_iterable(l2)) print(list2) list3 = list(chain.from_iterable(l3)) print(list3) # In[21]: x2 = [2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018] fig = make_subplots(specs=[[{"secondary_y": True}]]) fig.add_trace( go.Scatter(x=x2, y=list1, name="Tree Cover Loss"), secondary_y=False, ) fig.add_trace( go.Scatter(x=x2, y=list2, name="Biomass Loss"), secondary_y=True, ) fig.add_trace( go.Scatter(x=x2, y=list3, name="CO2 Emissions"), secondary_y=True, )
def test_get_subplot(self): # Make Figure with subplot types fig = subplots.make_subplots( rows=4, cols=2, specs=[[{}, {'secondary_y': True}], [{'type': 'polar'}, {'type': 'ternary'}], [{'type': 'scene'}, {'type': 'geo'}], [{'type': 'domain', 'colspan': 2}, None]] ) fig.add_scatter(y=[2, 1, 3], row=1, col=1) fig.add_scatter(y=[2, 1, 3], row=1, col=2) fig.add_scatter(y=[1, 3, 2], row=1, col=2, secondary_y=True) fig.add_trace(go.Scatterpolar( r=[2, 1, 3], theta=[20, 50, 125]), row=2, col=1) fig.add_traces([go.Scatterternary(a=[.2, .1, .3], b=[.4, .6, .5])], rows=[2], cols=[2]) fig.add_scatter3d(x=[2, 0, 1], y=[0, 1, 0], z=[0, 1, 2], mode='lines', row=3, col=1) fig.add_scattergeo(lat=[0, 40], lon=[10, 5], mode='lines', row=3, col=2) fig.add_parcats( dimensions=[ {'values': ['A', 'A', 'B', 'A', 'B']}, {'values': ['a', 'a', 'a', 'b', 'b']}, ], row=4, col=1) fig.update_traces(uid=None) fig.update(layout_height=1200) # Check expected = Figure({ 'data': [ {'type': 'scatter', 'xaxis': 'x', 'y': [2, 1, 3], 'yaxis': 'y'}, {'type': 'scatter', 'xaxis': 'x2', 'y': [2, 1, 3], 'yaxis': 'y2'}, {'type': 'scatter', 'xaxis': 'x2', 'y': [1, 3, 2], 'yaxis': 'y3'}, {'r': [2, 1, 3], 'subplot': 'polar', 'theta': [20, 50, 125], 'type': 'scatterpolar'}, {'a': [0.2, 0.1, 0.3], 'b': [0.4, 0.6, 0.5], 'subplot': 'ternary', 'type': 'scatterternary'}, {'mode': 'lines', 'scene': 'scene', 'type': 'scatter3d', 'x': [2, 0, 1], 'y': [0, 1, 0], 'z': [0, 1, 2]}, {'geo': 'geo', 'lat': [0, 40], 'lon': [10, 5], 'mode': 'lines', 'type': 'scattergeo'}, {'dimensions': [{'values': ['A', 'A', 'B', 'A', 'B']}, {'values': ['a', 'a', 'a', 'b', 'b']}], 'domain': {'x': [0.0, 0.9400000000000001], 'y': [0.0, 0.19375]}, 'type': 'parcats'}], 'layout': { 'geo': {'domain': {'x': [0.5700000000000001, 0.9400000000000001], 'y': [0.26875, 0.4625]}}, 'height': 1200, 'polar': {'domain': {'x': [0.0, 0.37], 'y': [0.5375, 0.73125]}}, 'scene': {'domain': {'x': [0.0, 0.37], 'y': [0.26875, 0.4625]}}, 'ternary': {'domain': {'x': [0.5700000000000001, 0.9400000000000001], 'y': [0.5375, 0.73125]}}, 'xaxis': {'anchor': 'y', 'domain': [0.0, 0.37]}, 'xaxis2': {'anchor': 'y2', 'domain': [0.5700000000000001, 0.9400000000000001]}, 'yaxis': {'anchor': 'x', 'domain': [0.80625, 1.0]}, 'yaxis2': {'anchor': 'x2', 'domain': [0.80625, 1.0]}, 'yaxis3': {'anchor': 'x2', 'overlaying': 'y2', 'side': 'right'}} }) expected.update_traces(uid=None) # Make sure we have expected starting figure self.assertEqual(fig, expected) # (1, 1) subplot = fig.get_subplot(1, 1) self.assertEqual( subplot, SubplotXY( xaxis=fig.layout.xaxis, yaxis=fig.layout.yaxis)) # (1, 2) Primary subplot = fig.get_subplot(1, 2) self.assertEqual( subplot, SubplotXY( xaxis=fig.layout.xaxis2, yaxis=fig.layout.yaxis2)) # (1, 2) Primary subplot = fig.get_subplot(1, 2, secondary_y=True) self.assertEqual( subplot, SubplotXY( xaxis=fig.layout.xaxis2, yaxis=fig.layout.yaxis3)) # (2, 1) subplot = fig.get_subplot(2, 1) self.assertEqual( subplot, fig.layout.polar) # (2, 2) subplot = fig.get_subplot(2, 2) self.assertEqual( subplot, fig.layout.ternary) # (3, 1) subplot = fig.get_subplot(3, 1) self.assertEqual( subplot, fig.layout.scene) # (3, 2) subplot = fig.get_subplot(3, 2) self.assertEqual( subplot, fig.layout.geo) # (4, 1) subplot = fig.get_subplot(4, 1) domain = fig.data[-1].domain self.assertEqual( subplot, SubplotDomain(x=domain.x, y=domain.y))
# key takeaways st.markdown( '**ISIL is responsible for most of the attacks in the region**') ISIL_data = data2[ (data2.gname == 'Islamic State of Iraq and the Levant (ISIL)') & (data2.region_txt == 'Middle East & North Africa')] ISIL_casualty = ISIL_data.groupby( ['country_txt'])['ncasualty'].sum().sort_values(ascending=False) ISIL_weapon = ISIL_data.attacktype1_txt.value_counts() ISIL_target = ISIL_data.targtype1_txt.value_counts() # create subplots fig = make_subplots(rows=1, cols=3, shared_yaxes=False, horizontal_spacing=0.1) # plot each subplot fig.add_trace( go.Bar(x=ISIL_casualty[0:5].index, y=ISIL_casualty[0:5], name="Casualty"), 1, 1) fig.add_trace( go.Bar(x=ISIL_weapon[0:5].index, y=ISIL_weapon[0:5], name='Weapon'), 1, 2) fig.add_trace( go.Bar(x=ISIL_target[0:5].index, y=ISIL_target[0:5], name="Target"), 1, 3) # styling
from DPOD.models_handler import * from plotly.graph_objects import Scatter3d, Figure from plotly.subplots import make_subplots model_handler = ModelsHandler('data/kaggle') model_id = 5 vertices, _ = model_handler.model_id_to_vertices_and_triangles(model_id) color_array = model_handler.get_color_to_3dpoints_arrays(model_id) color_array_vertices = color_array.reshape(-1, 3)[::10] fig = make_subplots(rows=1, cols=2, specs=[[{ 'type': 'scatter3d' }, { 'type': 'scatter3d' }]]) fig.add_trace(Scatter3d(x=vertices[:, 0], y=vertices[:, 1], z=vertices[:, 2], mode='markers', marker_color=model_handler.color_points( vertices, model_id)), row=1, col=1) fig.add_trace(Scatter3d(x=vertices[:, 0], y=vertices[:, 1], z=vertices[:, 2], mode='markers', marker_color=model_handler.color_points( color_array_vertices, model_id)), row=1,
def plot(self, objects=None, baseline=None, max_vars=10, digits=3, rounding_function=np.around, bar_width=16, min_max=None, vcolors=None, title="Shapley Values", vertical_spacing=None, show=True): """ Plot function for Shap class. :param objects: object of Shap class or list or tuple containing such objects :param baseline: float, starting point of bars :param max_vars: int, maximum number of variables that shall be presented for for each model :param digits: int, number of columns in the plot grid :param rounding_function: a function to be used for rounding numbers :param bar_width: float, width of bars :param min_max: 2-tuple of float values, range of x-axis :param vcolors: 3-tuple of str values, color of bars :param title: str, the plot's title :param vertical_spacing: ratio of vertical space between the plots, by default it's 0.2/`number of plots` :param show: True shows the plot, False returns the plotly Figure object that can be edited or saved using `write_image()` method :return None or plotly Figure (see :param show) """ # are there any other objects to plot? if objects is None: n = 1 _result_list = [self.result.loc[self.result['B'] == 0, ].copy()] _intercept_list = [self.intercept] _prediction_list = [self.prediction] elif isinstance( objects, self.__class__): # allow for objects to be a single element n = 2 _result_list = [ self.result.loc[self.result['B'] == 0, ].copy(), objects.result.loc[objects.result['B'] == 0, ].copy() ] _intercept_list = [self.intercept, objects.intercept] _prediction_list = [self.prediction, objects.prediction] else: # objects as tuple or array n = len(objects) + 1 _result_list = [self.result.loc[self.result['B'] == 0, ].copy()] _intercept_list = [self.intercept] _prediction_list = [self.prediction] for ob in objects: if not isinstance(ob, self.__class__): raise TypeError("Some explanations aren't of Shap class") _result_list += [ob.result.loc[ob.result['B'] == 0, ].copy()] _intercept_list += [ob.intercept] _prediction_list += [ob.prediction] # TODO: add intercept and prediction list update for multi-class # deleted_indexes = [] # for i in range(n): # result = _result_list[i] # # if len(result['label'].unique()) > 1: # n += len(result['label'].unique()) - 1 # # add new data frames to list # _result_list += [v for k, v in result.groupby('label', sort=False)] # deleted_indexes += [i] # # _result_list = [j for i, j in enumerate(_result_list) if i not in deleted_indexes] model_names = [ result.iloc[0, result.columns.get_loc("label")] for result in _result_list ] if vertical_spacing is None: vertical_spacing = 0.2 / n fig = make_subplots(rows=n, cols=1, shared_xaxes=True, vertical_spacing=vertical_spacing, x_title='contribution', subplot_titles=model_names) plot_height = 78 + 71 if vcolors is None: vcolors = get_break_down_colors() if min_max is None: temp_min_max = [np.Inf, -np.Inf] else: temp_min_max = min_max for i in range(n): _result = _result_list[i] if _result.shape[0] <= max_vars: m = _result.shape[0] else: m = max_vars + 1 if baseline is None: baseline = _intercept_list[i] prediction = _prediction_list[i] df = prepare_data_for_shap_plot(_result, baseline, prediction, max_vars, rounding_function, digits) fig.add_shape(type='line', x0=baseline, x1=baseline, y0=0, y1=m - 1, yref="paper", xref="x", line={ 'color': "#371ea3", 'width': 1.5, 'dash': 'dot' }, row=i + 1, col=1) fig.add_bar( orientation="h", y=df['variable'].tolist(), x=df['contribution'].tolist(), textposition="outside", text=df['label_text'].tolist(), marker_color=[vcolors[int(c)] for c in df['sign'].tolist()], base=baseline, hovertext=df['tooltip_text'].tolist(), hoverinfo='text', hoverlabel={'bgcolor': 'rgba(0,0,0,0.8)'}, showlegend=False, row=i + 1, col=1) fig.update_yaxes( { 'type': 'category', 'autorange': 'reversed', 'gridwidth': 2, 'automargin': True, 'ticks': 'outside', 'tickcolor': 'white', 'ticklen': 10, 'fixedrange': True }, row=i + 1, col=1) fig.update_xaxes( { 'type': 'linear', 'gridwidth': 2, 'zeroline': False, 'automargin': True, 'ticks': "outside", 'tickcolor': 'white', 'ticklen': 3, 'fixedrange': True }, row=i + 1, col=1) plot_height += m * bar_width + (m + 1) * bar_width / 4 if min_max is None: cum = df.contribution.values + baseline min_max_margin = cum.ptp() * 0.15 temp_min_max[0] = np.min( [temp_min_max[0], cum.min() - min_max_margin]) temp_min_max[1] = np.max( [temp_min_max[1], cum.max() + min_max_margin]) plot_height += (n - 1) * 70 fig.update_xaxes({'range': temp_min_max}) fig.update_layout(title_text=title, title_x=0.15, font={'color': "#371ea3"}, template="none", height=plot_height, margin={ 't': 78, 'b': 71, 'r': 30 }) if show: fig.show( config={ 'displaylogo': False, 'staticPlot': False, 'modeBarButtonsToRemove': [ 'sendDataToCloud', 'lasso2d', 'autoScale2d', 'select2d', 'zoom2d', 'pan2d', 'zoomIn2d', 'zoomOut2d', 'resetScale2d', 'toggleSpikelines', 'hoverCompareCartesian', 'hoverClosestCartesian' ] }) else: return fig
def generate_rank(data, args): num_cols = 2 fig = {} layouts = [] num_groups = len(data.index.unique()) num_rows = math.ceil(num_groups / num_cols) fig = subplots.make_subplots(rows=num_rows, cols=num_cols, shared_yaxes=True, print_grid=False) r = 1 c = 1 range_y = [data['Value'].min(), data['Value'].max() + 1] for index in data.index.unique(): gdata = data.loc[index, :].dropna(how='all').groupby( 'Name', as_index=False).mean().sort_values(by='Value', ascending=False) gdata = gdata.reset_index().reset_index() cols = ['x', 'group', 'name', 'y'] cols.extend(gdata.columns[4:]) gdata.columns = cols gfig = get_simple_scatterplot(gdata, args) trace = gfig['data'].pop() glayout = gfig['layout']['annotations'] for l in glayout: nlayout = dict(x=l.x, y=l.y, xref='x' + str(c), yref='y' + str(r), text=l.text, showarrow=True, ax=l.ax, ay=l.ay, font=l.font, align='center', arrowhead=1, arrowsize=1, arrowwidth=1, arrowcolor='#636363') layouts.append(nlayout) trace.name = index fig.append_trace(trace, r, c) if c >= num_cols: r += 1 c = 1 else: c += 1 fig['layout'].update( dict(height=args['height'], width=args['width'], title=args['title'], xaxis={ "title": args['x_title'], 'autorange': True }, yaxis={ "title": args['y_title'], 'range': range_y }, template='plotly_white')) fig['layout'].annotations = [ dict(xref='paper', yref='paper', showarrow=False, text='') ] + layouts return fig
def generate_profit_graph(pairs: str, data: Dict[str, pd.DataFrame], trades: pd.DataFrame, timeframe: str, stake_currency: str) -> go.Figure: # Combine close-values for all pairs, rename columns to "pair" try: df_comb = combine_dataframes_with_mean(data, "close") except ValueError: raise OperationalException( "No data found. Please make sure that data is available for " "the timerange and pairs selected.") # Trim trades to available OHLCV data trades = extract_trades_of_period(df_comb, trades, date_index=True) if len(trades) == 0: raise OperationalException('No trades found in selected timerange.') # Add combined cumulative profit df_comb = create_cum_profit(df_comb, trades, 'cum_profit', timeframe) # Plot the pairs average close prices, and total profit growth avgclose = go.Scatter( x=df_comb.index, y=df_comb['mean'], name='Avg close price', ) fig = make_subplots(rows=5, cols=1, shared_xaxes=True, row_heights=[1, 1, 1, 0.5, 1], vertical_spacing=0.05, subplot_titles=[ "AVG Close Price", "Combined Profit", "Profit per pair", "Parallelism", "Underwater", ]) fig['layout'].update(title="Freqtrade Profit plot") fig['layout']['yaxis1'].update(title='Price') fig['layout']['yaxis2'].update(title=f'Profit {stake_currency}') fig['layout']['yaxis3'].update(title=f'Profit {stake_currency}') fig['layout']['yaxis4'].update(title='Trade count') fig['layout']['yaxis5'].update(title='Underwater Plot') fig['layout']['xaxis']['rangeslider'].update(visible=False) fig.update_layout(modebar_add=["v1hovermode", "toggleSpikeLines"]) fig.add_trace(avgclose, 1, 1) fig = add_profit(fig, 2, df_comb, 'cum_profit', 'Profit') fig = add_max_drawdown(fig, 2, trades, df_comb, timeframe) fig = add_parallelism(fig, 4, trades, timeframe) fig = add_underwater(fig, 5, trades) for pair in pairs: profit_col = f'cum_profit_{pair}' try: df_comb = create_cum_profit(df_comb, trades[trades['pair'] == pair], profit_col, timeframe) fig = add_profit(fig, 3, df_comb, profit_col, f"Profit {pair}") except ValueError: pass return fig
font=dict(size=24)), range=[3e3, 18e3], ticks='inside', tickangle=0, showline=True, linewidth=2, linecolor='black', gridcolor='rgba(0,0,0,0.25)', zeroline=False, mirror='ticks'), plot_bgcolor='white' ) grad_plot_data = [go.Scatter(x=[], y=[], yaxis='y1', xaxis='x1'), go.Scatter(x=[], y=[], yaxis='y2', xaxis='x2'), ] fig = make_subplots(rows=2, cols=1, vertical_spacing=0, shared_xaxes=True) fig.append_trace(go.Scatter(x=[], y=[]), row=1, col=1) fig.append_trace(go.Scatter(x=[], y=[]), row=2, col=1) fig.update_layout(width=1000, height=800, margin=dict(l=75, r=50, t=30, b=50), yaxis=dict(title=dict(text='df/dX', font=dict(size=24)), range=[-0.5, 0.5], ticks='inside', showline=True, linewidth=2, linecolor='black', gridcolor='rgba(0,0,0,0.25)', zeroline=False, mirror='ticks',), yaxis2=dict(title=dict(text='SNR (pixel<sup>-1</sup>)',
def resultados(relatorio, pasta, tabela_opr, tabela_mdl): print("Gerando Relatório de Resultados das Operações...") # In[4]: # DataFrame principal. dados_STG_OPR_ITT = pd.read_excel(tabela_opr) # DataFrame de Operações. # DataFrame para validação (esse será usado para validar as modalidades do principal). dados_STG_MDL = pd.read_excel(tabela_mdl) # DataFrame das Modalidades. #dados_STG_OPR_ITT # In[5]: #dados_STG_OPR_ITT # ## Filtrando os casos especiais e analisando o preenchimento das células # In[6]: # Tratando as datas. dados_STG_OPR_ITT.DAT_RSS_FNT_ITT = pd.to_datetime( dados_STG_OPR_ITT.DAT_RSS_FNT_ITT, format='%Y-%m-%d', errors='coerce') dados_STG_OPR_ITT.DAT_INC_DBO = pd.to_datetime( dados_STG_OPR_ITT.DAT_INC_DBO, format='%Y-%m-%d', errors='coerce') # Completando espaços vázios com zeros. dados_STG_OPR_ITT = dados_STG_OPR_ITT.fillna(0) dict_tipo = { 'ID_STG_OPR_ITT': np.int64, 'VLR_CTRD_CSC': np.float64, 'QTD_PCL': np.int64, 'VLR_SDO_DDR': np.float64, 'QTD_CLI_CAD_POS': np.int64, 'QTD_OPR': np.int64, 'ID_FNT_ITT': np.int64, 'ID_MDL': str, 'DES_TIP_PSS': str } for coluna, tipo in dict_tipo.items(): dados_STG_OPR_ITT[coluna] = dados_STG_OPR_ITT[coluna].astype(tipo) #dados_STG_OPR_ITT.dtypes # ## Convertendo os valores para acrescentar dois decimais # In[7]: # Colunas a serem convertidas. colunas = ['VLR_CTRD_CSC', 'VLR_SDO_DDR'] for coluna in colunas: # print('-' * 60 + f'\nColuna: {coluna}\n') for index, antes in enumerate(dados_STG_OPR_ITT[coluna].dropna()): depois = antes * 0.01 dados_STG_OPR_ITT[coluna] = dados_STG_OPR_ITT[coluna].replace( antes, depois) # Trocando o velho pelo novo. # if index < 10: # Essa parte serve somente para limitar a amostragem dos valores. # print(f'R${depois:>20.2f}') # Mostrando alguns exemplos. # print('-' * 60) # ## Adicionando uma nova coluna (MDL_DESCRICAO) ao DataFrame principal # ### Primeiramente criando um dicionário com o código e sua respectiva descrição para facilitar o processo # In[8]: # Escolhendo as colunas necessárias. new = dados_STG_MDL[['COD_MDL', 'DES_MDL']] dictionary = new.set_index('COD_MDL').T.to_dict( 'list') # Convertendo em um dicionário (valores em lista). for key, value in dictionary.items(): dictionary[key] = str(value).strip( "['']") # Convertendo os valores (em lista) para string. #dictionary # Mostrando o dicionário final. # ### Linha por linha atribuindo ao respectivo código a sua descrição # In[9]: # Linha por linha atribuindo ao respectivo código a sua descrição x = [ dictionary.get(codigo, f'{codigo} (MODALIDADE NAO ENCONTRADA)') for index, codigo in enumerate(dados_STG_OPR_ITT['ID_MDL']) ] dados_STG_OPR_ITT['MDL_DESCRICAO'] = x #dados_STG_OPR_ITT[['ID_MDL', 'MDL_DESCRICAO']].head() # ## Analisando a soma das operações por modalidades # In[10]: #Cria um dataframe analisar = dados_STG_OPR_ITT[['MDL_DESCRICAO', 'QTD_OPR']].dropna() operacoes = analisar.groupby('MDL_DESCRICAO').sum().sort_values( by='QTD_OPR', ascending=False) #operacoes # ### Criando um gráfico # In[11]: #Define escalas para o tamanho máximo padrão dos gráficos operacoes_range = [0, round(max(operacoes.QTD_OPR))] #Filtra valores menores ou iguais a 1 milhão operacoes_menor = operacoes[operacoes['QTD_OPR'] > 0] operacoes_menor = operacoes_menor[operacoes_menor['QTD_OPR'] <= 1000] operacoes_menor = operacoes_menor.sort_values(by='QTD_OPR', ascending=True) #Filtra valores menores ou iguais a 1 milhão operacoes_medio = operacoes[operacoes['QTD_OPR'] > 1000] operacoes_medio = operacoes_medio[operacoes_medio['QTD_OPR'] <= 50000] operacoes_medio = operacoes_medio.sort_values(by='QTD_OPR', ascending=True) #Filtra valores maiores que 1 milhão e menores e iguais a 5 bilhões operacoes_maior = operacoes[operacoes['QTD_OPR'] > 50000] operacoes_maior = operacoes_maior.sort_values(by='QTD_OPR', ascending=True) fig = make_subplots(rows=3, cols=1, row_heights=[0.3, 0.6, 0.4]) fig.add_trace(go.Bar(y=operacoes_menor.index, x=operacoes_menor['QTD_OPR'], orientation='h', marker=dict(color='#112244')), row=3, col=1) fig.add_trace(go.Bar(y=operacoes_medio.index, x=operacoes_medio['QTD_OPR'], orientation='h', marker=dict(color='#112244')), row=2, col=1) fig.add_trace(go.Bar(y=operacoes_maior.index, x=operacoes_maior['QTD_OPR'], orientation='h', marker=dict(color='#112244')), row=1, col=1) fig.update_yaxes(showline=True, linewidth=1, linecolor='#717171') fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='#D9D9DE') fig.update_xaxes(range=operacoes_range) fig.update_layout(dict(plot_bgcolor='#FFFFFF', paper_bgcolor='#FFFFFF')) fig.update_layout(showlegend=False) #iplot(fig) fig.write_html(pasta + "/opr_soma_opr_por_mdl.html") # ## Analisando a soma das parcelas por modalidades # In[12]: #Cria um dataframe analisar = dados_STG_OPR_ITT[['MDL_DESCRICAO', 'QTD_PCL']].dropna() parcelas = analisar.groupby('MDL_DESCRICAO').sum().sort_values( by='QTD_PCL', ascending=False) #parcelas # ### Criando um gráfico # In[13]: #Define escalas para o tamanho máximo padrão dos gráficos parcelas_range = [0, round(max(parcelas.QTD_PCL))] #Filtra valores menores ou iguais a 1 milhão parcelas_menor = parcelas[parcelas['QTD_PCL'] > 0] parcelas_menor = parcelas_menor[parcelas_menor['QTD_PCL'] <= 500] parcelas_menor = parcelas_menor.sort_values(by='QTD_PCL', ascending=True) #Filtra valores menores ou iguais a 1 milhão parcelas_medio = parcelas[parcelas['QTD_PCL'] > 500] parcelas_medio = parcelas_medio[parcelas_medio['QTD_PCL'] <= 5000] parcelas_medio = parcelas_medio.sort_values(by='QTD_PCL', ascending=True) #Filtra valores maiores que 1 milhão e menores e iguais a 5 bilhões parcelas_maior = parcelas[parcelas['QTD_PCL'] > 5000] parcelas_maior = parcelas_maior.sort_values(by='QTD_PCL', ascending=True) fig = make_subplots(rows=1, cols=3, column_widths=[0.6, 0.2, 0.5]) fig.add_trace(go.Bar(x=parcelas_menor.index, y=parcelas_menor['QTD_PCL'], marker=dict(color='#3749E9')), row=1, col=1) fig.add_trace(go.Bar(x=parcelas_medio.index, y=parcelas_medio['QTD_PCL'], marker=dict(color='#3749E9')), row=1, col=2) fig.add_trace(go.Bar(x=parcelas_maior.index, y=parcelas_maior['QTD_PCL'], marker=dict(color='#3749E9')), row=1, col=3) fig.update_xaxes(showline=True, linewidth=1, linecolor='#717171') fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='#D9D9DE') fig.update_yaxes(range=parcelas_range) fig.update_layout(dict(plot_bgcolor='#FFFFFF', paper_bgcolor='#FFFFFF')) fig.update_layout(showlegend=False) #iplot(fig) fig.write_html(pasta + "/opr_soma_pcl_por_mdl.html") # ## Analisando a soma de clientes por modalidades # In[14]: #Cria um dataframe analisar = dados_STG_OPR_ITT[['MDL_DESCRICAO', 'QTD_CLI_CAD_POS']].dropna() clientes = analisar.groupby('MDL_DESCRICAO').sum().sort_values( by='QTD_CLI_CAD_POS', ascending=False) #clientes # ### Criando um gráfico # In[15]: #Define escalas para o tamanho máximo padrão dos gráficos clientes_range = [0, round(max(clientes.QTD_CLI_CAD_POS))] #Filtra valores menores ou iguais a 1 milhão clientes_menor = clientes[clientes['QTD_CLI_CAD_POS'] > 0] clientes_menor = clientes_menor[clientes_menor['QTD_CLI_CAD_POS'] <= 1000] clientes_menor = clientes_menor.sort_values(by='QTD_CLI_CAD_POS', ascending=True) #Filtra valores menores ou iguais a 1 milhão clientes_medio = clientes[clientes['QTD_CLI_CAD_POS'] > 1000] clientes_medio = clientes_medio[ clientes_medio['QTD_CLI_CAD_POS'] <= 100000] clientes_medio = clientes_medio.sort_values(by='QTD_CLI_CAD_POS', ascending=True) #Filtra valores maiores que 1 milhão e menores e iguais a 5 bilhões clientes_maior = clientes[clientes['QTD_CLI_CAD_POS'] > 100000] clientes_maior = clientes_maior.sort_values(by='QTD_CLI_CAD_POS', ascending=True) fig = make_subplots(rows=1, cols=3, column_widths=[0.5, 0.5, 0.3]) fig.add_trace(go.Bar(x=clientes_menor.index, y=clientes_menor['QTD_CLI_CAD_POS'], marker=dict(color='#112244')), row=1, col=1) fig.add_trace(go.Bar(x=clientes_medio.index, y=clientes_medio['QTD_CLI_CAD_POS'], marker=dict(color='#112244')), row=1, col=2) fig.add_trace(go.Bar(x=clientes_maior.index, y=clientes_maior['QTD_CLI_CAD_POS'], marker=dict(color='#112244')), row=1, col=3) fig.update_xaxes(showline=True, linewidth=1, linecolor='#717171') fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='#D9D9DE') fig.update_yaxes(range=clientes_range) fig.update_layout(dict(plot_bgcolor='#FFFFFF', paper_bgcolor='#FFFFFF')) fig.update_layout(showlegend=False) #iplot(fig) fig.write_html(pasta + "/opr_soma_cliente_por_mdl.html") # ## Agrupando clientes e parcelas por modalidades # In[16]: #Plota em formato tabela e exporta para HTML data = [ go.Bar(x=clientes.index, y=clientes.QTD_CLI_CAD_POS, name='Número de Clientes', marker=dict(color='#3749E9')), go.Bar(x=parcelas.index, y=parcelas.QTD_PCL, name='Número de Parcelas', marker=dict(color='#112244')) ] fig = go.Figure(data=data) fig = go.Figure(data=data) fig.update_xaxes(showline=True, linewidth=1, linecolor='#717171') fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='#D9D9DE') fig.update_layout(dict(plot_bgcolor='#FFFFFF', paper_bgcolor='#FFFFFF')) fig.update_layout(xaxis={'categoryorder': 'total ascending'}, showlegend=True) #iplot(fig) fig.write_html(pasta + "/opr_clientes_parcelas.html") # ## Adicionando uma nova coluna (VLR_PAGO) ao DataFrame temporário # ### Primeiramente criando uma lista com o resultado da subtração # In[17]: # Selecionando somente as linhas com o ID_MDL igual a C01 (consórcio). analisar = dados_STG_OPR_ITT[dados_STG_OPR_ITT['ID_MDL'] == 'C01'] analisar = analisar[['VLR_CTRD_CSC', 'VLR_SDO_DDR' ]] # Selecionando somente as colunas necessárias. total = [] # Lista temporária. for index in analisar.index: # Efeituando a conta e jogando o resultado na lista. total.append( round( analisar['VLR_CTRD_CSC'][index] - analisar['VLR_SDO_DDR'][index], 2)) analisar['VLR_PAGO'] = total # Adicionando a lista. #analisar.head(10) # In[18]: #Plota em formato grafico lista_somas = [ analisar['VLR_SDO_DDR'].sum(), analisar['VLR_CTRD_CSC'].sum(), analisar['VLR_PAGO'].sum() ] lista_nomes = ['Saldo Devedor', 'Valor Contratado', 'Valor Pago'] data = [ go.Bar(x=lista_nomes, y=lista_somas, marker=dict(color=['#FFAA00', '#112244', '#00B7CC'])) ] fig = go.Figure(data=data) fig.update_xaxes(showline=True, linewidth=1, linecolor='#717171') fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='#D9D9DE') fig.update_layout(dict(plot_bgcolor='#FFFFFF', paper_bgcolor='#FFFFFF')) fig.update_layout(showlegend=False) #iplot(fig) fig.write_html(pasta + "/opr_grafico_valor_consorcio.html") # ## Dataframe resumido # In[19]: #Cria uma nova tabela com os dados resumidos tabela = dados_STG_OPR_ITT[[ 'MDL_DESCRICAO', 'QTD_PCL', 'QTD_CLI_CAD_POS', 'QTD_OPR' ]] tabela = tabela.groupby('MDL_DESCRICAO').sum() tabela = tabela.rename( columns={ 'QTD_PCL': 'Número de Parcelas', 'QTD_CLI_CAD_POS': 'Número de Clientes', 'QTD_OPR': 'Número de Operações' }).sort_values(by='Número de Parcelas', ascending=True) tabela = tabela.rename_axis('Modalidades', axis='columns') tabela.index.name = None visualiza = tabela.replace(to_replace=0, value=np.nan) visualiza = (visualiza.style.format(formatacao, na_rep="-").highlight_null( null_color='#F2F200').apply(highlight_max).apply(highlight_min)) write_to_html_file(visualiza, title='', filename=pasta + "/opr_tab_modalidades.html") visualiza # ## Calculando os indicadores gerais # In[20]: #Calcula os resultados totais da remessa total_vlr_contratado = dados_STG_OPR_ITT['VLR_CTRD_CSC'].sum() total_qtd_pcl = dados_STG_OPR_ITT['QTD_PCL'].sum() total_vlr_saldo_devedor = dados_STG_OPR_ITT['VLR_SDO_DDR'].sum() total_qtd_opr = dados_STG_OPR_ITT['QTD_OPR'].sum() #total_vlr_contratado, total_qtd_pcl, total_vlr_saldo_devedor, total_qtd_opr html_string = '''<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="author" content="git: diegosilva89, ud21"> <title>METAlitcs - Operações</title> <link rel="stylesheet" href="_style.css"> <link rel="icon" href=""> </head> <body> <main> <div class="header"> <div class="metalitcs"> <p><b>METAlitcs</b></p> </div> <div class="location"> <div class="leftbox"> <p>Resultados da Remessa</p> </div> <div class="rightbox"> <p>Operações</p> </div> </div> </div> <div class="container"> <div class="container-menu-col"> <div class="menu-col"> <div class="index"> <p>Validação das Tabelas</p> </div> <a style="text-decoration: none;" href="table_FNT.html"> <div class="stg"> <p>STG_FNT_ITT</p> </div> </a> <a style="text-decoration: none;" href="table_MVT.html"> <div class="stg"> <p>STG_MVT_CRD</p> </div> </a> <a style="text-decoration: none;" href="table_OPR.html"> <div class="stg"> <p>STG_OPR_ITT</p> </div> </a> <a style="text-decoration: none;" href="table_PGT.html"> <div class="stg"> <p>STG_PGT</p> </div> </a> <div class="index"> <p>Resultados da Remessa</p> </div> <a style="text-decoration: none;" href="result_MVT.html"> <div class="stg"> <p>Movimentações</p> </div> </a> <a style="text-decoration: none;" href="result_OPR.html"> <div class="stg-in"> <p>Operações</p> </div> </a> <a style="text-decoration: none;" href="result_PGT.html"> <div class="stg"> <p>Pagamentos</p> </div> </a> <div class="index"> <p>Índice de Pagamentos em Dia</p> </div> <a style="text-decoration: none;" href="index_MOD.html"> <div class="stg"> <p>Por Modalidades</p> </div> </a> <a style="text-decoration: none;" href="index_FaixaVAL.html"> <div class="stg"> <p>Por Faixas de Valores</p> </div> </a> </div> </div> <!-- content of graphs and etc. --> <div class="container-content-col"> <section id="graphs"> <div class="static_graph_container_02"> <div class="graph_one"> <!-- graph form --> <div class="head"> <div class="title">Valor Total Contratado</div> </div> <div class="content_percent"> <div class="percent_value">R$ ''' + str( "{:,.2f}".format(total_vlr_contratado)) + '''</div> </div> <!-- graph form --> </div> <div class="graph_two"> <!-- graph form --> <div class="head"> <div class="title"> Valor Total do Saldo Devedor</div> </div> <div class="content_percent"> <div class="percent_value">R$ ''' + str( "{:,.2f}".format(total_vlr_saldo_devedor)) + '''</div> </div> <!-- graph form --> </div> <div class="graph_three"> <!-- graph form --> <div class="head"> <div class="title">Quantidade Total de Parcelas</div> </div> <div class="content_percent"> <div class="percent_value">''' + str( int(total_qtd_pcl)) + '''</div> </div> <!-- graph form --> </div> <div class="graph_four"> <!-- graph form --> <div class="head"> <div class="title">Quantidade Total de Operações</div> </div> <div class="content_percent"> <div class="percent_value">''' + str( int(total_qtd_opr)) + '''</div> </div> <!-- graph form --> </div> </div> <div class="dynamic_graph_container_result_OPR"> <div class="graph_five"> <!-- graph form --> <div class="head"> <div class="title">Número Total de Operações</div> <div class="paragraph">Por modalidades</div> </div> <div class="content"> <iframe src=pags/opr_soma_opr_por_mdl.html></iframe> </div> <!-- graph form --> </div> <div class="graph_six"> <!-- graph form --> <div class="head"> <div class="title">Valores Totais de Consórcio</div> <div class="paragraph">Por valor contratado, saldo devedor e valor pago</div> </div> <div class="content"> <iframe src=pags/opr_grafico_valor_consorcio.html></iframe> </div> <!-- graph form --> </div> <div class="graph_seven"> <!-- graph form --> <div class="head"> <div class="title">Tabela de Operações por Modalidades</div> <div class="paragraph">Número de parcelas, número de clientes e número de operações</div> </div> <div class="content"> <iframe src=pags/opr_tab_modalidades.html></iframe> </div> <!-- graph form --> </div> <div class="graph_eight"> <!-- graph form --> <div class="head"> <div class="title">Comparação do Número Total de Clientes e Operações</div> <div class="paragraph">Por modalidades</div> </div> <div class="content"> <iframe src=pags/opr_clientes_parcelas.html></iframe> </div> <!-- graph form --> </div> <div class="graph_nine"> <!-- graph form --> <div class="head"> <div class="title">Número Total de Clientes</div> <div class="paragraph">Por modalidades</div> </div> <div class="content"> <iframe src=pags/opr_soma_cliente_por_mdl.html></iframe> </div> <!-- graph form --> </div> <div class="graph_ten"> <!-- graph form --> <div class="head"> <div class="title">Número Total de Parcelas</div> <div class="paragraph">Por modalidades</div> </div> <div class="content"> <iframe src=pags/opr_soma_pcl_por_mdl.html></iframe> </div> <!-- graph form --> </div> </div> </section> </div> </div> </main> </body> </html>''' f = open(relatorio + '/result_OPR.html', 'w', encoding='utf-8') f.write(html_string) f.close() print(f"O Relatório de Resultados das Operações foi criado com sucesso!")
def densityPlot(beta_f_space, Dists, key='Weighted'): years = [50, 75, 100] titles = ["Year {}".format(year) for year in years] fig = make_subplots(1, len(years), print_grid=False, subplot_titles=titles) dom = beta_f_space inds = ((dom >= 0) & (dom <= 5e-3)) for i, year in enumerate(years): # data = loadmat("{}/50-50 weight/Dist_{}yr.mat".format(quad_rule, year)) data = Dists if key == 'Weighted': if i == 0: fig.add_scatter(x=dom[inds] * 1000, y=data['Original'][inds], row=1, col=i + 1, name='Original Distribution', line=dict(color='#1f77b4', width=3), showlegend=True, legendgroup='Original Distribution') fig.add_scatter(x=dom[inds] * 1000, y=data['Nordhaus_year' + str(year)][inds], row=1, col=i + 1, name='Low Damage Function', line=dict(color='red', dash='dashdot', width=3), showlegend=True, legendgroup='Low Damage Function') fig.add_scatter(x=dom[inds] * 1000, y=data['Weitzman_year' + str(year)][inds], row=1, col=i + 1, name='High Damage Function', line=dict(color='green', dash='dash', width=3), showlegend=True, legendgroup='High Damage Function') else: fig.add_scatter(x=dom[inds] * 1000, y=data['Original'][inds], row=1, col=i + 1, name='Original Distribution', line=dict(color='#1f77b4', width=3), showlegend=False, legendgroup='Original Distribution') fig.add_scatter(x=dom[inds] * 1000, y=data['Nordhaus_year' + str(year)][inds], row=1, col=i + 1, name='Low Damage Function', line=dict(color='red', dash='dashdot', width=3), showlegend=False, legendgroup='Low Damage Function') fig.add_scatter(x=dom[inds] * 1000, y=data['Weitzman_year' + str(year)][inds], row=1, col=i + 1, name='High Damage Function', line=dict(color='green', dash='dash', width=3), showlegend=False, legendgroup='High Damage Function') elif key == 'High': if i == 0: fig.add_scatter(x=dom[inds] * 1000, y=data['Original'][inds], row=1, col=i + 1, name='Original Distribution', line=dict(color='#1f77b4', width=3), showlegend=True, legendgroup='Original Distribution') fig.add_scatter(x=dom[inds] * 1000, y=data['Weitzman_year' + str(year)][inds], row=1, col=i + 1, name='High Damage Function', line=dict(color='green', dash='dash', width=3), showlegend=True, legendgroup='High Damage Function') else: fig.add_scatter(x=dom[inds] * 1000, y=data['Original'][inds], row=1, col=i + 1, name='Original Distribution', line=dict(color='#1f77b4', width=3), showlegend=False, legendgroup='Original Distribution') fig.add_scatter(x=dom[inds] * 1000, y=data['Weitzman_year' + str(year)][inds], row=1, col=i + 1, name='High Damage Function', line=dict(color='green', dash='dash', width=3), showlegend=False, legendgroup='High Damage Function') elif key == 'Low': if i == 0: fig.add_scatter(x=dom[inds] * 1000, y=data['Original'][inds], row=1, col=i + 1, name='Original Distribution', line=dict(color='#1f77b4', width=3), showlegend=True, legendgroup='Original Distribution') fig.add_scatter(x=dom[inds] * 1000, y=data['Nordhaus_year' + str(year)][inds], row=1, col=i + 1, name='Low Damage Function', line=dict(color='red', dash='dashdot', width=3), showlegend=True, legendgroup='Low Damage Function') else: fig.add_scatter(x=dom[inds] * 1000, y=data['Original'][inds], row=1, col=i + 1, name='Original Distribution', line=dict(color='#1f77b4', width=3), showlegend=False, legendgroup='Original Distribution') fig.add_scatter(x=dom[inds] * 1000, y=data['Nordhaus_year' + str(year)][inds], row=1, col=i + 1, name='Low Damage Function', line=dict(color='red', dash='dashdot', width=3), showlegend=False, legendgroup='Low Damage Function') fig['layout'].update(title=key + " Damage Specification", showlegend=True, titlefont=dict(size=20), height=450) for i in range(len(years)): fig['layout']['yaxis{}'.format(i + 1)].update(showgrid=False) fig['layout']['xaxis{}'.format(i + 1)].update(showgrid=False) fig['layout']['yaxis1'].update(title=go.layout.yaxis.Title( text="Probability Density", font=dict(size=16))) fig['layout']['xaxis2'].update(title=go.layout.xaxis.Title( text="Climate Sensitivity", font=dict(size=16)), showgrid=False) fig = go.FigureWidget(fig) iplot(fig)
def generate_candlestick_graph( pair: str, data: pd.DataFrame, trades: pd.DataFrame = None, *, indicators1: List[str] = [], indicators2: List[str] = [], plot_config: Dict[str, Dict] = {}, ) -> go.Figure: """ Generate the graph from the data generated by Backtesting or from DB Volume will always be ploted in row2, so Row 1 and 3 are to our disposal for custom indicators :param pair: Pair to Display on the graph :param data: OHLCV DataFrame containing indicators and buy/sell signals :param trades: All trades created :param indicators1: List containing Main plot indicators :param indicators2: List containing Sub plot indicators :param plot_config: Dict of Dicts containing advanced plot configuration :return: Plotly figure """ plot_config = create_plotconfig(indicators1, indicators2, plot_config) rows = 2 + len(plot_config['subplots']) row_widths = [1 for _ in plot_config['subplots']] # Define the graph fig = make_subplots( rows=rows, cols=1, shared_xaxes=True, row_width=row_widths + [1, 4], vertical_spacing=0.0001, ) fig['layout'].update(title=pair) fig['layout']['yaxis1'].update(title='Price') fig['layout']['yaxis2'].update(title='Volume') for i, name in enumerate(plot_config['subplots']): fig['layout'][f'yaxis{3 + i}'].update(title=name) fig['layout']['xaxis']['rangeslider'].update(visible=False) # Common information candles = go.Candlestick(x=data.date, open=data.open, high=data.high, low=data.low, close=data.close, name='Price') fig.add_trace(candles, 1, 1) if 'buy' in data.columns: df_buy = data[data['buy'] == 1] if len(df_buy) > 0: buys = go.Scatter(x=df_buy.date, y=df_buy.close, mode='markers', name='buy', marker=dict( symbol='triangle-up-dot', size=9, line=dict(width=1), color='green', )) fig.add_trace(buys, 1, 1) else: logger.warning("No buy-signals found.") if 'sell' in data.columns: df_sell = data[data['sell'] == 1] if len(df_sell) > 0: sells = go.Scatter(x=df_sell.date, y=df_sell.close, mode='markers', name='sell', marker=dict( symbol='triangle-down-dot', size=9, line=dict(width=1), color='red', )) fig.add_trace(sells, 1, 1) else: logger.warning("No sell-signals found.") # TODO: Figure out why scattergl causes problems plotly/plotly.js#2284 if 'bb_lowerband' in data and 'bb_upperband' in data: bb_lower = go.Scatter( x=data.date, y=data.bb_lowerband, showlegend=False, line={'color': 'rgba(255,255,255,0)'}, ) bb_upper = go.Scatter( x=data.date, y=data.bb_upperband, name='Bollinger Band', fill="tonexty", fillcolor="rgba(0,176,246,0.2)", line={'color': 'rgba(255,255,255,0)'}, ) fig.add_trace(bb_lower, 1, 1) fig.add_trace(bb_upper, 1, 1) if ('bb_upperband' in plot_config['main_plot'] and 'bb_lowerband' in plot_config['main_plot']): del plot_config['main_plot']['bb_upperband'] del plot_config['main_plot']['bb_lowerband'] # Add indicators to main plot fig = add_indicators(fig=fig, row=1, indicators=plot_config['main_plot'], data=data) fig = plot_trades(fig, trades) # Volume goes to row 2 volume = go.Bar(x=data['date'], y=data['volume'], name='Volume', marker_color='DarkSlateGrey', marker_line_color='DarkSlateGrey') fig.add_trace(volume, 2, 1) # Add indicators to separate row for i, name in enumerate(plot_config['subplots']): fig = add_indicators(fig=fig, row=3 + i, indicators=plot_config['subplots'][name], data=data) return fig
def growthdensityPlot(beta_f_space, Dists): years = [50, 75, 100] titles = ["Year {}".format(year) for year in years] tilt_colors = [ '#FFFF00', '#FFE600', '#FFCC00', '#FFB300', '#FF9900', '#FF8000', '#FF6600', '#FF4D00', '#FF3300', '#FF1A00', '#FF0000' ] fig = make_subplots(1, len(years), print_grid=False, subplot_titles=titles) dom = beta_f_space inds = ((dom >= 0) & (dom <= 5e-3)) for i, year in enumerate(years): data = Dists if i == 0: fig.add_scatter(x=dom[inds] * 1000, y=data['Original'][inds], row=1, col=i + 1, name='Original Distribution', line=dict(color='#1f77b4', width=3), showlegend=True, legendgroup='Original Distribution') for j, tilt in enumerate( Dists['Year{}'.format(year)]['tilt_dist']): fig.add_scatter(x=dom[inds] * 1000, y=tilt[inds], row=1, col=i + 1, name='Tilted {}'.format(j + 1), line=dict(color=tilt_colors[j], dash='dash', width=2), showlegend=True, legendgroup='Tilted Densities {}'.format(j)) else: fig.add_scatter(x=dom[inds] * 1000, y=data['Original'][inds], row=1, col=i + 1, name='Original Distribution', line=dict(color='#1f77b4', width=3), showlegend=False, legendgroup='Original Distribution') for j, tilt in enumerate( Dists['Year{}'.format(year)]['tilt_dist']): fig.add_scatter(x=dom[inds] * 1000, y=tilt[inds], row=1, col=i + 1, name='Tilted {}'.format(j + 1), line=dict(color=tilt_colors[j], dash='dash', width=2), showlegend=False, legendgroup='Tilted Densities {}'.format(j)) fig['layout'].update( title="Worst Case Probabilities, Growth Damage Specification", showlegend=True, titlefont=dict(size=20), height=450) for i in range(len(years)): fig['layout']['yaxis{}'.format(i + 1)].update(showgrid=False) fig['layout']['xaxis{}'.format(i + 1)].update(showgrid=False) fig['layout']['yaxis1'].update(title=go.layout.yaxis.Title( text="Probability Density", font=dict(size=16))) fig['layout']['xaxis2'].update(title=go.layout.xaxis.Title( text="Climate Sensitivity", font=dict(size=16)), showgrid=False) fig = go.FigureWidget(fig) iplot(fig)
def getSAData(): global SADF, maxTests, fig, fig2, fig3, fig4, fig5 rows = [] for element in table.find('tbody').find_all('tr'): rows.append(element.get_text().split('\n')) Date = [row[1] for row in rows if (len(row) > 3)] Date = [element for element in Date if validate(element) == True] Total = [row[31] for row in rows if (len(row) > 25)] Total = [element for element in Total if len(element) > 0] Total = [element for element in Total if element[-1].isdigit()] Total = [s[3::] if s.isdigit() == False else s for s in Total] Tests = [row[7] for row in rows if (len(row) > 35)] Tests = [ element for element in Tests if (element.isdigit() or len(str(element)) == 0 or element[-1].isdigit()) ] Tests = [s[3::] if s.isdigit() == False else s for s in Tests] new = [] for element in Total: if element.isdigit(): pass else: for element2 in element: if element2.isdigit() == False: element = element.replace(element2, '') new.append(element) Total = [element for element in new if element != ''] print(len(Date), len(Tests), len(Total)) mapper = [{n: m} for n, m in list(zip(Date, Total))] SADF = pd.DataFrame(data=[d.values() for d in mapper], columns=['Total cases'], index=[list(d.keys())[0] for d in mapper]) SADF['Total cases'] = SADF['Total cases'].astype(float) SADF['Daily cases'] = SADF['Total cases'] - SADF['Total cases'].shift(1) SADF['Cumulative tests'] = Tests SADF.index.name = 'Date' SADF = SADF.replace('', np.nan) SADF = SADF.apply(pd.to_numeric) SADF['Daily tests'] = SADF['Cumulative tests'] - SADF[ 'Cumulative tests'].shift(1) maxTests = 100 SADF['Cases per {} tests'.format(maxTests)] = round( (SADF['Daily cases'] * maxTests) / SADF['Daily tests'], 3) SADF = SADF.replace([np.inf, -np.inf], np.nan) print(SADF) fig = subplots.make_subplots() fig['layout'].update( height=500, title='Cases per 100 tests for South Africa as of {}'.format( SADF.reset_index()['Date'].tail(1).item()), title_x=0.5, xaxis_title="Date", yaxis_title="Cases per 100 tests") fig['layout']['margin'] = {'l': 20, 'b': 30, 'r': 10, 't': 50} SADF = SADF.reset_index() SADF['Date'] = SADF['Date'].apply(lambda x: "2020-" + x) fig.append_trace( { 'x': SADF['Date'], 'y': SADF['Cases per {} tests'.format(maxTests)], 'type': 'bar', 'name': 'Cases per test' }, 1, 1) fig.append_trace( { 'x': SADF['Date'], 'y': SADF['Cases per {} tests'.format(maxTests)], 'type': 'scatter', 'name': 'Cases per test' }, 1, 1) fig2 = subplots.make_subplots() fig2['layout'].update( height=500, title='Daily cases reported in South Africa as of {}'.format( SADF.reset_index()['Date'].tail(1).item()), title_x=0.5, xaxis_title="Date", yaxis_title="Daily cases reported") fig2['layout']['margin'] = {'l': 20, 'b': 30, 'r': 10, 't': 50} fig2.append_trace( { 'x': SADF['Date'], 'y': SADF['Daily cases'], 'type': 'bar', 'name': 'Daily cases' }, 1, 1) fig2.append_trace( { 'x': SADF['Date'], 'y': SADF['Daily cases'], 'type': 'scatter', 'name': 'Daily cases' }, 1, 1) fig3 = subplots.make_subplots() fig3['layout'].update( height=500, title='Daliy tests reported in South Africa as of {}'.format( SADF.reset_index()['Date'].tail(1).item()), title_x=0.5, xaxis_title="Date", yaxis_title="Daily tests reported") fig3['layout']['margin'] = {'l': 20, 'b': 30, 'r': 10, 't': 50} fig3.append_trace( { 'x': SADF['Date'], 'y': SADF['Daily tests'], 'type': 'bar', 'name': 'Daily tests' }, 1, 1) fig3.append_trace( { 'x': SADF['Date'], 'y': SADF['Daily tests'], 'type': 'scatter', 'name': 'Daily tests' }, 1, 1) fig4 = subplots.make_subplots() fig4['layout'].update( height=500, title='Total cases reported in South Africa as of {}'.format( SADF.reset_index()['Date'].tail(1).item()), title_x=0.5, xaxis_title="Date", yaxis_title="Total cases reported") fig4['layout']['margin'] = {'l': 20, 'b': 30, 'r': 10, 't': 50} fig4.append_trace( { 'x': SADF['Date'], 'y': SADF['Total cases'], 'type': 'bar', 'name': 'Total cases' }, 1, 1) fig4.append_trace( { 'x': SADF['Date'], 'y': SADF['Total cases'], 'type': 'scatter', 'name': 'Total cases' }, 1, 1) fig5 = subplots.make_subplots() fig5['layout'].update( height=500, title='Total tests reported in South Africa as of {}'.format( SADF.reset_index()['Date'].tail(1).item()), title_x=0.5, xaxis_title="Date", yaxis_title="Total tests reported") fig5['layout']['margin'] = {'l': 20, 'b': 30, 'r': 10, 't': 50} fig5.append_trace( { 'x': SADF['Date'], 'y': SADF['Cumulative tests'], 'type': 'bar', 'name': 'Total tests' }, 1, 1) fig5.append_trace( { 'x': SADF['Date'], 'y': SADF['Cumulative tests'], 'type': 'scatter', 'name': 'Total tests' }, 1, 1)
def show_bbox_data(session_state, selected_image, label_col, colors): session_state.bbox_disp = st.sidebar.checkbox( "Display bounding box view", value=session_state.bbox_disp) session_state.graph = st.sidebar.checkbox("Display adjacency graph", value=session_state.graph) f = open(os.path.join(path, 'src', 'visualization', 'part_label.json'), ) with open(os.path.join(path, 'src', 'visualization', 'tree.json'), ) as fp: tree = json.load(fp) part_labels = json.load(f) part_labels = part_labels[session_state.object_type] labels = list(part_labels.keys()) part_labels[""] = 24 with open( os.path.join(anno_source, session_state.object_type, 'bbox', selected_image + '.json')) as fp: annotation_dict = json.load(fp) image_path = annotation_dict['image'] im_x_min, im_y_min, im_x_max, im_y_max = annotation_dict['bbox'] raw_image = cv2.imread(os.path.join(img_source, image_path + '.jpg')) raw_image = cv2.cvtColor(raw_image, cv2.COLOR_BGR2RGB) graph = make_subplots(rows=1, cols=2) part_image = raw_image.copy() graph.add_trace(px.imshow(part_image[im_x_min:im_x_max + 1, im_y_min:im_y_max + 1]).data[0], row=1, col=1) if session_state.gt_overlay: alpha = 0.2 graph.add_trace(px.imshow(part_image[im_x_min:im_x_max + 1, im_y_min:im_y_max + 1]).data[0], row=1, col=2) else: graph.add_trace(px.imshow( np.zeros((im_x_max - im_x_min, im_y_max - im_y_min, 3))).data[0], row=1, col=2) part_dict = annotation_dict['parts'] if part_dict and session_state.bbox_disp: bb_image = raw_image.copy() part_centers = np.zeros((24, 3)) for part in part_dict.keys(): x_min, y_min, x_max, y_max = part_dict[part]['bbox'] part_centers[part_labels[part] - 1] = [1, (x_min + x_max) / 2, (y_min + y_max) / 2] part_col = colors[part_labels[part] - 1] clr_str = 'rgb(' + str(int(part_col[0])) + ',' + str( int(part_col[1])) + ',' + str(int(part_col[1])) + ')' graph.add_shape(type="rect", xref="x", yref="y", y0=x_min - im_x_min, x0=y_min - im_y_min, y1=x_max - im_x_min, x1=y_max - im_y_min, line=dict(color=clr_str, width=1), row=1, col=2) labels = list(part_labels.keys()) parts_present = [ labels[i] for i in np.where(part_centers[:, 0] == 1)[0] ] part_centers = part_centers[np.where(part_centers[:, 0] == 1)] node_x = [x - im_x_min for _, x, y in part_centers] node_y = [y - im_y_min for _, x, y in part_centers] graph.add_trace( go.Scatter( x=node_y, y=node_x, mode='markers', hoverinfo='text', text=parts_present, marker=dict( showscale=False, reversescale=True, # colorscale='YlreGnBu', color=['red'] * len(node_x), size=10, opacity=0.0, colorbar=dict(thickness=15, ), line_width=2)), row=1, col=2) l = min(6, len(part_labels)) b = int(np.ceil(len(part_labels) / 6)) label_matrix = np.zeros((b, l)).astype('str') labels = list(part_labels.keys()) for i in range(b): for j in range(l): try: label_matrix[i][j] = labels[i * 6 + j] except: label_matrix[i][j] = "" label_df = pd.DataFrame(label_matrix) label_df = label_df.style.applymap( lambda x: ('background-color : #' + rgb_to_hex( tuple(list(colors[part_labels[x] - 1].astype(int)))))) if part_dict and session_state.graph: part_centers = np.zeros((24, 3)) for part in part_dict.keys(): x_min, y_min, x_max, y_max = part_dict[part]['bbox'] part_centers[part_labels[part] - 1] = [1, (x_min + x_max) / 2, (y_min + y_max) / 2] adj_mat = np.matrix(annotation_dict['adj']) G = nx.from_numpy_matrix(adj_mat) parts_present = [ labels[i] for i in np.where(part_centers[:, 0] == 1)[0] ] part_centers = part_centers[np.where(part_centers[:, 0] == 1)] edge_x = [] edge_y = [] for i in range(len(adj_mat)): for j in range(i, len(adj_mat)): if adj_mat[i, j] == 1: _, x0, y0 = part_centers[i] _, x1, y1 = part_centers[j] edge_x.append(x0 - im_x_min) edge_x.append(x1 - im_x_min) edge_x.append(None) edge_y.append(y0 - im_y_min) edge_y.append(y1 - im_y_min) edge_y.append(None) graph.add_trace(go.Scatter(x=edge_y, y=edge_x, line=dict(width=1, color='blue'), hoverinfo='none', mode='lines'), row=1, col=2) node_x = [x - im_x_min for _, x, y in part_centers] node_y = [y - im_y_min for _, x, y in part_centers] graph.add_trace(go.Scatter(x=node_y, y=node_x, mode='markers', hoverinfo='text', text=parts_present, marker=dict(showscale=False, reversescale=True, color=['red'] * len(node_x), size=10, colorbar=dict(thickness=15, ), line_width=2)), row=1, col=2) l = min(6, len(part_labels)) b = int(np.ceil(len(part_labels) / 6)) label_matrix = np.zeros((b, l)).astype('str') for i in range(b): for j in range(l): try: label_matrix[i][j] = labels[i * 6 + j] except: label_matrix[i][j] = "" label_df = pd.DataFrame(label_matrix) label_df = label_df.style.applymap( lambda x: ('background-color : #' + rgb_to_hex( tuple(list(colors[part_labels[x] - 1].astype(int)))))) graph.update_layout(showlegend=False, width=500, height=500, plot_bgcolor='rgb(0,0,0)') graph.update_xaxes(visible=False) graph.update_yaxes( autorange="reversed", visible=False, scaleanchor="x", scaleratio=1, ) st.plotly_chart(graph) if session_state.bbox_disp: st.dataframe(label_df) st.subheader("Part Legend") session_state.sync()
def stochasticModel(Pop, recDays, avgInfections, initialInfections, worlds): global fig_stoch fig_stoch = subplots.make_subplots() fig_stoch['layout'].update( height=500, title='Stochastic SIR Model evolution of Virus', title_x=0.5, xaxis_title="Days", yaxis_title="Population") fig_stoch['layout']['margin'] = { 'l': 20, 'b': 30, 'r': 10, 't': 50 } for i in range(0, worlds): # int; total population N = Pop # maximum elapsed time T = 1000 # start time t = 0.0 # recovery days recDays = 14 # rate of infection after contact _alpha = avgInfections / recDays # rate of cure _beta = 1 / recDays # initial infected population n_I = initialInfections # susceptible population, set recovered to zero n_S = N - n_I n_R = 0 # Initialize results list SIR_data = [] SIR_data.append((t, n_S, n_I, n_R)) # Main loop while t < T: if n_I == 0: break w1 = _alpha * n_S / N * n_I w2 = _beta * n_I W = w1 + w2 dt = np.random.exponential(1 / W) t = t + dt if np.random.uniform(0, 1) < w1 / W: n_S = n_S - 1 n_I = n_I + 1 else: n_I = n_I - 1 n_R = n_R + 1 SIR_data.append((t, n_S, n_I, n_R)) S = [y[1] for y in SIR_data] I = [y[2] for y in SIR_data] R = [y[3] for y in SIR_data] t = [y[0] for y in SIR_data] df_stoch = pd.DataFrame(data=list(zip(t, I)), columns=['Time', 'Infected']) df_stoch['Susceptible'] = S df_stoch['Recovered'] = R print(df_stoch) if i == 0: fig_stoch.add_scatter(x=df_stoch['Time'], y=df_stoch['Susceptible'], mode="lines", row=1, col=1, name='Susceptible', marker=dict(size=20, color='blue')) fig_stoch.add_scatter(x=df_stoch['Time'], y=df_stoch['Infected'], mode="lines", row=1, col=1, name='Infected', marker=dict(size=20, color='red'), fillcolor='red') fig_stoch.add_scatter(x=df_stoch['Time'], y=df_stoch['Recovered'], mode="lines", row=1, col=1, name='Recovered', marker=dict(size=20, color='green'), fillcolor='green') else: fig_stoch.add_scatter(x=df_stoch['Time'], y=df_stoch['Susceptible'], mode="lines", row=1, col=1, name='Susceptible', marker=dict(size=20, color='blue'), showlegend=False) fig_stoch.add_scatter(x=df_stoch['Time'], y=df_stoch['Infected'], mode="lines", row=1, col=1, name='Infected', marker=dict(size=20, color='red'), showlegend=False, fillcolor='red') fig_stoch.add_scatter(x=df_stoch['Time'], y=df_stoch['Recovered'], mode="lines", row=1, col=1, name='Recovered', showlegend=False, marker=dict(size=20, color='green'), fillcolor='green')
def monthly_ic_heatmap_plot(mean_monthly_ic): mean_monthly_ic = mean_monthly_ic.copy() # 给原来df的添加一个index,便于后面搜索 mean_monthly_ic = mean_monthly_ic \ .set_index([mean_monthly_ic['year'], mean_monthly_ic['month']]) # print(mean_monthly_ic) # 设定集合 x = [str(i) for i in range(1, 13)] # month y = list(set([int(i) for i in mean_monthly_ic['year']])) # year 去重 y.sort(reverse=True) mean_monthly_ic = mean_monthly_ic.drop(columns=['month', 'year']) # print(mean_monthly_ic) # heatmap的z z = list() # heatmap titles titles = list() for i in mean_monthly_ic.iteritems(): titles.append(i[0]) z_year = list() for year in y: z_month = list() for month in x: try: ic = i[1].loc[str(year), str(month)] except: z_month.append(np.nan) else: z_month.append(ic) # z_year存的是一个period的heatmap z_year.append(z_month) # z存的是所有period的heatmap z.append(z_year) # 开始画图 fig = make_subplots(rows=int(len(mean_monthly_ic.columns) / 3) + 1, cols=3, subplot_titles=titles) count = 0 for z1 in z: # z1 是其中一个subplot的z fig1 = ff.create_annotated_heatmap(x=x, y=y, z=np.array(z1), hoverinfo='z') # fig1.show() fig.add_trace(fig1.data[0], int(count / 3) + 1, count % 3 + 1) count += 1 layout = dict() for i in range(1, count + 1): layout['xaxis' + str(i)] = {'type': 'category'} layout['yaxis' + str(i)] = {'type': 'category'} fig.update_layout( layout ) return fig
def plot_trends(group, country="US", state=None, place=None, predictive_method="ARIMA"): """ Plot trends of queries @param group: Query group @param country: Country code @param state: State code @param place: Place data in dictionary @param predictive_method: ARIMA or GAM """ print( f"* Plotting Google Trends of `{group}` for {country} - {state or 'All'}" ) group_queries = get_group_queries(group, only_root=True) n_queries = len(group_queries) n_cols = 3 n_rows = int(n_queries / n_cols) + (1 if n_queries % n_cols else 0) # Annotations annotations = [] # Initialize figure with subplots subplot_titles = [ "%s..." % t[:22] if len(t) >= 22 else t for t in group_queries ] fig = make_subplots(rows=n_rows, cols=n_cols, subplot_titles=subplot_titles, shared_yaxes=True, print_grid=True) # Marked Dates covid_start_date = COVID_START_DATE reopen_date = REOPEN_DATE reopen_date_minus_1 = REOPEN_DATE_MINUS_1 data_start_date = DATA_START_DATE data_end_date = DATA_END_DATE # Figure variable baseline = 0 value_range = [0, 100] # Model params model_params = [] for idx, query in enumerate(group_queries): row = int(idx / n_cols) + 1 col = idx % n_cols + 1 showlegend = idx == 0 query_file_path = get_data_filename(group, query, country=country, state=state, full=True) df = pd.read_csv(query_file_path, parse_dates=True) count = df["date"].count() # ARIMA Model if query in df.columns: print("Query: ", query) # get_arima_params(df[query]) df, model = arima_predict(df, from_date=PREDICT_FROM_DATE, value_col=query) params = model.get_params() model_params.append([query, str(params["order"])]) # return False # No data if count == 0: continue # Process stayhome_order_date = place.get( "ClosedFrom") if place else SOCIAL_DISTANCE_ORDER_DATE df = df[(df["date"] >= data_start_date) & (df["date"] <= data_end_date)] df_before = df[(df["date"] <= reopen_date)] df_after = df[(df["date"] >= reopen_date_minus_1)] df_prediction = df[df["is_predicted"] == 1] # Normalize if config.TRENDS_APPLY_NORMALIZATION: max_value = df[query].max() baseline = df_before[query].median() df["value"] = df[query].apply(lambda x: (x - baseline) / max_value) df_before["value"] = df_before[query].apply( lambda x: (x - baseline) / max_value) df_after["value"] = df_after[query].apply( lambda x: (x - baseline) / max_value) baseline = 0 value_range = [-1, 1] else: max_value = df[query].max() baseline = df_before[query].median() df["value"] = df[query] df_before["value"] = df_before[query] df_after["value"] = df_after[query] # Compute difference query_text = query.split( "+")[0].strip() + " + ..." if "+" in query else query actual_mean, actual_meanCI95min, actual_meanCI95max = mean_confidence_interval( df_prediction[query]) predict_mean = df_prediction["prediction"].mean() diff = round(100 * (actual_mean - predict_mean) / predict_mean, 1) diffCI95min = round( 100 * (actual_meanCI95min - predict_mean) / predict_mean, 1) diffCI95max = round( 100 * (actual_meanCI95max - predict_mean) / predict_mean, 1) x_date = list(df['date'])[int(df["date"].count() / 2)] diff_annot = go.layout.Annotation( text= f'<b>{query_text}</b><br><sub><b style="color:{config.COLOR_UPTREND if diff >= 0 else config.COLOR_DOWNTREND}">{diff}%</b>; 95%CI, [{diffCI95min}%, {diffCI95max}%]</sub>', showarrow=False, xanchor="center", yanchor="top", x=x_date, y=0.0, xshift=0, yshift=-5, xref=f"x{'' if idx == 0 else idx + 1}", yref=f"y{'' if idx == 0 else idx + 1}") annotations.append(diff_annot) # Lockdown period max_y = max(df[query].max(), abs(df[query].min())) min_y = -max_y shape_lockdown = go.layout.Shape( **{ "type": "rect", "y0": 100, "y1": -100, "x0": COVID_START_DATE, "x1": REOPEN_DATE, "xref": "x1", "yref": "y1", "layer": "below", "fillcolor": "#eeeeee", "line": dict(width=0), "line_width": 0 }) fig.add_shape(shape_lockdown, row=row, col=col) # Horizontal line shape = go.layout.Shape( **{ "type": "line", "y0": baseline, "y1": baseline, "x0": str(df["date"].values[0]), "x1": str(df["date"].values[-1]), "xref": "x1", "yref": "y1", "layer": "below", "line": { "color": "rgb(200, 200, 200)", "width": 1.5 } }) fig.add_shape(shape, row=row, col=col) # Stay home order if stayhome_order_date: shape_stayhome_order = go.layout.Shape( **{ "type": "line", "y0": -0.25, "y1": 0.25, "x0": stayhome_order_date, "x1": stayhome_order_date, "xref": "x1", "yref": "y1", "line": { "color": "blue", "width": 1.5, "dash": "dot" } }) fig.add_shape(shape_stayhome_order, row=row, col=col) # Plot subplot_before = go.Scatter(x=df_before["date"], y=df_before["value"], mode="lines", name="Before Lockdown", line=dict(width=1, color=config.LINE_COLOR_BEFORE), line_shape="linear", showlegend=False) # linear or spline subplot_after = go.Scatter(x=df_after["date"], y=df_after["value"], mode="lines", name="Actual Queries", line=dict(width=1.5, color=config.LINE_COLOR_AFTER), line_shape="linear", showlegend=showlegend) # linear or spline subplot_prediction = go.Scatter( x=df_prediction["date"], y=df_prediction["prediction"], mode="lines", name="Expected Queries", line=dict(width=2, color=config.LINE_COLOR_BEFORE, dash="dot"), line_shape="linear", showlegend=showlegend) # linear or spline subplot_lockdown_legend = go.Bar(x=[ reopen_date, ], y=[ 0, ], name="Early Lockdown Phase", showlegend=showlegend, marker_color="#eeeeee") fig.add_trace(subplot_before, row=row, col=col) fig.add_trace(subplot_after, row=row, col=col) fig.add_trace(subplot_prediction, row=row, col=col) if idx == 0: fig.add_trace(subplot_lockdown_legend, row=row, col=col) # break # Caption # caption = go.layout.Annotation( # showarrow=False, # text="", # xanchor="center", # x=0.5, # yanchor="top", # y=0.0, # yshift=0, # ) # Layout # location = f"{country}.{state}" if state else country # fig_title = f"""Term: {group}. Location: {location}<br> # <span style="font-size: 14px;line-height:1">Period: {data_start_date} - {data_end_date} # <br>Lockdown Period: {covid_start_date} - {PREDICT_FROM_DATE}</span>""" fig_title = "" fig.update_layout(title={ "text": fig_title, "x": 0.5, "xanchor": "center" }, title_font=dict(size=12), height=50 + n_rows * 175, width=250 * n_cols, coloraxis=dict(colorscale="Bluered_r"), showlegend=True, plot_bgcolor="rgb(255,255,255)", titlefont={"size": 30}, margin={"t": 50}, annotations=annotations, legend=dict(orientation="v", yanchor="bottom", y=0, xanchor="right", x=1, bgcolor="white", bordercolor="#333", borderwidth=1)) fig.update_xaxes(showgrid=False, showticklabels=False, showline=False) fig.update_yaxes(showgrid=False, showticklabels=False, showline=True, range=value_range) # Store model parameters mkdir_if_not_exist(config.TRENDS_OUTPUT_DIR) df_params = pd.DataFrame(model_params, columns=["Query", "Order"]) df_params.to_csv("%s/ARIMA_orders_%s.csv" % (config.TRENDS_OUTPUT_DIR, group), index=False) # Create online URL url = py.iplot(fig, filename=group, file_id=group) print("URL:", url.src) if config.TRENDS_EXPORT_FIGURES: # Save mkdir_if_not_exist(config.TRENDS_FIGURES_DIR) fig.write_image( "%s/%s_%s_%s.jpg" % (config.TRENDS_FIGURES_DIR, country, state or "All", group)) # fig.show() else: # Show fig.show()
def generate_chart(profile, days): fig = make_subplots(specs=[[{"secondary_y": True}]]) dates, weight, bf = [], [], [] point_in_time = date.today() - timedelta(days=days) for measurement in profile.measurements.filter( created_at__gte=point_in_time): # dates.append(measurement.created_at + timedelta(hours=2)) dates.append(timezone.localtime(measurement.created_at)) weight.append(measurement.weight) bf.append(measurement.bf_percent) fig.add_trace(go.Scatter(x=dates, y=weight, mode='lines+markers', name='masa ciała', opacity=0.8, marker_color='Navy'), secondary_y=False) fig.add_trace(go.Scatter(x=dates, y=bf, mode='lines+markers', name='tkanka tłuszczowa', opacity=0.8, marker_color='Plum'), secondary_y=True) fig.update_yaxes(secondary_y=False, ticksuffix='kg', color='MediumSlateBlue') fig.update_yaxes(secondary_y=True, ticksuffix='%', color='Navy') fig.update_layout(font={'family': 'Catamaran'}, plot_bgcolor='#e9ecef', paper_bgcolor='#e9ecef', height=400, annotations=[ dict(xref='paper', yref='paper', x=0.0, y=1.05, xanchor='left', yanchor='bottom', text='Wykres pomiarów z {} dni'.format(days), font=dict(family='Catamaran', size=40, color='rgb(37,37,37)'), showarrow=False) ], legend_orientation="h", shapes=[{ 'type': 'line', 'xref': 'x', 'yref': 'y', 'x0': now(), 'y0': profile.goal, 'x1': now() + timedelta(days=21), 'y1': profile.goal, 'name': 'goal', 'line': { 'color': 'LightSeaGreen' } }]) return plot(fig, output_type='div', include_plotlyjs=False, show_link=False, link_text="")
def setUp(self): _future_flags.add('v4_subplots') fig = make_subplots( rows=3, cols=2, specs=[[{}, {'type': 'scene'}], [{'secondary_y': True}, {'type': 'polar'}], [{'type': 'domain', 'colspan': 2}, None]] ).update(layout={'height': 800}) # data[0], (1, 1) fig.add_scatter( mode='markers', y=[2, 3, 1], name='A', marker={'color': 'green', 'size': 10}, row=1, col=1) # data[1], (1, 1) fig.add_bar(y=[2, 3, 1], row=1, col=1, name='B') # data[2], (2, 1) fig.add_scatter( mode='lines', y=[1, 2, 0], line={'color': 'purple'}, name='C', row=2, col=1, ) # data[3], (2, 1) fig.add_heatmap( z=[[2, 3, 1], [2, 1, 3], [3, 2, 1]], row=2, col=1, name='D', ) # data[4], (1, 2) fig.add_scatter3d( x=[0, 0, 0], y=[0, 0, 0], z=[0, 1, 2], mode='markers', marker={'color': 'green', 'size': 10}, name='E', row=1, col=2 ) # data[5], (1, 2) fig.add_scatter3d( x=[0, 0, -1], y=[-1, 0, 0], z=[0, 1, 2], mode='lines', line={'color': 'purple', 'width': 4}, name='F', row=1, col=2 ) # data[6], (2, 2) fig.add_scatterpolar( mode='markers', r=[0, 3, 2], theta=[0, 20, 87], marker={'color': 'green', 'size': 8}, name='G', row=2, col=2 ) # data[7], (2, 2) fig.add_scatterpolar( mode='lines', r=[0, 3, 2], theta=[20, 87, 111], name='H', row=2, col=2 ) # data[8], (3, 1) fig.add_parcoords( dimensions=[{'values': [1, 2, 3, 2, 1]}, {'values': [3, 2, 1, 3, 2, 1]}], line={'color': 'purple'}, name='I', row=3, col=1 ) # data[9], (2, 1) with secondary_y fig.add_scatter( mode='lines', y=[1, 2, 0], line={'color': 'purple'}, name='C', row=2, col=1, secondary_y=True ) self.fig = fig self.fig_no_grid = go.Figure(self.fig.to_dict())
def save_to_graph(code, trade_starttime, datetime_arr, price_arr, volume_datetime_arr, volume_arr, mavg_datetime_arr, mavg_price_arr, buy_datetime_arr, buy_price_arr, sell_datetime_arr, sell_price_arr, peaks, bottom_peaks, save_path, title): shapes = [] annotations = [] shapes_y_height = sell_price_arr[0] * 0.003 for i, bda in enumerate(buy_datetime_arr): if len(buy_price_arr) > i: annotations.append( go.layout.Annotation(x=bda, y=buy_price_arr[i], text='buy', xref='x', yref='y', showarrow=True, arrowhead=7)) for i, sda in enumerate(sell_datetime_arr): if len(sell_price_arr) > i: annotations.append( go.layout.Annotation(x=sda, y=sell_price_arr[i], text='sell', xref='x', yref='y', showarrow=True, arrowhead=7)) for p in peaks: d = mavg_datetime_arr[p] p = mavg_price_arr[p] shapes.append( dict(type='circle', x0=d - timedelta(seconds=1), x1=d + timedelta(seconds=1), y0=p - shapes_y_height, y1=p + shapes_y_height, xref='x', yref='y', line_color='black')) for p in bottom_peaks: d = mavg_datetime_arr[p] p = mavg_price_arr[p] shapes.append( dict(type='circle', x0=d - timedelta(seconds=1), x1=d + timedelta(seconds=1), y0=p - shapes_y_height, y1=p + shapes_y_height, xref='x', yref='y', line_color='orange')) fig = make_subplots(rows=2, cols=1, shared_xaxes=True) fig.add_trace(go.Scatter(x=datetime_arr, y=price_arr, name='price'), row=1, col=1) fig.add_trace(go.Scatter(x=mavg_datetime_arr, y=mavg_price_arr, name='mavg', line=dict(color='red')), row=1, col=1) fig.add_trace(go.Bar(x=volume_datetime_arr, y=volume_arr, marker_color='indianred'), row=2, col=1) fig.add_shape( dict(type='line', x0=trade_starttime - timedelta(seconds=1), x1=trade_starttime + timedelta(seconds=1), y0=np.array(price_arr).min(), y1=np.array(price_arr).max(), line=dict(color="RoyalBlue", width=2))) fig.update_layout(title=code, yaxis_tickformat='d', shapes=shapes, annotations=annotations, title_text=title) filename = morning_client.get_save_filename( save_path, code + '_' + datetime_arr[0].strftime('%Y%m%d%H%M'), 'html') print('Saved to', filename) fig.write_html(filename, auto_open=False)