Ejemplo: Simulación Shocks oferta y demanda#

Elaborado por: Alejandro Acosta

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets

plt.rcParams['figure.dpi'] = 120
plt.rcParams['savefig.dpi'] = 120

sns.set_style("whitegrid")
def plotsd(m1_s=+1, m1_d=-1, shock_s=0, shock_d=0, show_proj=True):
    plt.figure(figsize=(5, 5))
    # PARAMETROS
    ## Inicial
    p1_s = 0
    p1_d = 100

    # m1_s = +1
    # m1_d = -1

    ## Final
    m2_s = m1_s
    m2_d = m1_d
    # shock_s = 20
    # shock_d = 30

    p2_s = 0 - shock_s
    p2_d = 100 + shock_d
    pp = {'lw': 2.5, 'alpha': .6}

    # show_proj = False

    # INICIAL
    ## Equilibrio
    xe1 = (p1_d - p1_s) / (m1_s-m1_d)
    ye1 = p1_s + m1_s*xe1
    plt.scatter(xe1, ye1, c='green', s=100)
    plt.text(xe1, ye1, '$E$\n', ha='center', c='darkgreen')

    ## Curvas
    plt.axline(xy1=(0, p1_s), slope=m1_s, **pp, c='navy')
    plt.axline(xy1=(0, p1_d), slope=m1_d, **pp, c='darkred')

    ## Proyecciones
    if show_proj:
        plt.axvline(xe1, ymax=ye1/100, **pp, ls=':', c='green')
        plt.axhline(ye1, xmax=xe1/100, **pp, ls=':', c='green')
        plt.scatter(xe1, 0, c='green', s=75)
        plt.scatter(0, ye1, c='green', s=75)
        plt.text(xe1, 0, '$q_1$', ha='center', c='darkgreen', va='top')
        plt.text(0, ye1, '$p_1$ ', ha='right', c='darkgreen', va='center_baseline')

    # FINAL
    ## Equilibrio
    xe2 = (p2_d - p2_s) / (m2_s-m2_d)
    ye2 = p2_s + m2_s*xe2
    plt.scatter(xe2, ye2, c='purple', s=100)
    plt.text(xe2, ye2, '$E$\n', ha='center', c='purple')

    ## Curvas
    plt.axline(xy1=(0, p2_s), slope=m1_s, **pp, c='navy', ls='--')
    plt.axline(xy1=(0, p2_d), slope=m1_d, **pp, c='darkred', ls='--')

    ## Proyecciones
    if show_proj:
        plt.axvline(xe2, ymax=ye2/100, **pp, ls=':', c='purple')
        plt.axhline(ye2, xmax=xe2/100, **pp, ls=':', c='purple')
        plt.scatter(xe2, 0, c='purple', s=75)
        plt.scatter(0, ye2, c='purple', s=75)
        plt.text(xe2, 0, '$q_2$', ha='center', c='purple', va='top')
        plt.text(0, ye2, '$p_2$ ', ha='right', c='purple', va='center_baseline')

    plt.xlim(0, 100)
    plt.ylim(0, 100)
    plt.xticks(alpha=0.5)
    plt.yticks(alpha=0.5)
    plt.xlabel('Q')
    plt.ylabel('P')

    plt.show()
interact(plotsd, shock_s=(-50,+50), shock_d=(-50,+50), m1_s=fixed(1), m1_d=fixed(-1))
# plt.show()
<function __main__.plotsd(m1_s=1, m1_d=-1, shock_s=0, shock_d=0, show_proj=True)>