2
Good night, I recently started the program in python because of an elective I have at university, but my knowledge is still minimal. My teacher gave a job to do where I should create a code that reads a Precos.txt table like this:
1, 69.00
2, 69.00
3, 69.00
4, 69.00
5, 69.00
6, 69.00
7, 69.00
8, 79.00
9, 56.51
10, 56.51
Where the first column represents a few days of the month and the second column represents prices. As far as I could learn, I know I can do this reading with these commands:
precos = sp.loadtxt("Precos.txt", delimiter=",")
precos = np.genfromtxt('Precos.txt', delimiter=',')
x, y = np.loadtxt("Precos.txt", delimiter=",", unpack=True)
After reading the table, the program should plot a chart, which I also managed to do without any kind of problem:
plt.plot(x,y)
My problem starts now. My teacher wants q I make an interpolation, and suggested to use sp.interpolate.interp1d(x,y)
to generate another chart. But the script does not want to run.
Could someone help me? Is there a more efficient way to do this? Or something more "professional", since the ideal would be to read a CSV file instead of a TXT.
Grateful from now on
----EDIT--- Program almost ready, but I caught on error:
Valueerror: A value in x_new is above the Interpolation range.
import scipy as sp
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
## Importando os dados numa matriz Nx2
M = sp.loadtxt('Precos.txt', delimiter=',')
## Construindo os vetores X e Y
x=np.zeros(len(M))
y=np.zeros(len(M))
for i in range(len(M)):
x[i] = M[i][0]
y[i] = M[i][1]
##Plotando grafico
plt.plot(x,y)
plt.title("Fone de Ouvido JBL com Microfone T100A - Fevereiro 2017")
plt.xlabel("Dia")
plt.ylabel("Preco em R$")
##Interpolação
F = sp.interpolate.interp1d(x,y)
xn = sp.arange(0,9,0.1)
yn = F(xn)
plt.plot(x, y, 'o', xn, yn, '-')
plt.show()
In the case of this part of Interpolation, it was my teacher who told me to use it like this. But it’s not doing much good.
Have you opened a common CSV text editor file? There is nothing more "professional" in a CSV file than in a TXT - your TXT, by the way, is structured as a CSV.
– jsbueno
Yes, yes. I expressed myself badly.
– GDPS