1

I am trying to plot two columns from a text file using python matplotlib but I am getting

ValueError: invalid literal for float(): 148.000000;

This is my python script

import numpy as np import matplotlib.pyplot as plt x,y = np.loadtxt('sharma5.txt') fig = plt.figure() ax = fig.add_subplot(111) ax.plot(x,y) plt.show() 

and here is a part of my text file

36.000000 61.000000 36.000000 61.000000 36.000000 148.000000; 36.000000 60.000000 36.000000 120.000000 36.000000 77.000000 36.000000 160.000000 

Thanks in advance..

1
  • Please remember to format your code using Ctrl-K! Commented Apr 3, 2012 at 8:21

2 Answers 2

1

In case you don't want to fix your data file, you can use the converters option to loadtxt in order to remove any extraneous semicolons. Something like np.loadtxt("sharma5.txt", converters = {1: lambda s: float(s.strip(";"))}) should work.

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

Comments

0

The problem is the semicolon in your text file, which is not recognized as a legal character for converting to a number. Fix the bug in the program that generated that text file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.