user8778945
user8778945

Reputation: 23

Read time series csv file to plot with matplotlib

I'm trying to plot a time series from the csv file. eg. datalog.csv contains:

19:06:17.188,12.2

19:06:22.360,3.72

19:06:27.348,72

19:06:32.482,72

19:06:37.515,74

19:06:47.660,72

tried some thing like below:

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.dates as mdates


time, impressions = np.loadtxt("datalog_new.csv", unpack=True,

        converters={ 0: mdates.strptime2num('%H:%M:%S.%f')})


plt.plot_date(x=time, y=impressions)

plt.show()

but could not parse the time, mdates.strptime2num('%H:%M:%S.%f')

Any suggestions are greatly appreciated.

Upvotes: 2

Views: 2408

Answers (1)

Serenity
Serenity

Reputation: 36725

You have to use bytespdate2num function to read csv file (because you read the file in binary mode):

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.dates import bytespdate2num

time, impressions = np.loadtxt("datalog_new.csv",
 unpack=True, delimiter=',', converters={0: bytespdate2num('%H:%M:%S.%f')})

plt.plot_date(x=time, y=impressions)
plt.show()

enter image description here

Upvotes: 3

Related Questions