0

I am trying to plot a chart showing when job(s) finished each day for different business date.

My pandas dataframe looks like ...

     business_dt  job_finish_time     job_name
0    19/03/2020   20/03/2020 09:17:08 job_1
1    20/03/2020   21/03/2020 08:10:11 job_1
2    21/03/2020   22/03/2020 14:12:02 job_1
3    19/03/2020   20/03/2020 20:10:04 job_2
4    20/03/2020   21/03/2020 20:06:02 job_2
5    21/03/2020   22/03/2020 21:44:01 job_2

So the x-axis will show the business dt(dates) and the y-axis to show when the job finished, hh24:mi:ss.

As I am pretty new to matplotlib, and I am having difficulty in ploting the Y-Axis (hh24:Mi:ss). Can one of you guide me how to plot this using matplotlib?

Any help would be appreciated.

Thanks

  • `I am having difficulty ...` - can you show what you are trying and explain how it is deficient? – wwii Mar 29 '20 at 19:58

2 Answers2

0

Convert your data to datetime.datetime using datetime.strptime. Refer to the documentation on it here.

from datetime import datetime

datetime_y=[datetime.strptime(i, "%d/%m/%Y %H:%M:%S") for i in df['job_finish_time']]
datetime_x=[datetime.strptime(i, "%d/%m/%Y") for i in df['business_dt']]

plt.plot(datetime_x,datetime_y)
plt.show()
Sameeresque
  • 2,464
  • 1
  • 9
  • 22
0

Firstly, as @Sameeresque said, you need to convert your data to datetime if it isn't already:

df.business_dt = pd.to_datetime(df.business_dt)
df.job_finish_time = pd.to_datetime(df.job_finish_time)

Secondly, I found it necessary to use

pd.plotting.register_matplotlib_converters()

in order to plot datetime. Finally you can use Pandas datetime series handling to extract the time, and then plot normally.

To get the correct date/time formatting, I had to play around a bit with DateFormatter (based on this answer). Note that your data is a bit odd (two values for each day) so it looks weird:

from matplotlib.dates import DateFormatter

df.business_dt = pd.to_datetime(df.business_dt)
df.job_finish_time = pd.to_datetime(df.job_finish_time)

pd.plotting.register_matplotlib_converters()

f, ax = plt.subplots(figsize=(10, 10))
ax.xaxis.set_major_formatter(DateFormatter('%d/%m/%Y'))
ax.yaxis.set_minor_formatter(DateFormatter('%H:%M:%S'))
ax.set_xlabel('Business dt')
ax.set_ylabel('Job Finish Time')
ax.plot(df.business_dt, df.job_finish_time.dt.time)

Output: enter image description here

The major ticks on the y-axis aren't quite right, but it's almost there.

Josh Friedlander
  • 10,870
  • 5
  • 35
  • 75
  • Hi Josh, Thanks for the quick response. I tried your code and I did get the result you just displayed above. But the x-axis does not display date in for dd/mm/yyyy and nor does the y-axis display HH24:MI:SS. Am I missing something? – Tomic Manka Mar 29 '20 at 20:59
  • In the above example the time component are ordered so the Y-axis remains ordered. What if my two y-axis timestamp are in a different order, how do order them? yaxis1 = ['06:39','15:59', '06:37','06:38'] yaxis2 = ['08:40','18:19', '08:39', '19:34' ] The Y-Axis in the line graph using one single subplots is displayed in the following order. 06:39, 15:59 06:37 06:38 08:40 18:19 08:39 19:34 How do I convert this string as timestamp (hh24:mi) and dsplay it ordered? – Tomic Manka Apr 02 '20 at 11:42
  • This is a separate question, please open a new one rather than asking follow-up questions in comments – Josh Friedlander Apr 02 '20 at 13:10
  • done.https://stackoverflow.com/questions/60999169/using-matplotlib-how-to-display-y-axis-values-ordered-in-hh24mi-format – Tomic Manka Apr 02 '20 at 19:17