Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: How do I auto-fill the area above a line graph with solid color?

I made a chart with 2 line graphs. The red line is the high temperature of the day and the blue line is the low temperature of the day. Temps is the Dataframe, High is the high temp, Low is the low temp.

Temps = Temps.set_index('Date')
Temps['High'].plot()
Temps['Low'].plot()

Here's the resulting chart:

enter image description here

How do I automatically fill-in the area above the red line with solid red color? And then automatically fill-in the area below the blue line with solid blue color? Is it possible with MatPlotLib to make it something like the chart below (excuse the crude Paint job)?

enter image description here

like image 725
phan Avatar asked Jan 24 '23 13:01

phan


1 Answers

Here is how I will approach it:

import numpy as np
import matplotlib.pyplot as plt

n = 20
low = 10 * np.random.rand(n, 1)
high = low + 5

f, ax = plt.subplots(1, 1)
ax.plot(range(n), low, color='g')
ax.plot(range(n), high, color='r')
ax.fill_between(range(n), low.squeeze(), ax.get_ylim()[0], color='g')
ax.fill_between(range(n), ax.get_ylim()[1], high.squeeze(),  color='r')

enter image description here

like image 195
quest Avatar answered Jan 27 '23 01:01

quest