Tommy Do
Tommy Do

Reputation: 61

Python - Plot lower and upper bound for a small region

I have 4 numpy arrays

true = np.array([1,2,3,4,5])
pred = np.array([3.1,4.1,5.1])
lower = np.array([3,4,5])
upper = np.array([3.2,4.2,5.2])

where the pred,lower and upper are the median predictions, the lower bound and the upper bound of the predictions respectively (only for the last 3 timepoints, not the first 2 timepoints).

I am not sure how to plot all 4 arrays in 1 plots. Appreciate any help.

Upvotes: 0

Views: 4361

Answers (2)

JohanC
JohanC

Reputation: 80409

Maybe something like this? If the plots overlap a lot, you could add an alpha parameter.

import matplotlib.pyplot as plt
import numpy as np

true = np.array([1,2,3,4,5])
pred = np.array([3.1,4.1,5.1])
lower = np.array([2.9,3.9,4.9])
upper = np.array([3.2,4.2,5.2])
xs = range(1,6)

f, ax = plt.subplots()

ax.plot(xs[2:], lower, color='limegreen', marker='o', label='lower', lw=0.5, markersize=2)
ax.plot(xs[2:], pred, color='aqua', marker='o', label='pred', markersize=2)
ax.plot(xs[2:], upper, color='dodgerblue', marker='o', label='upper', lw=0.5, markersize=2)
ax.plot(xs, true, color='crimson', marker='o', label='true')
ax.fill_between(xs[2:], lower, upper, color='gold', alpha=0.3, label='region')
plt.legend()

plt.show()

resulting plot

Upvotes: 2

Dawid Gacek
Dawid Gacek

Reputation: 557

To have everything on one plot, you can use function plot from matplotlib. To add multiple lines on one plot, you need to provide pairs of x and y. This code should explain it:

import numpy as np
import matplotlib.pyplot as plt

true = np.array([1,2,3,4,5])
pred = np.array([3.1,4.1,5.1])
lower = np.array([3,4,5])
upper = np.array([3.2,4.2,5.2])

x = np.arange(1, 6, 1)
x_med = np.arange(3,6,1)

plt.plot(x, true, x_med, pred, x_med, lower, x_med, upper)
plt.show()

Upvotes: 1

Related Questions