I have a dataset with time and columns. I want to plot a graph with time and value. I tried many methods but didn't come proper graph. Because I have a time series. Then I thought I will convert time into UTC then try to plot it. But I didn't have any idea how to convert the whole column into UTC. Can anyone help me to solve this error?

time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(time.mktime(time.strptime("2008-09-17 14:04:00", "%Y-%m-%d %H:%M:%S"))))

This code is for only one time. If I want to convert the whole column then What should I have to do?

def convertTime(s):
tm = ((datetime.datetime.strptime(s, '%d/%m/%Y %H:%M'))-datetime.datetime(1970,1,1)).total_seconds()*1000 

I wrote this code. But it gave me an error. my csv file

My code is:

 condition = ""
date_time  = []
x1 = []
x2 = []
x3 = []
def convertTime(s):
  tm = time.strptime(s, " %d/%m/%Y %H:%M")
  return datetime.datetime(tm.tm_year,tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec)
with open('data43.csv','r') as csv_file:
csv_data = csv.reader(csv_file, delimiter=',')
 row_num = 0
for row in csv_data:
 if(row_num == 0):
  condition = row[0]
elif(row_num > 1): #Data starts here
  if(row[0] != ''):
  if(row[1] != ''):
  if(row[2] != ''):
  if(row[3] != ''):
row_num = row_num + 1
fig1 = plt.figure(1)
ax = fig1.add_subplot(2,1,1)
ax.stem(date_time,x2,'C1--','C1o',linefmt=None, markerfmt=None, basefmt=None)
ax.stem(date_time,x3,'C2--','C2o',linefmt=None, markerfmt=None, basefmt=None)
ax.get_xaxis().set_major_formatter(DateFormatter('%d/%m/%Y %H:%M:%S'))
leg = plt.legend( loc = 'upper right')
plt.draw() # Draw the figure so you can find the positon of the legend. 
bb = leg.get_bbox_to_anchor().inverse_transformed(ax.transAxes)
xOffset = 0.3
bb.x0 += xOffset
bb.x1 += xOffset
leg.set_bbox_to_anchor(bb, transform = ax.transAxes)
plt.rcParams["figure.figsize"] = [20,20]

One of the easiest ways to solve the problem of plotting your time-series graph is by using pandas. You can use this code

data = pd.read_csv('YOUR_FILENAME_HERE.csv')
data['datetime'] = data['date'] + ' ' + data['time']
data['datetime'] = pd.to_datetime(data['datetime'])
data.index = data['date_time']
data.drop(['date', 'time', 'datatime'], inplace = True, axis = 1)
  • $\begingroup$ I tried your code then It gave me this image. I upload the image. It's not proper graph. That's why I want to try another method. Thank you for the fast reply. $\endgroup$
    – user59020
    Feb 22 '19 at 9:49
  • $\begingroup$ @kas can you please share your csv file? $\endgroup$ Feb 22 '19 at 10:23
  • $\begingroup$ sure I shared my csv , but it is image. Sorry for that, because I don't have any idea how to share csv file. $\endgroup$
    – user59020
    Feb 22 '19 at 10:48
  • $\begingroup$ Please share a link and not an image $\endgroup$ Feb 22 '19 at 10:51
  • $\begingroup$ Here is the link of my csv file. docs.google.com/spreadsheets/d/… $\endgroup$
    – user59020
    Feb 22 '19 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy