Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何导入txt,将字符串转换为浮点_Python_Numpy_Matplotlib - Fatal编程技术网

Python 如何导入txt,将字符串转换为浮点

Python 如何导入txt,将字符串转换为浮点,python,numpy,matplotlib,Python,Numpy,Matplotlib,我正在尝试将数据导入python以绘制图形。对于x和y轴,我希望x在导入时是一个分数,比如1/60,2/60,3/60,等等。我希望y是8pi/10,9pi/10。我该怎么做?我知道这样的字符串不能转换成浮点数。我应该手动计算这些分数的值并输入它们吗? 这是我的密码。我使用了一个模板,因为我是python的初学者。如何修改它 filename="mydata.txt" # change this if your filename is different import s

我正在尝试将数据导入python以绘制图形。对于x和y轴,我希望x在导入时是一个分数,比如1/60,2/60,3/60,等等。我希望y是8pi/10,9pi/10。我该怎么做?我知道这样的字符串不能转换成浮点数。我应该手动计算这些分数的值并输入它们吗? 这是我的密码。我使用了一个模板,因为我是python的初学者。如何修改它

filename="mydata.txt"
# change this if your filename is different


import scipy.optimize as optimize
import numpy as np
import matplotlib.pyplot as plt
from pylab import loadtxt


data=loadtxt(filename, usecols=(0,1,2,3), skiprows=1, unpack=True)
# load filename, take columns 0 & 1 & 2 & 3, skip 1 row, unpack=transpose x&y

xdata=data[0]
ydata=data[1]
xerror=data[2]
yerror=data[3]
# finished importing data, naming it sensibly

def my_func(t,a,tau,T,phi):
    return a*np.exp(-t/tau)*np.cos(2*np.pi*t/T+phi)
# this is the function we want to fit. the first variable must be the
# x-data (time), the rest are the unknown constants we want to determine

popt, pcov = optimize.curve_fit(my_func, xdata, ydata)
# we have the best fit values in popt[], while pcov[] tells us the uncertainties

a=popt[0]
tau=popt[1]
T=popt[2]
phi=popt[3]
# best fit values are named nicely
u_a=pcov[0,0]**(0.5)
u_tau=pcov[1,1]**(0.5)
u_T=pcov[2,2]**(0.5)
u_phi=pcov[3,3]**(0.5)
# uncertainties of fit are named nicely

def fitfunction(t):
    return a*np.exp(-t/tau)*np.cos(2*np.pi*t/T+phi)
#fitfunction(t) gives you your ideal fitted function, i.e. the line of best fit

start=min(xdata)
stop=max(xdata)
xs=np.arange(start,stop,(stop-start)/1000) # fit line has 1000 points
curve=fitfunction(xs)
# (xs,curve) is the line of best fit for the data in (xdata,ydata)

plt.rcParams["figure.figsize"] = (10,6)
# Change the size of your plot - numbers are inches because USA

plt.errorbar(xdata,ydata,yerr=yerror,xerr=xerror,fmt=".")
# plot the data, fmt makes it data points not a line
plt.plot(xs,curve)
# plot the best fit curve on top of the data points as a line

plt.xlabel("xdata")
plt.ylabel("ydata")
plt.title("Best fit of some data points")
# HERE is where you change how your graph is labelled!

plt.show()
# show the graph


print("A:", a, "+/-", u_a)
print("tau:", tau, "+/-", u_tau)
print("T:", T, "+/-", u_T)
print("phi:", phi, "+/-", u_phi)
# prints the various values with uncertainties

plt.rcParams["figure.figsize"] = (10,3)
# Change the size of your plot - numbers are inches because USA

residual=ydata-fitfunction(xdata)
# find the residuals
zeroliney=[0,0]
zerolinex=[start,stop]
# create the line y=0

plt.errorbar(xdata,residual,yerr=yerror,xerr=xerror,fmt=".")
# plot the residuals with error bars
plt.plot(zerolinex,zeroliney)
# plotnthe y=0 line on top

plt.xlabel("xdata")
plt.ylabel("residuals of ydata")
plt.title("Residuals of the fit")
# HERE is where you change how your graph is labelled!

plt.show()
# show the graph
如果有人能提供一些见解,那将非常有帮助