Plot the multiple values returned by a function

Viewed 78

My function returns 2 different values which I want to utilise in 2 different graphs using Matplotlib. How can I achieve it?

def option_value_european_put(T, m, r, sigma, mu, E):
    cost_value_at_initial_t_put = []
    portfolio_payoff_put = []
    for e in E:
        delta_t = T / m
        u = (1 + (sigma * math.sqrt(delta_t)) * (math.sqrt(1 + ((mu ** 2) * delta_t) / math.pow(sigma, 2))))
        v = 2 - u
        option_stock_price_matrix_put = np.zeros((m + 1, m + 1))
        sum = 0
        k = m
        start = m
        for i in range(m + 1):
            option_stock_price_matrix_put[i][start] = max(
                (e - stock_price_binomial_model(
                    mu, sigma, T, m,
                    S
                )[i][start], 0)
            )

        for j in range(m - 1, -1, -1):
            for i in range(0, j + 1):
                v_plus = option_stock_price_matrix_put[i][j + 1]
                v_minus = option_stock_price_matrix_put[i + 1][j + 1]
                v_t = ((((v_plus - v_minus) / (u - v)) * (1 + r * delta_t)) + (u * v_minus - v * v_plus) / (u - v)) / (
                        1 + r * delta_t)
                option_stock_price_matrix_put[i][j] = v_t
        cost_value_at_initial_t_put.append(option_stock_price_matrix_put[0][0])

        for i in range(0, m+1):
            sum = sum + option_stock_price_matrix_put[k][i]

        portfolio_return_average = math.average(sum)
        portfolio_payoff_put.append(portfolio_return_average-option_stock_price_matrix_put[0][0] )


    return cost_value_at_initial_t_put, portfolio_payoff_put

I want to use cost_value_at_initial_t_put in 1 Matplotlib plot and the other value in another plot. How can I use it?

1 Answers

Supposing that cost_value_at_initial_t_put and portfolio_payoff_cut are both lists you can create subplots:

import matplotlib.pyplot as plt

fig, (ax_cost, ax_payoff) = plt.subplots(nrows=2)

ax_cost.plot(cost_value_at_initial_t_put)
ax_payoff.plot(portfolio_payoff_cut)
Related