How to smooth data - python

I have a data file that I would like to smooth out. I am not sure on how to do it. I have looked at a few things and nothing really helps. here is my code as is:
import matplotlib.pyplot as plt
import numpy as np
data1 = np.loadtxt('2_Record2308.dat')
plt.title('2_Record2308')
plt.plot(data1)
plt.show()
I know you can do it with the "spicy" thing but I would prefer not to use it.

Related

Inverse method of ".remove()" in matplotlib

I found out how to remove existing lines in a plot in the following code:
import matplotlib.pyplot as plt
fig=plt.Figure()
axes=fig.add_subplot(111)
line,=axes.plot([0,1,2,3])
line.remove()
After doing this, I wanted to know if I could show the removed line again. Thanks!

How to plot a graph with matplotlib

Good evening, I am a student taking a required python course (completely new to coding) in Hong Kong.
I was asked to write a python program that shows a graph in an assignment, but I had encountered some difficulties.
I tried to follow some source codes I found on the internet, but it end up showing a bug-like page.
Can someone please kindly tell me which part of my code is wrong, how to correct it, and why is this resulting happening?
Also, I would like to ask that is there any difference between import as mpl and as plt?
source code:
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
xaxis = np.array([2,8])
yaxis = np.array([4,9])
plt.plot(xaxis, yaxis )
plt.show()
plt.savefig('table.png')
result:
bug-like result
Thank you all very much. Hope you have a nice day.

Why does the file sound bad when gone through a filter in python

When I try and filter a noisy file with a filter and try and play it again, it is just a mess. I found a solution that worked here, but why do i have to save it like this, and why cant i just play it in python, Can someone explain this?
Here is the code that i used.
import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as sg
from scipy.io import wavfile
import soundfile as sf
import sounddevice as sd
import pandas as pd
path = 'hammingfilter_1.csv'
b = np.genfromtxt(path, delimiter=',')
Fs, data = wavfile.read('pianoise.wav')
sd.play(data, blocking=True) #plays the sound
lfilt = sg.lfilter(b,1,data) #just tried to different ways, shouldnt matter much
y = sg.filtfilt(b,1,data)#
wavfile.write('LPF_filttered.wav', Fs, np.int16(y/np.max(np.abs(y)) * 32767)) # this is the solution im wondering about. This works and the sound is nice and filtered
sd.play(y,Fs,blocking=True)#this sounds totally shit

How to plot large Y values from a file in python?

I have +8000 values that I need to plot in python. I tried several snippets that I found but none worked.
My file (.dat) has the following structure :
-7.1441700e-01
-5.5069000e-01
-4.5883200e-01
-5.5877700e-01
-5.7281500e-01
-7.2219800e-01
-4.0725700e-01
-8.7051400e-01
-1.1273190e+00
-1.0572810e+00
-9.7869900e-01
-9.4284100e-01
-8.7326000e-01
-8.7219200e-01
-7.1533200e-01
-5.2352900e-01
-4.7027600e-01 ....
My goal is to obtain something like this:
But for some reason I'm not getting the same result. Thanks in advance !
Assuming you are using numpy and matplotlib, you should be able to load the data directly into a numpy array and then plot that:
import numpy as np
import matplotlib.pyplot as plt
data = np.loadtxt('filename.dat')
plt.plot(data)
plt.show()
If this does not work, post the error messages you are receiving.

slicing and plotting 3D array with Python

i am rather new in python and before was a MATLAB user. I am sorry if my question is to obvious.
I have a huge 47MB file that contains 3D array (351:467:300). That is 300 images from a camera. I want to re-plot them as a kind of animation. Basically, just slice and plot all 300 images. Here is my code
import numpy as np
import time
import matplotlib.pyplot as plt
import scipy.io as c
datafile = c.loadmat('data.mat') # loading data
img = datafile['img'] # extracting the (351:467:300) array
imgShape = np.shape(img)
for i in range(0,imgShape(2)):
plt.imshow(img[:,:,i])
time.sleep(0.3)
plt.draw()
print('Done!')
the problem is: when it comes to imshow the figure window is black and NOT RESPONDING until it finishes the loop, so i can't see anything. How to solve this? How to force it to update "on stream" ?
And the plotting is veeeryyy slow compared to matlab (not because of time.sleep :) i tried without :) ) loop runs really slowly. I am using Spyder, is that a reason ?
Thanks a lot in advance !
Use the following corrections:
you need to make it interactive.
import numpy as np
#import time
#import matplotlib.pyplot as plt
import scipy.io as c
import pylab as pl #added
datafile = c.loadmat('data.mat') # loading data
img = datafile['img'] # extracting the (351:467:300) array
imgShape = np.shape(img)
pl.ion() #added
for i in range(imgShape(2)): #no need for 0
pl.cla() #added
pl.imshow(img[:,:,i])
# time.sleep(0.3)
pl.draw()
pl.pause(0.3) #added
pl.ioff() #added
print('Done!')

Categories