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?

3
  • The row of data you have provided, is it 01st of may, 2013? Commented Aug 2, 2019 at 12:56
  • @Amit yes. and the rest is hours, minutes and seconds Commented Aug 2, 2019 at 13:15
  • I'd try with converters={0:lambda x: mdates.date2num(datetime.strptime(x, '%Y%m%d%H%M%S'))} Commented Aug 2, 2019 at 13:21

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

11 Comments

TypeError: strptime() argument 0 must be str, not <class 'module'> @Amit
Yes, I assumed mdates is a string. There was little before that to tell otherwise. My assumption was wrong. Is there a function on the class to cast to string first?
the code on the reply above didn't work either because datetime has no attribute 'strptime'. and no there is no such function. i've copied the whole project in here.
is there an import such as "import matplotlib.dates as mdates"?
yes. these are all the imported functions if thats what you mean. i apologize if i don't understand 100% i am fairly new at this. import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.ticker as mticker import matplotlib.dates as mdates import numpy as np import datetime
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.