Python、Matplotlib、subplot:如何设置轴范围? [英] Python, Matplotlib, subplot: How to set the axis range?

查看:58
本文介绍了Python、Matplotlib、subplot:如何设置轴范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将第二个子图的 y 轴范围设置为例如[0,1000] ?我的数据的 FFT 图(文本文件中的一列)导致(inf.?)尖峰,因此实际数据不可见.

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 a (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()

其他改进也值得赞赏!

推荐答案

您有 pylab.ylim:

pylab.ylim([0,1000])

注意:该命令必须在绘图后执行!

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

2021 年更新
由于 现在 matplotlib 强烈反对使用 pylab, 你应该改用 pyplot:

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

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

这篇关于Python、Matplotlib、subplot:如何设置轴范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆