简体   繁体   中英

How do I plot bar graphs for unique entries in a dataframe column in Python?

I have a dataframe here with two unique time window in df_mo4['time_window'] , 2018-09-26 11:30:00 to 2018-09-26 11:32:30 and 2018-09-26 11:32:30 to 2018-09-26 11:35:00 . I would like to plot 2 sets of bar graph (not subplot), where the first bar graph depicts the mean, max and min time of individual ID at one time window, and the next bar graph depicting same data at another time window, how can I do about that? Also, I would like the title to be like Duration (time_window). Thank you!

df.plot(kind='bar')
plt.title('Duration \n {}'.format(df['time_window']))
plt.ylabel('Time (s)')
plt.xlabel('ID')
plt.legend(['Mean Time (s)', 'Minimum Time (s)', 'Maximum Time (s)'], loc='upper right')
plt.xticks(np.arange(len(df['ID'])), df['ID'], fontsize=5)
plt.show()

Sample data:

       ID  time_window                         mean_time   min_time    max_time 
0  8027  2018-09-26 11:30:00 to 2018-09-26 11:32:30  0.101679 0.056412 0.340988
1  8027  2018-09-26 11:32:30 to 2018-09-26 11:35:00  0.090957 0.052196 0.323442
2  8028  2018-09-26 11:30:00 to 2018-09-26 11:32:30  0.199167 0.076872 0.614797       
3  8028  2018-09-26 11:32:30 to 2018-09-26 11:35:00  0.239885 0.062660 0.590710     
4  8029  2018-09-26 11:30:00 to 2018-09-26 11:32:30  0.243241 0.098516 0.5713      
5  8030  2018-09-26 11:30:00 to 2018-09-26 11:32:30  0.083064 0.055656 0.27892       
6  8031  2018-09-26 11:32:30 to 2018-09-26 11:35:00  0.134786 0.058290 0.51279        

This is the code that I can suggest you:

#Read your Dataframe
df = pd.read_csv('Test.csv', index_col=None, header='infer', encoding="utf-8-sig")

#split the time_window column into two columns, so you can calculate Duration
df['start_time'], df['end_time'] = df['time_window'].str.split('to', 1).str

#convert start and ending time columns to datetime and ID to numeric
df[['start_time','end_time']] = df[['start_time','end_time']].apply(pd.to_datetime, format='%Y-%m-%d %H:%M:%S')
df["ID"] = pd.to_numeric(df["ID"])

#Calculate the duration of a time window and convert into seconds
df['Duration'] = df['start_time'] - df['end_time']
df['Duration']=df['Duration']/np.timedelta64(1,'s')

#plot 
ax = df.plot(x="ID", y=["min_time", "max_time", "mean_time"], kind="bar", rot=25)
ax.set_xlabel("Instances (ID)")
ax.set_ylabel("Duraction(s)")
ax.set_title("Visualization")

rects = ax.patches
labels = df['Duration']

for rect, label in zip(rects, labels):
     height = rect.get_height()
     ax.text(rect.get_x() + rect.get_width(), height+0.3, label,
             ha='center', va='bottom')

This will yield the following dataframe and plot.

在此处输入图片说明

在此处输入图片说明

Is this what you are looking for? You say you don't want subplots, but it also sounds like you would like an individual graph for each ID?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM