Mahdi_J
Mahdi_J

Reputation: 428

formatting string into date in matplotlib 3.1

A tutorial for older versions of python and matplotlib contains code like this:

def graphRawFX ():
    date,bid,ask = np.loadtxt('GBPUSD1d.txt',
                              unpack=True,
                              delimiter=',',
                              converters={0:mdates.strpdate2num('%Y%m%d%H%M%S')})

    fig = plt.figure(figsize=(10,7))
    ax1 = plt.subplot2grid((40,40),(0,0), rowspan=40,colspan=40)

    ax1.plot(date,bid)
    ax1.plot(date,ask)

    ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S'))

    plt.grid(True)
    plt.show()

graphRawFX()

I get the following error when I run the code:

The strpdate2num class was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use time.strptime or dateutil.parser.parse or datestr2num instead. converters={0:mdates.strpdate2num('%Y%m%d%H%M%S')})

Here is one row of the data for more information:

20130501000000,1.55358,1.55371

so how do I turn that string into dates using matplotlib 3.1?

Upvotes: 1

Views: 1927

Answers (1)

Amit
Amit

Reputation: 2100

I think what the warning is asking you to do is use time.strptime function to convert from string to time. You may want to change the first line.

import time
date,bid,ask =np.loadtxt('GBPUSD1d.txt',unpack=True,delimiter=',',converters={0:time.strptime(mdates,'%Y%m%d%H%M%S')}) 

This is as much I can see based on the error/warning and the cose that was given.

After conversation, I realized that mdates is actually a matplotlib module. Thus suggesting a change. Please try it out.

date,bid,ask = np.loadtxt('GBPUSD1d.txt', unpack=True, delimiter=',', converters={0: lambda x: mdates.datestr2num(x.decode('utf8'))})  

Hopefully this will work.

Upvotes: 6

Related Questions