2

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

4
  • 2
    What do you expect the output to be? You have values ranging from ~500 to 4150 on date '11-30'... which value should be shown on that date? The average? The minimum? The minimum? The mode? Commented Nov 30, 2020 at 20:42
  • the average should be shown. Commented Nov 30, 2020 at 20:55
  • Could you sketch how your desired output looks like? If you want to share an x- or y-axis, the answer to this question might help Commented Dec 1, 2020 at 6:54
  • @max I've added the outputs. unfortunately that question doesn't seem to answer. Commented Dec 1, 2020 at 13:45

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

9 Comments

I tried your implementation but I got this error: current_readings = np.random.uniform(0, 500, len(time), alpha=0.5, color="#ff7f7f") File "mtrand.pyx", line 1217, in mtrand.RandomState.uniform TypeError: uniform() got an unexpected keyword argument 'alpha'
maybe we're using different versions?
i am so sorry, there was a bit of wrong code. The np.random.uniform is to simulate data, since I cannot see your data. If you try to put your list into a data.frame like I did, and do the groupby followed by plot, it should work
and is it stacked?
i see... let me try to plot that with your code. I edited the answer to use something like your code, see whether it works for you?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.