Issue
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:
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)?
Solution
Yes it is possible with matplotlib using fill_between
.
# library
import numpy as np
import matplotlib.pyplot as plt
# Create data
x=range(1,6)
y=[1,4,6,8,4]
z = [8-i for i in y]
# Area plot below line
plt.fill_between(x, y, color='g')
plt.show()
# Area plot above line
plt.fill_between(x, y, max(y), color='r')
plt.show()
Complete Solution:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({"Datum": ['1/2/2018',
'1/3/2018',
'1/4/2018',
'1/5/2018',
'1/6/2018',
'1/7/2018',
'1/8/2018',
'1/9/2018'],
"Menge": [19.5, 19.,19.5,19.5,21,19.5,20,23]})
# sns.lineplot(x="Datum", y="Menge", data=df)
df['Datum'] = pd.to_datetime(df['Datum'])
df.set_index('Datum', inplace=True)
df['Menge2'] = df.Menge+12
df['Menge'].plot()
df['Menge2'].plot()
# Area plot below line
plt.fill_between(df.index, df.Menge, color='g')
# plt.show()
# Area plot above line
plt.fill_between(df.index, df.Menge2, max(df.Menge2), color='r')
Answered By - Pygirl
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.