2

I have created a charging simulation program that simulates different electric cars arriving to different stations to charge.

When simulation is finished, the program creates CSV files for charging stations, both about stats per hour and stats per day, first for now, the stats per hour CSV is important for me.

I want to plot queue_length_per_hour (how many cars are waiting in queue, every hour from 0 to 24), for the different stations.

But the thing is that I do NOT want to include all the station because there are too many of them, therefore I think only 3 stations is more than enough.

Which 3 stations should I pick? I choose to pick 3 stations based on which of them had most visited cars to the station during the day (which I can see at hour 24),

As you can see in the code, I have used the filter method from pandas so I can pick top 3 stations based on who had most visited cars at hour 24 from the CSV file.

And now I have the top three stations, and now I want to plot the entire column cars_in_queue_per_hour, not only for hour 24, but all the way down from Hour 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()

The code is also available at this repl.it link together with the CSV files: https://repl.it/@raxor2k/almost-done

giroud13
  • 31
  • 5

1 Answers1

1

I really like the seaborn-package to make this type of plot, so I would use

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')

You already selected the top three names, so the only relevant part is to use pd.isin to select the lines of the dataframe where the name matches the one in the top three and let seaborn make the plot.

enter image description here

For this to work, make sure you change one line of code by removing the inplace:

filtered = file_to_read.where(visited_cars_at_hour_24, axis=0)
top_three = (filtered.nlargest(3, 'visited_cars'))

This leaves your original dataframe intact to use all the data from. If you use inplace, you cannot assign it back - the operation is acted inplace and returns None.

I cleaned the lines of code you don't need for the plot, so your complete code to reproduce would be

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')
Jondiedoop
  • 3,303
  • 9
  • 24
  • Thanks, I will do that! Just an "extra question": Let us say I now want to also plot "percentage of chargers used per hour", is it possible to add this into the same graph, or is the most logical way to plot another figure only for chargers used per hour ? – giroud13 Dec 30 '18 at 22:42
  • It's possible, e.g. see https://stackoverflow.com/questions/33925494/seaborn-produces-separate-figures-in-subplots. Whether it's logical and visually clear, I would sayis up to you to decide. Good luck! :) – Jondiedoop Dec 30 '18 at 22:46