Last active
July 28, 2024 03:35
-
-
Save joaomj/41afa48badd588a0508bfb35a3a23386 to your computer and use it in GitHub Desktop.
Charts like the ones from @cremieuxrecueil (twitter)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Este é o gráfico que serviu de modelo:
![GThrMVFWUAAbMsq](https://private-user-images.githubusercontent.com/87662918/352787976-57755d11-ce0a-454f-924b-88254ceb5061.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjIxNzY2OTEsIm5iZiI6MTcyMjE3NjM5MSwicGF0aCI6Ii84NzY2MjkxOC8zNTI3ODc5NzYtNTc3NTVkMTEtY2UwYS00NTRmLTkyNGItODgyNTRjZWI1MDYxLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjglMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzI4VDE0MTk1MVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWZkMzlkM2U2MzcwYTY4NGI2YjUxNzdiODI5OTg0ZDc1NzE2OWIxNmI3NDFhMzc2YjZjNzViNWM5YzYyY2ZlZDImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.lj6GRRlffNS0E3WjjSv_jj6nJ09d_xz8iANOl-Q_ltQ)