MatplotLib: stackplot remove vertical line due to multiple y values with same x value

Viewed 203

I am trying to plot two line graphs with the area underneath shaded. I have a 3 lists. One of the dates. Two data sets. Each date has about 96 values. Due to multiple y axis (values from the two data sets) having the same x axis (dates) I believe it is causing vertical lines to appear in the graph to represent these values.

How can I get matplotlib to plot a smooth line and ? How can I remove the vertical lines instead displaying the average?

list_1= [562.2, 550.8, 531.0, 0.0, .... 524.4, 492.6, 509.4, 502.2, 496.2, 490.2, 4152.48, 149.96, 15.0]
list_2= [562.2, 550.8, 531.0, 0.0, .... 524.4, 492.6, 509.4, 502.2, 496.2, 490.2, 4152.48, 149.96, 15.0]
time = ['11-01', '11-01', '11-01', '11-01', ....  '11-30', '11-30' '11-30', '11-30', '11-30', '11-30', '11-30', '11-30', '12-01']

plt.stackplot(time, current_readings, alpha=0.5, color="#ff7f7f", )
plt.stackplot(time, historic_readings, alpha=0.5, color="#7f7fff",)

current output:enter image description here

desired output: enter image description here

1 Answers

If you want to display the average, you can put them into a data.frame, groupby into a data.frame (called avg below). You can call plot as a method, and I guess in your case you want stacked=False so that they overlap:

import matplotlib.pyplot as plt
import seaborn as sns

Days = pd.date_range('2018-01-01', '2018-01-30',freq='D')
time = np.repeat(Days,5)
current_readings = np.random.uniform(0,500,len(time))
historic_readings = np.random.uniform(0,500,len(time))

df = pd.DataFrame({'time':time,
                   'current_readings':current_readings,
                   'historic_readings':historic_readings})

avg = df.groupby('time').agg('mean')
avg.plot.area(alpha=0.1,stacked=False)

enter image description here

Or using the grouped data frame above, you call matplotlib:

plt.figure(figsize=(10,5))
plt.stackplot(avg.index, avg['current_readings'], alpha=0.5, color="#ff7f7f" )
plt.stackplot(avg.index, avg['historic_readings'], alpha=0.5, color="#7f7fff")

enter image description here

Related