Skip to content

Instantly share code, notes, and snippets.

@iamvee
Created July 28, 2024 10:12
Show Gist options
  • Save iamvee/1342bd070d509530e148d1bd625bcb10 to your computer and use it in GitHub Desktop.
Save iamvee/1342bd070d509530e148d1bd625bcb10 to your computer and use it in GitHub Desktop.
Iran Income Distribution with Monthly and Yearly, Toman and USD Equivalents
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
# source: https://asriran.com/fa/news/922435
income_max_values = [30, 64, 92, 116, 141, 165, 192, 225, 280, 429, 450]
percentiles = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 99, 100]
conversion_rate = 58850
monthly_income_max_values = [value / 12 for value in income_max_values]
usd_monthly_income_max_values = [(value * 1_000_000) / 12 / conversion_rate for value in income_max_values]
usd_yearly_income_max_values = [(value * 1_000_000) / conversion_rate for value in income_max_values]
plt.rcParams.update({'font.size': 10})
fig, ax1 = plt.subplots(figsize=(10, 5))
ax1.bar(percentiles[:-1], monthly_income_max_values, width=np.diff(percentiles), edgecolor='black', alpha=0.7, align='edge')
ax1.set_xlabel('Percentile')
ax1.set_ylabel('Monthly Income (Million Toman)', color='tab:blue')
ax1.tick_params(axis='y', labelcolor='tab:blue')
ax2 = ax1.twinx()
ax2.bar(percentiles[:-1], income_max_values, width=np.diff(percentiles), edgecolor='black', alpha=0.3, align='edge')
ax2.set_ylabel('Yearly Income (Million Toman)', color='tab:green')
ax2.tick_params(axis='y', labelcolor='tab:green')
ax3 = ax1.twinx()
ax3.spines['right'].set_position(('outward', 60))
ax3.bar(percentiles[:-1], usd_monthly_income_max_values, width=np.diff(percentiles), edgecolor='black', alpha=0.3, align='edge')
ax3.set_ylabel('Monthly Income (USD)', color='tab:red')
ax3.tick_params(axis='y', labelcolor='tab:red')
ax4 = ax1.twinx()
ax4.spines['right'].set_position(('outward', 120))
ax4.bar(percentiles[:-1], usd_yearly_income_max_values, width=np.diff(percentiles), edgecolor='black', alpha=0.3, align='edge')
ax4.set_ylabel('Yearly Income (USD)', color='tab:purple')
ax4.tick_params(axis='y', labelcolor='tab:purple')
fig.suptitle('Income Distribution with Monthly and Yearly, Toman and USD Equivalents')
ax1.set_xticks([0, 20, 50, 90, 99])
fig.tight_layout()
bars1 = ax1.patches
bars2 = ax2.patches
bars3 = ax3.patches
bars4 = ax4.patches
for i, (bar1, bar2, bar3, bar4, monthly, yearly, usd_monthly, usd_yearly) in enumerate(zip(bars1, bars2, bars3, bars4, monthly_income_max_values, income_max_values, usd_monthly_income_max_values, usd_yearly_income_max_values)):
if i == 9:
# a white space for better visibility
height1 = bar1.get_height() - 13
height2 = bar2.get_height() - 180
height3 = bar3.get_height() - 300
height4 = bar4.get_height() - 4200
ax1.text(bar1.get_x() + bar1.get_width()/2., height1 + 0.5, f'{monthly:.1f} T', ha='center')
ax2.text(bar2.get_x() + bar2.get_width()/2., height2 + 0.5, f'{yearly} T', ha='center')
ax3.text(bar3.get_x() + bar3.get_width()/2., height3 + 0.5, f'{usd_monthly:.0f}$', ha='center')
ax4.text(bar4.get_x() + bar4.get_width()/2., height4 + 0.5, f'{usd_yearly:.0f}$', ha='center')
elif i == 10:
continue
else:
height1 = bar1.get_height() + 10
height2 = bar2.get_height() + 100
height3 = bar3.get_height()+100
height4 = bar4.get_height()+800
ax1.text(bar1.get_x() + bar1.get_width()/2., height1 + 0.5, f'{monthly:.1f} T', ha='center', color='tab:blue')
ax2.text(bar2.get_x() + bar2.get_width()/2., height2 + 0.5, f'{yearly} T', ha='center', color='tab:green')
ax3.text(bar3.get_x() + bar3.get_width()/2., height3 + 0.5, f'{usd_monthly:.0f}$', ha='center', color='tab:red')
ax4.text(bar4.get_x() + bar4.get_width()/2., height4 + 0.5, f'{usd_yearly:.0f}$', ha='center', color='tab:purple')
ax1.annotate('↑', xy=(98.5, 38))
dpi = 300
plt.savefig('income_distribution.png', dpi=dpi)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment