from bcb import sgs
from bcb import Expectativas, sgs
from plotly.subplots import make_subplots
import pandas as pd
import plotly
import plotly.graph_objects as go
import plotly.express as px
from datetime import datetime
import plotly.io as plt_io
# create our custom_dark theme from the plotly_dark template
"custom_dark"] = plt_io.templates["plotly_dark"]
plt_io.templates[
# set the paper_bgcolor and the plot_bgcolor to a new color
"custom_dark"]['layout']['paper_bgcolor'] = '#111111'
plt_io.templates["custom_dark"]['layout']['plot_bgcolor'] = '#111111'
plt_io.templates[#plt_io.templates["custom_dark"]['layout']['font']['size'] = 12
= ("#6A38D4",) + plt_io.templates["custom_dark"]['layout']['colorway']
color_pallet "custom_dark"]['layout']['colorway'] = color_pallet
plt_io.templates[
"custom_dark"]['layout']['title']['font']['color'] = '#A6A5A5'
plt_io.templates[
# you may also want to change gridline colors if you are modifying background
'custom_dark']['layout']['yaxis']['gridcolor'] = '#4A4A4A'
plt_io.templates['custom_dark']['layout']['xaxis']['gridcolor'] = '#4A4A4A'
plt_io.templates[
'custom_dark']['layout']['font']['color'] = '#A6A5A5'
plt_io.templates[
= {
custom_template "layout": go.Layout(
={
font#"family": "Open Sans",
"size": 12,
"color": "#707070",
},={
title"font": {
#"family": "Open Sans",
"size": 18,
#"color": "#1f1f1f",
"color": "#6A38D4",
},
},="#555050",
plot_bgcolor="#555050",
paper_bgcolor#plot_bgcolor="#FFFFFF",
#paper_bgcolor="#FFFFFF",
#colorway=["#6A38D4"] + px.colors.qualitative.G10
=["#9A6EF8"] + px.colors.qualitative.G10
colorway
)
}
def format_title(title, subtitle=None, subtitle_font_size=14):
= f'<b>{title}</b>'
title if not subtitle:
return title
= f'<span style="font-size: {subtitle_font_size}px;">{subtitle}</span>'
subtitle return f'{title}<br>{subtitle}'
def series_pct_format(series, prec=2):
return series.apply(lambda x: '{0:.{pre}f}%'.format(x, pre=prec))
= "plotly_dark" template
|— format: revealjs: logo: “../../images/logo_grey.png” theme: [dark, “../../assets/slides.scss”] echo: false slide-number: true footer: “www.fintechscientist.com (por Marcus Silva)”
description: “Variáveis Macro para análise de risco de crédito” author: “Marcus O. Silva” date: “12/10/2022”
code-block-bg: true code-block-border-left: “#31BAE9” highlight-style: ayu-dark
Buscando os indicadores macroeconômicos do Bacen em python
# instale a lib do Bacen
!pip install python-bcb
= {
series # <descrição da série>: <código da série>
'Endividamento familiar': 29037,
'Inflação (IPCA)': 13522,
'Taxa de Juros (CDI)': 4392,
'Taxa de Desemprego': 24369,
}
# Carregando as variáveis
= sgs.get(series, start='2021-01-01') df
Acesse o sistema de séries temporais do BACEN para consultar as definições dessas séries: aqui
= df.reset_index()
df_p = df_p.melt(value_vars=df.columns, id_vars=['Date'],
df_p ="indicator",
var_name="value")
value_name= df_p[df_p.Date <= '2022-10-01'] df_p
Plot dos Indicadores macroeconômicos
def series_pct_format(series, prec=2):
return series.apply(lambda x: '{0:.{pre}f}%'.format(x, pre=prec))
= px.line(df_p, x='Date', y="value", title=format_title("", ''),
fig ='custom_dark', height=500, width=900,
template="indicator", facet_col_wrap=2,
facet_col=0.08,
facet_row_spacing=0.08,
facet_col_spacing
)lambda a: a.update(text=a.text.split("=")[1]))
fig.for_each_annotation(='')
fig.update_xaxes(title='')
fig.update_yaxes(title= fig.update_yaxes(matches=None, showticklabels=True)
fig #fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
#fig.update_traces(textposition='top center')
#fig.update_layout(yaxis_range=[1.6, 2.4])
#fig.for_each_trace(
# lambda trace: trace.update(marker_symbol="square") if trace.y == else (),
#)
#fig.add_vline(df_p.Date.max(), row=1,col=1)
#fig.add_annotation(2, x=df_p.Date.max(), row=1,col=1)
#fig.add_trace(go.Scatter(...), row='all', col='all', exclude_empty_subplots=True)
#fig.add_annotation
#fig.fo
#fig.update_traces(textposition='top center')
# add traces for annotations and text for end of lines
for i, d in enumerate(fig.data):
= (2+1) - int(i / 2 + 1)
row = int(i % 2 + 1)
column #print('i:', i, 'd:', d)
=[d.x[-1]], y = [d.y[-1]],
fig.add_scatter(x= 'markers+text',
mode = str(round(d.y[-1],1)) + ' %',
text = dict(color=d.line.color),
textfont #textfont = dict(color=d.line.color),
='middle left',
textposition= dict(color = d.line.color, size = 12),
marker = d.name,
legendgroup =False, row=row,col=column)
showlegend
#fig.show()
= fig.to_image(format="png", scale=2)
img_bytes from IPython.display import Image
Image(img_bytes)