2

I am trying to read some data from a file and then take the first row and plot it on a graph against some other data using matplotlib.

My code is

import numpy as np    
import matplotlib.pyplot as plt

with open("file path",'r') as f:
    s=  f.readlines()

    y=(s[0])

x=np.arange(0.00,9.04,0.04)

plt.plot(x,y)

plt.ylabel('Probability Distribution')

plt.xlabel('Photometric Redshift')
plt.title('r2')                 
plt.show()

I get the following error message

Traceback (most recent call last):
  File "dsl_2_python.py", line 36, in <module>
    plt.plot(x,y)
  File "/usr/lib64/python2.7/site-packages/matplotlib/pyplot.py", line 2817, in plot
    ret = ax.plot(*args, **kwargs)
  File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line 3996, in plot
    for line in self._get_lines(*args, **kwargs):
  File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line 330, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line 289, in _plot_args
    linestyle, marker, color = _process_plot_format(tup[-1])
  File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line 126, in _process_plot_format

    'Unrecognized character %c in format string' % c)

ValueError: Unrecognized character 0 in format string

>

I think it is something to do with y being a string not a list, but s seems to be a list, so I don't know why y becomes a string or how to make it a list. Can anyone help?

1
  • do you have special characters in your file f? Commented May 16, 2013 at 10:52

2 Answers 2

2

You have correctly identified the problem, y is a string. readlines returns a list of strings (each line of the file is a string). When you call plot(x, y) matplotlib is trying to parse y as a line format string (which is failing because the formatting is wrong) (doc). This is not a bug, matplotlib is responding correctly to the input you gave it.

What you need to do is convert your line into a list of numbers. With out seeing your data file I can only guess, but I suspect something like

y_flt = [float(n) for n in s[0].split()]
plt.plot(x, y_flt)

will do the trick.

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

3 Comments

On a side note, float accepts and ignores whitespace in strings, so the strip() isn't necessary. (Regardless, any trailing or leading whitespace would be removed when splitting.)
@JoeKington I think we have had this conversation before.
Thanks. That is similar to what I did in the end, though yours is neater. ystringlist = s[0].split() yfloatlist = [float(i) for i in ystringlist] plt.plot(x,yfloatlist)
2

Given the file "sample.txt" as:

2 4 6 8 10 12 20
1 2 3 4  5  6  7

You might want to use numpy.loadtxt to read in the file

import numpy as np
import pylab as plt

S = np.loadtxt("sample.txt")
Y = S[0]
X = np.linspace(0.00,9.04,Y.shape[0])

plt.plot(X,Y)
plt.show()

2 Comments

Thanks I got it to work using the following ystringlist = s[0].split() yfloatlist = [float(i) for i in ystringlist] plt.plot(x,yfloatlist)
@nancyh Glad we could help and welcome to Stack Overflow! It looks like you found tcaswell's answer more helpful than mine (that's ok!). Please accept his answer by clicking on the green checkmark so we know that the solution has been found.

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.