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

  • 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? – Diziet Asahi Nov 30 '20 at 20:42
  • the average should be shown. –  Nov 30 '20 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](https://stackoverflow.com/questions/65056691/plotly-how-to-show-more-than-2-x-axes-titles-ranges-on-the-same-subplot/65070189#65070189) might help – max Dec 01 '20 at 06:54
  • @max I've added the outputs. unfortunately that question doesn't seem to answer. –  Dec 01 '20 at 13:45

1 Answers1

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

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • 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'` –  Dec 01 '20 at 13:24
  • maybe we're using different versions? –  Dec 01 '20 at 13:24
  • 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 – StupidWolf Dec 01 '20 at 13:47
  • and is it stacked? – StupidWolf Dec 01 '20 at 13:47
  • No, its not stacked but when running your code in isolation, it doesn't seem to produce anything. even if I try plt.show() –  Dec 01 '20 at 14:11
  • i am on pandas '1.0.2' and matplotlib 3.3.1 – StupidWolf Dec 01 '20 at 14:28
  • ahh I see, this solution probably would have worked but unfortunately I have other dependencies that rely on the current version and updating the packages may cause breaks. I was trying to avoid manually averaging the values but it seems that would be the easiest approach without significantly changing the code current structure. –  Dec 01 '20 at 15:25
  • Your solution seems to be the appropriate solution granted I didn't have my restrictions. –  Dec 01 '20 at 15:26
  • 2
    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? – StupidWolf Dec 01 '20 at 15:33