[英]Weird plot with matplotlib
我正在嘗試使用Python和matplotlib繪制特定建築物的供熱需求曲線。 但是,與其說是一行,不如說是:
有沒有人曾經繪制過這樣的結果? 還是有人知道這里發生了什么?
相應的代碼片段為:
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]")
附加信息:
timesteps
是一個列表,例如[0.00、0.01、0.02,...,23.59]-一天中的分鍾(24 * 60值) b.lp.heating_list
是一個包含一些浮點值的列表 b.Q_Heiz_TT
是一個常數 根據您的信息,我創建了一個最小的示例,該示例應重現您的問題(如果不是,則可能沒有足夠詳細地解釋問題/參數)。 我敦促您下次自己創建一個這樣的示例,因為沒有它您的問題很可能會被忽略。 該示例如下所示:
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()
這是怎么回事? 對於d in range(N)
每個d in range(N)
( N = 24*60
,即一天中的每一分鍾),您將繪制直至 lp_heating_list[d]
和d
所有值。 這是因為heating_datalist
被附加了d
的當前值和lp_heating_list
對應值。 您得到的是24x60 = 1440條線,它們彼此部分重疊。 根據后端處理方式的不同,它可能會非常緩慢並開始顯得凌亂。
更好的方法是簡單地使用
plt.plot(range(timesteps), lp_heating_list)
plt.show()
僅繪制一條線,而不是1440條。
我懷疑您的代碼中存在縮進問題。
嘗試這個:
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) # <<<<<<<<
這樣,您只需要為每座建築物繪制一條線,這可能就是您想要的。
此外,您可以使用zip
生成x值和y值,如下所示:
xs_heat, ys_heat = zip(*heating_datalist)
之所以有效, 是因為zip
是它自己的逆函數!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.