1

I'm trying to plot a demand profile for heating energy for a specific building with Python and matplotlib. But instead of being a single line it looks like this:

enter image description here

Did anyone ever had plotting results like this? Or does anyone have an idea whats going on here?

The corresponding code fragment is:

for b in list_of_buildings:

    print(b.label, b.Q_Heiz_a, b.Q_Heiz_TT, len(b.lp.heating_list))

    heating_datalist=[]
    for d in range(timesteps):
        b.lp.heating_list[d] = b.lp.heating_list[d]*b.Q_Heiz_TT     
        heating_datalist.append((d, b.lp.heating_list[d]))

        xs_heat = [x[0] for x in heating_datalist]
        ys_heat = [x[1] for x in heating_datalist]
        pyplot.plot(xs_heat, ys_heat, lw=0.5)              

pyplot.title(TT)

#get legend entries from list_of_buildings
list_of_entries = []
for b in list_of_buildings:
    list_of_entries.append(b.label)
pyplot.legend(list_of_entries)          
pyplot.xlabel("[min]")
pyplot.ylabel("[kWh]")

Additional info:

  • timesteps is a list like [0.00, 0.01, 0.02, ... , 23.59] - the minutes of the day (24*60 values)
  • b.lp.heating_list is a list containing some float values
  • b.Q_Heiz_TT is a constant
MPA
  • 1,878
  • 2
  • 26
  • 51
Tybald
  • 167
  • 1
  • 1
  • 12
  • 1
    Probably an issue with your data / data treatment. Add the data if you want help. – Mathieu May 17 '18 at 09:57
  • You plot `xs_heat`/`ys_heat` for each `d in range(timesteps)`. If `timesteps` is large, then you will have many lines on your plot. With the current information, we can't do much to help you. Please provide a simple example that reproduces the problem, or provide details of the shape/contents of `timesteps`, `b.lp.heating_list`, etc. – MPA May 17 '18 at 11:22
  • I added some info. The data is there. I double checked that. – Tybald May 17 '18 at 11:36
  • @Tybald FYI: if you don't notify somebody with the @ mention, then he/she doesn't get an alert. I just happened to have revisited this page, otherwise I would not have known that you posted an update. – MPA May 17 '18 at 13:47
  • Looks a lot like a zooming problem to me. If you _zoom horizontally_, what do you see? – heltonbiker May 17 '18 at 13:54
  • Or else, it is the way you are plotting. I notice that you call `plot` _once for each timestep_, while I would expect you to call `plot` once _per building`. The later would mean plotting "consumption vs time" for each building. Was that your intention? – heltonbiker May 17 '18 at 13:57

2 Answers2

0

Based on your information, I have created a minimal example that should reproduce your problem (if not, you may have not explained the problem/parameters in sufficient detail). I'd urge you to create such an example yourself next time, as your question is likely to get ignored without it. The example looks like this:

import numpy as np
import matplotlib.pyplot as plt

N = 24*60
Q_Heiz_TT = 0.5
lp_heating_list = np.random.rand(N)
lp_heating_list = lp_heating_list*Q_Heiz_TT

heating_datalist = []

for d in range(N):
    heating_datalist.append((d, lp_heating_list[d]))
    xs_heat = [x[0] for x in heating_datalist]
    ys_heat = [x[1] for x in heating_datalist]
    plt.plot(xs_heat, ys_heat)

plt.show()

What is going in in here? For each d in range(N) (with N = 24*60, i.e. each minute of the day), you plot all values up to and including lp_heating_list[d] versus d. This is because heating_datalist, is appended with the current value of d and corresponding value in lp_heating_list. What you get is 24x60=1440 lines that partially overlap one another. Depending on how your backend is handling things, it may be very slow and start to look messy.

A much better approach would be to simply use

plt.plot(range(timesteps), lp_heating_list)
plt.show()

Which plots only one line, instead of 1440 of them.

MPA
  • 1,878
  • 2
  • 26
  • 51
  • The problem is indeed the plot itself, not the data. 'plt.plot(timesteps, lp_heating_list)' sounds good and I tried it but I get: ValueError: x and y must have same first dimension, but have shapes (1,) and (1440,) ... =/ – Tybald May 17 '18 at 13:46
  • My bad, plot `range(timesteps)`, not just `timesteps` – MPA May 17 '18 at 13:48
  • It is MUCH faster now, but it still looks like one big blob. I'm beginning to believe this is due to the plot type with lines. I'll try it with dots and stuff. – Tybald May 17 '18 at 13:58
0

I suspect there is an indentation problem in your code.

Try this:

heating_datalist=[]
for d in range(timesteps):
    b.lp.heating_list[d] = b.lp.heating_list[d]*b.Q_Heiz_TT     
    heating_datalist.append((d, b.lp.heating_list[d]))

xs_heat = [x[0] for x in heating_datalist]  # <<<<<<<<
ys_heat = [x[1] for x in heating_datalist]  # <<<<<<<<
pyplot.plot(xs_heat, ys_heat, lw=0.5)       # <<<<<<<<

That way you'll plot only one line per building, which is probably what you want.

Besides, you can use zip to generate x values and y values like this:

xs_heat, ys_heat = zip(*heating_datalist)

This works because zip is it's own inverse!

heltonbiker
  • 26,657
  • 28
  • 137
  • 252
  • It's true, `zip(*...)` is elegant and short but also way less intuitional I guess. Nevertheless I came to the conclusion that there are just too many datapoints to plot them the way I did. – Tybald May 17 '18 at 15:08