Ben the Pyro
Ben the Pyro

Reputation: 51

Python 2.7 Unrecognized Character G in format string during runtime

I have Python 2.7 Win 32 and have installed Matplotlib, Numpy, PyParsing, Dateutil. In IDLE I place in the code:

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
import numpy as np

def graphRawFX () :
    date=mdates.strpdate2num('%Y%m%d%H%M%S')
    bid,ask = np.loadtxt,unpack=True,('GPBUSD1d.txt')
    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()

I then proceed to enter:

rawGraphFX()

This results into the error below:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    graphRawFX()
  File "C:/Users/Emanuel/Desktop/graph", line 16, in graphRawFX
    ax1.plot(date,ask)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 4137, in plot
    for line in self._get_lines(*args, **kwargs):
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 317, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 276, in _plot_args
    linestyle, marker, color = _process_plot_format(tup[-1])
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 129, in _process_plot_format
    'Unrecognized character %c in format string' % c)
ValueError: Unrecognized character G in format string
>>> 

This is probably easy to fix but I need help since I'm getting frustrated over this.

Upvotes: 0

Views: 4579

Answers (1)

John1024
John1024

Reputation: 113834

There are at least two issues with these two lines:

date=mdates.strpdate2num('%Y%m%d%H%M%S')
bid,ask = np.loadtxt,unpack=True,('GPBUSD1d.txt')

The first of these lines sets date to a class instance that would convert strings to matplotlib style dates. However, you never supply the dates. You need to come up with date strings from somewhere and apply this function to them.

The second line of these lines makes two assignments. It first assigns np.loadtxt to True and unpack to 'GPBUSD1d.txt'. It consequently assigns bid to True and ask to 'GPBUSD1d.txt'. It is the latter that causes the Unrecognized character error when matplotlib tries to interpret the G in 'GPBUSD1d.txt' as some kind of format instruction. You probably intended something like:

bid, ask = np.loadtxt('GPBUSD1d.txt', unpack=True)

This would call the function np.loadtxt which would load the file GPBUSD1d.txt' and transpose ("unpack") it.

Upvotes: 2

Related Questions