我创建了一个充电模拟程序,模拟不同的电动汽车到达不同的站点充电。
模拟完成后,程序会为充电站创建 CSV 文件,包括每小时的统计数据和每天的统计数据,首先,每小时的统计数据 CSV 对我来说很重要。
我想为不同的车站绘制queue_length_per_hour(有多少辆车在排队等候,每小时从 0 到 24)。
但问题是我不想包括所有的站,因为它们太多了,所以我认为只有 3 个站就足够了。
我应该选择哪三个站?我选择了 3 个车站,这是根据白天去车站的汽车最多的车站(我可以在 24 小时看到),
正如您在代码中看到的那样,我使用了 pandas 的过滤方法,因此我可以根据 CSV 文件中 24 小时访问最多的汽车选择前 3 个站点。
现在我有了前三个车站,现在我想绘制整个列 cars_in_queue_per_hour,不仅是第 24 小时,而且从第 0 小时开始一直绘制。
from time import sleep
import pandas as pd
import csv
import matplotlib.pyplot as plt
file_to_read = pd.read_csv('results_per_hour/hotspot_districts_results_from_simulation.csv', sep=";",encoding = "ISO-8859-1")
read_columns_of_file = file_to_read.columns
read_description = file_to_read.describe()
visited_cars_at_hour_24 = file_to_read["hour"] == 24
filtered = file_to_read.where(visited_cars_at_hour_24, inplace = True, axis=0)
top_three = (file_to_read.nlargest(3, 'visited_cars'))
# This pick top 3 station based on how many visited cars they had during the day
#print("Top Three stations based on amount of visisted cars:\n{}".format(top_three))
#print(type(top_three))
top_one_station = (top_three.iloc[0]) # HOW CAN I PLOT QUEUE_LENGTH_PER_HOUR COLUMN FROM THIS STATION TO A GRAPH?
top_two_station = (top_three.iloc[1]) # HOW CAN I ALSO PLOT QUEUE_LENGTH_PER_HOUR COLUMN FROM THIS STATION TO A GRAPH?
top_three_station = (top_three.iloc[2]) # AND ALSO THIS?
#print(top_one_station)
#print(file_to_read.where(file_to_read["name"] == "Vushtrri"))
#for row_index, row in top_three.iterrows():
# print(row)
# print(row_index)
# print(file_to_read.where(file_to_read["name"] == row["name"]))
# print(file_to_read.where(file_to_read["name"] == row["name"]).columns)
xlabel = []
for hour in range(0,25):
xlabel.append(hour)
ylabel = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] # how to append queue length per hour for the top 3 stations here?
plt.plot(xlabel,ylabel)
plt.show()
该代码也可通过此 repl.it 链接连同 CSV 文件获得:https://repl.it/@raxor2k/almost-done
请您参考如下方法:
我真的很喜欢 seaborn 包来制作这种类型的情节,所以我会使用
import seaborn as sns
df_2 = file_to_read[file_to_read['name'].isin(top_three['name'])]
sns.factorplot(x='hour', y='cars_in_queue_per_hour', data=df_2, hue='name')
您已经选择了前三个名称,所以唯一相关的部分是使用 pd.isin选择名称与前三名匹配的数据框行,并让 seaborn 绘制。
为此,请确保通过删除 inplace 来更改一行代码:
filtered = file_to_read.where(visited_cars_at_hour_24, axis=0)
top_three = (filtered.nlargest(3, 'visited_cars'))
这会使您的原始数据框完好无损,可以使用其中的所有数据。如果您就地使用,则无法将其重新分配 - 操作就地执行并返回 None。
我清理了你不需要的代码行,所以你要重现的完整代码是
import seaborn as sns
top_three = file_to_read[file_to_read['hour'] == 24].nlargest(3, 'visited_cars')
df_2 = file_to_read[file_to_read['name'].isin(top_three['name'])]
sns.factorplot(x='hour', y='cars_in_queue_per_hour', data=df_2, hue='name')
