Skip to content

Instantly share code, notes, and snippets.

@joaomj
Last active July 28, 2024 03:35
Show Gist options
  • Save joaomj/41afa48badd588a0508bfb35a3a23386 to your computer and use it in GitHub Desktop.
Save joaomj/41afa48badd588a0508bfb35a3a23386 to your computer and use it in GitHub Desktop.
Charts like the ones from @cremieuxrecueil (twitter)
# Gráfico modelo: https://x.com/cremieuxrecueil/status/1817320318916006119/photo/1
import matplotlib.pyplot as plt
# Dados do gráfico
categories = ['Top 1%', 'Top 5%', 'Top 10%', 'Top 50%', 'Bottom 50%']
values = [7.9, 24.7, 37.8, 80.8, 19.2]
colors = ['#f8de7e', '#f8de7e', '#f8de7e', '#f8de7e', '#6495ed']
# Configurações do gráfico
fig, ax = plt.subplots(figsize=(9, 6), facecolor='#242a33') # aspect ratio: 1.5x --> w=3600, h=2400
ax.set_facecolor('#242a33')
bars = ax.bar(categories, values, color=colors, edgecolor='black')
# Adiciona porcentagens acima das barras
for bar in bars:
yval = bar.get_height()
ax.text(bar.get_x() + bar.get_width() / 2.0, yval + 2, f'{yval}%', ha='center', va='bottom', fontsize=12, color='white')
# Títulos e rótulos
fig.suptitle('The Disproportionality of Lifetime Health Expenditures From Age 70 in the U.S.', x=0.508, fontsize=14, fontweight='regular', color='white')
ax.set_title('\nData From the Asset and Health Dynamics Among the Oldest Old Cohorts of the Health and Retirement Study', loc='left', fontsize=11, color='white')
ax.set_xlabel('')
ax.set_ylabel('Proportion Of All Spending', fontsize=12, color='white')
ax.set_ylim(0, 100)
# Configurações dos eixos e grades
# Adiciona linhas brancas nos eixos X e Y
ax.axhline(y=0, color='white', linewidth=1.3) # Linha do eixo X
ax.axvline(x=-0.4, color='white', linewidth=1.3) # Linha do eixo Y
ax.spines['top'].set_visible(False) # Remove a linha do topo
ax.spines['right'].set_visible(False) # Remove a linha da direita
ax.yaxis.grid(True, color='#292f37') # Linhas de grade cinza
ax.xaxis.grid(True, color='#292f37') # Linhas de grade cinza
# Define os ticks e labels do eixo Y
ax.set_yticks([0, 25, 50, 75, 100])
ax.set_yticklabels(['0%', '25%', '50%', '75%', '100%'], color='white')
# Texto na parte inferior alinhado à direita
fig.text(1, -0.1, 'Note: Medicaid payments imputed from the Medicare Current Beneficiary Survey and disproportionality estimated based on assumed symmetry\nChart by Crémieux Recueil, @cremieuxrecueil\nSource: Jones et al. 2018, Figure 1c', wrap=True, horizontalalignment='right', fontsize=10, color='white', bbox={"facecolor":"#2c2f38", "alpha":0.0, "pad":5})
# Adiciona moldura branca ao redor da imagem
fig.patch.set_linewidth(2) # Largura da linha da moldura
fig.patch.set_edgecolor('white') # Cor da linha da moldura
# Mostra o gráfico
plt.show()
plt.savefig('chart_cremieux.png') # adicione o nome do arquivo desejado
@joaomj
Copy link
Author

joaomj commented Jul 28, 2024

Este é o gráfico que serviu de modelo:
GThrMVFWUAAbMsq

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment