Desafio 1
AZUL1, AZUL2, AZUL3, AZUL4, AZUL5, AZUL6 = '#174A7E', '#4A81BF', "#6495ED", '#2596BE', '#94AFC5', '#CDDBF3'
CINZA1, CINZA2, CINZA3, CINZA4, CINZA5, BRANCO = '#231F20', '#414040', '#555655', '#A6A6A5', '#BFBEBE', '#FFFFFF'
VERMELHO1, VERMELHO2, LARANJA1 = '#C3514E', '#E6BAB7', '#F79747'
VERDE1, VERDE2, VERDE3 = '#0C8040', '#9ABB59', '#9ECCB3'
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/alura-cursos/dataviz-graficos-composicao-relacionamento/refs/heads/main/dados/pib_br_2002_2020_estados.csv')
df.head()
df_pib_regiao = df.copy()
anos = [x for x in range(2017, 2021)]
df_pib_regiao = df_pib_regiao.query("ano == @anos")[[ 'ano', 'regiao', 'pib']]
df_pib_regiao['pib'] = (df_pib_regiao['pib']/1e12).round(2)
df_pib_regiao = pd.crosstab(index = df_pib_regiao['ano'], columns = df_pib_regiao['regiao'],
values = df_pib_regiao['pib'],aggfunc= 'sum')
df_pib_regiao = df_pib_regiao[["Sudeste", "Sul", "Nordeste", "Centro-Oeste", "Norte"]]
df_pib_regiao = df_pib_regiao.reset_index()
df_pib_regiao
import matplotlib.pyplot as plt
import seaborn as sns
fig, ax = plt.subplots(figsize = (16,6))
sns.set_theme(style = 'white')
cores = [CINZA4, VERMELHO2, VERDE3, AZUL5, LARANJA1]
fig.suptitle('Distribuição do PIB do Brasil por Região(2017-2020).', fontsize = 20, color = CINZA2,
ha = 'right', x = 0.60, y = 1.10)
df_pib_regiao.plot(x = 'ano', kind = 'bar', stacked = True, color = cores, ax = ax)
ax.set_title('Em trilhões de reais', color = CINZA3, fontsize = 18, loc = 'left', x = 0.02, y = 1.1)
ax.set_xlabel('')
ax.set_ylabel('')
ax.set_yticklabels([])
ax.xaxis.set_tick_params(labelsize=14, color = CINZA2, labelrotation = 0)
ax.set_frame_on(False)
ax.legend(bbox_to_anchor = (1,1), title = 'Região', title_fontsize = 12, fontsize = 12 )
for container in ax.containers:
labels = [f'{valor.get_height():.2f} Tri' for valor in container]
ax.bar_label(container, label_type='center', labels = labels, size = 11, color = CINZA3, fontweight = "bold")
plt.show()