2
$\begingroup$

Here I have one csv file with different values and date_time. I wrote the code and run it. Then it gave me an error time data '8/6/2018 6:45' does not match format ' %d/%m/%Y %H:%M:%S' I want to plot this different three values in one graph. Can anyone help me to solve this problem? Here is my code:

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] != ''):
    date_time.append(convertTime(row[0]))
  if(row[1] != ''):
    x1.append(int(row[1]))
  if(row[2] != ''):
    x2.append(int(row[2]))
  if(row[3] != ''):
    x3.append(int(row[3]))
row_num = row_num + 1
fig1 = plt.figure(1)
ax = fig1.add_subplot(2,1,1)
ax.plot(date_time,x1)
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.legend()
ax.xaxis_date()
ax.get_xaxis().set_major_formatter(DateFormatter('%d/%m/%Y %H:%M:%S'))
plt.xlabel('t')
plt.ylabel('k')
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]
ax.plot(style='.-')
plt.show()

enter image description here

error :

enter image description here

$\endgroup$
1
$\begingroup$

error time data '8/6/2018 6:45' does not match format ' %d/%m/%Y %H:%M'

Your problem is you have a leading single space. Change the match to '%d/%m/%Y %H:%M'

$\endgroup$
4
  • $\begingroup$ I changed the format then the same error showed. "day is out of range for month" $\endgroup$
    – user59020
    Feb 21 '19 at 15:28
  • $\begingroup$ @kas can you update the question with the exact new error message and problematic line? $\endgroup$
    – iacob
    Feb 21 '19 at 15:42
  • $\begingroup$ I little bit chnaged and the same error that is showed in the image I posted earlier. $\endgroup$
    – user59020
    Feb 21 '19 at 15:56
  • $\begingroup$ @kas looking at the error message, it seems that some line(s) may be in the format d/m/y where it expects m/d/y. But not being able to see the error-producing line I can't confirm. $\endgroup$
    – iacob
    Mar 11 '19 at 11:12

Your Answer

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