Skip to content

Instantly share code, notes, and snippets.

@Ze1598
Created January 15, 2020 20:02
Show Gist options
  • Save Ze1598/06106e6e713e9bfb424dd7ac3e4bebab to your computer and use it in GitHub Desktop.
Save Ze1598/06106e6e713e9bfb424dd7ac3e4bebab to your computer and use it in GitHub Desktop.
Matplotlib intro: fill between
from matplotlib import pyplot as plt
import numpy as np
days = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Need to save the data for the Y axis as NumPy arrays because the\
# `fill_between()` method needs to receive arrays instead of lists
company_sales = np.array([2, 6, 9, 12, 8, 15, 20, 32, 27, 30])
competition_sales = np.array([1, 2, 4, 5, 13, 12, 18, 22, 34, 38])
plt.plot(days, company_sales, color="blue", label="Company A")
plt.plot(days, competition_sales, linestyle="--", color="black", label="Competition")
plt.fill_between(
days, company_sales, competition_sales,
where=(company_sales > competition_sales),
interpolate=True,
color="green",
alpha=0.25,
label="Company A sold more"
)
plt.fill_between(
days, competition_sales, company_sales,
where=(company_sales <= competition_sales),
interpolate=True,
color="red",
alpha=0.25,
label="Competition sold more"
)
plt.title("Company A vs Competition sales")
plt.xlabel("Day")
plt.ylabel("Number of sales (thousands)")
plt.legend()
plt.savefig("fill_between_demo.png")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment