[英]Weird plot with matplotlib
I'm trying to plot a demand profile for heating energy for a specific building with Python and matplotlib . 我正在尝试使用Python和matplotlib绘制特定建筑物的供热需求曲线。 But instead of being a single line it looks like this: 但是,与其说是一行,不如说是:
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) timesteps
是一个列表,例如[0.00、0.01、0.02,...,23.59]-一天中的分钟(24 * 60值) b.lp.heating_list
is a list containing some float values b.lp.heating_list
是一个包含一些浮点值的列表 b.Q_Heiz_TT
is a constant b.Q_Heiz_TT
是一个常数 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
, ie each minute of the day), you plot all values up to and including lp_heating_list[d]
versus d
. 对于d in range(N)
每个d in range(N)
( N = 24*60
,即一天中的每一分钟),您将绘制直至 lp_heating_list[d]
和d
所有值。 This is because heating_datalist
, is appended with the current value of d
and corresponding value in lp_heating_list
. 这是因为heating_datalist
被附加了d
的当前值和lp_heating_list
对应值。 What you get is 24x60=1440 lines that partially overlap one another. 您得到的是24x60 = 1440条线,它们彼此部分重叠。 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. 仅绘制一条线,而不是1440条。
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: 此外,您可以使用zip
生成x值和y值,如下所示:
xs_heat, ys_heat = zip(*heating_datalist)
This works because zip
is it's own inverse! 之所以有效, 是因为zip
是它自己的逆函数!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.