Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

How can I set the y-axis range of the second subplot to e.g. [0,1000]? The FFT plot of my data (a column in a text file) results in an (inf.?) spike so that the actual data is not visible.

pylab.ylim([0,1000])

has no effect, unfortunately. This is the whole script:

# based on http://www.swharden.com/blog/2009-01-21-signal-filtering-with-python/ 

import numpy, scipy, pylab, random 

xs = []

rawsignal = [] 

with open("test.dat", 'r') as f: 

for line in f: 

if line[0] != '#' and len(line) > 0: 

xs.append( int( line.split()[0] ) )

rawsignal.append( int( line.split()[1] ) )

h, w = 3, 1 

pylab.figure(figsize=(12,9)) 

pylab.subplots_adjust(hspace=.7) 

pylab.subplot(h,w,1) 

pylab.title("Signal") 

pylab.plot(xs,rawsignal) 

pylab.subplot(h,w,2) 

pylab.title("FFT") 

fft = scipy.fft(rawsignal) 

#~ pylab.axis([None,None,0,1000]) 

pylab.ylim([0,1000]) 

pylab.plot(abs(fft)) 

pylab.savefig("SIG.png",dpi=200) 

pylab.show()

Other improvements are also appreciated!

1 Answer

0 votes
by (106k points)

It can be done by using axes objects is a great approach for this. It helps if you want to interact with multiple figures and sub-plots. To add and manipulate the axes objects directly:

import matplotlib.pyplot as plt 

fig = plt.figure(figsize=(12,9)) 

signal_axes = fig.add_subplot(211) signal_axes.plot(xs,rawsignal) 

fft_axes = fig.add_subplot(212) 

fft_axes.set_title("FFT") 

fft_axes.set_autoscaley_on(False) 

fft_axes.set_ylim([0,1000]) 

fft = scipy.fft(rawsignal) 

fft_axes.plot(abs(fft)) 

plt.show()

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 18, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...