Python, Matplotlib, subplot: How to set the axis range? Python, Matplotlib, subplot: How to set the axis range? python python

Python, Matplotlib, subplot: How to set the axis range?


You have pylab.ylim:

pylab.ylim([0,1000])

Note: The command has to be executed after the plot!

Update 2021
Since the use of pylab is now strongly discouraged by matplotlib, you should instead use pyplot:

from matplotlib import pyplot as pltplt.ylim(0, 100) #corresponding function for the x-axisplt.xlim(1, 1000)


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 pltfig = 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()


Sometimes you really want to set the axes limits before you plot the data. In that case, you can set the "autoscaling" feature of the Axes or AxesSubplot object. The functions of interest are set_autoscale_on, set_autoscalex_on, and set_autoscaley_on.

In your case, you want to freeze the y axis' limits, but allow the x axis to expand to accommodate your data. Therefore, you want to change the autoscaley_on property to False. Here is a modified version of the FFT subplot snippet from your code:

fft_axes = pylab.subplot(h,w,2)pylab.title("FFT")fft = scipy.fft(rawsignal)pylab.ylim([0,1000])fft_axes.set_autoscaley_on(False)pylab.plot(abs(fft))


matomo