# No graph is displaying while plotting value with time interval using python

I have a large dataset with values and date and time. So I want to plot graph of value with time and date. So I wrote a code for time difference. Afterthat I tried to store value into that time difference. Afterthat I tried to plot it. It run with no graph. Can anyone helps me to solve this error? here is my code:

x= df1,iloc[:,2]
time_interval = 14400 #interval in seconds (14400s = 360 minutes)
date_array = []
date_array.append(pd.to_datetime(df1['date'][0]).date())
start_time = []
end_time   = []
temp_date  = pd.to_datetime(df1['date'][0]).date()
start_time.append(pd.to_datetime(df1['time'][0], format='%H:%M:%S').time())
for i in range(len(df1['date'])):
cur_date = pd.to_datetime(df1['date'][i]).date()
if( cur_date > temp_date):
end_time.append(pd.to_datetime(df1['time'][i-1], format='%H:%M:%S').time())
start_time.append(pd.to_datetime(df1['time'][i], format='%H:%M:%S').time())
date_array.append(cur_date)
temp_date = cur_date
end_time.append(pd.to_datetime(df1['time'][len(df1['date'])-1], format='%H:%M:%S').time())
datetime_array = []
for i in range(len(date_array)):
s_time = datetime.datetime.combine(date_array[i],start_time[i])
e_time = datetime.datetime.combine(date_array[i],  end_time[i])

timediff = (e_time - s_time)
#num_periods = int(timediff.total_seconds()/t3ime_interval) +1
num_periods = abs(int(timediff.total_seconds()/time_interval)) + 1
time_list = pd.date_range(start=s_time, end = e_time, periods=num_periods ).to_pydatetime()
datetime_array.extend(time_list)
time_stamps = [datetime.datetime.strftime(t,'%H:%m:%S') for t in datetime_array]
x = np.zeros([num_periods], dtype='timedelta64[s]')
plt.xticks(np.arange(num_periods), time_stamps)


my csv file:

after running the code output came like this:

image:

• What should the graph look like? Do you just want a scatter plot with the values on y-axis and datetime on x-axis? Or a histogram binned by 360 minute periods? – JahKnows Feb 23 '19 at 7:04
• @JahKnows I thought first I will try with scatter plot. Actually I want a line graph x1 with time. I upload the pic (graph) that I want as a example(This is just for understand which shape of graph line I need) – user59020 Feb 23 '19 at 7:55

In python you generally have all the libraries available to you. It is hard to find sometimes but you should rarely need to write out so much code. Try this out.

I created some dummy data using the same date formats as you have:

import pandas as pd
import matplotlib.pyplot as plt

data = {'date': ['08/06/2018', '8/6/2018', '8/6/2018', '9/6/2018'],
'time': ['6:15:00', '12:45:00', '18:15:00', '6:15:00'],
'x2': [1, 4, 8, 6]}


Now we will make a pandas DataFrame with this dummy data

df = pd.DataFrame(data)


Now we can get our x-axis datetimes by first concatenating the dates and times together separated by a space. Then we will get pandas to parse these datetimes.

datetimes = pd.to_datetime(df['date'] + ' ' + df['time'],
format='%d/%m/%Y %H:%M:%S')


You can then plot your data using

plt.plot(datetimes, df['x2'])


Put your csv file in your workspace. Then you can use this following code

import pandas as pd
import matplotlib.pyplot as plt

datetimes = pd.to_datetime(df['date'] + ' ' + df['time'],
format='%d/%m/%Y %H:%M:%S')

plt.plot(datetimes, df['x'])
plt.show()


import matplotlib.dates as mdates

fig, ax = plt.subplots(1)
fig.autofmt_xdate()

plt.plot(datetimes, df['x'])
plt.xticks(rotation=90)

xfmt = mdates.DateFormatter('%d-%m-%y %H:%M')
ax.xaxis.set_major_formatter(xfmt)
plt.show()


• Thank you for your fast reply. So you mentioned the date, time, x2 column data seperately. So can't I used putting the whole column value into that data? – user59020 Feb 23 '19 at 14:17
• @kas, what do you mean? – JahKnows Feb 23 '19 at 14:37
• "data = {'date': ['08/06/2018', '8/6/2018', '8/6/2018', '9/6/2018'], 'time': ['6:15:00', '12:45:00', '18:15:00', '6:15:00'], 'x2': [1, 4, 8, 6]}" you mentioned the date,time, value here. I have long dataset. So df=['x1'],df['date'],df['time'] like this can't I put it into that data. data={df['date'],df['time'],df['x1']} – user59020 Feb 23 '19 at 14:41
• Yes your df can replace my df. And then the following functions would still work the same way. – JahKnows Feb 23 '19 at 16:12
• Thank you very much. I already found that method. Once again thank you for helping me. – user59020 Feb 24 '19 at 8:35