如何设置子图轴范围
如何将第二个子图的 y 轴范围设置为 [0,1000] ? 我的数据(文本文件中的一列)的 FFT 图会产生(inf.?)峰值,因此实际数据不可见。
pylab.ylim([0,1000])
不幸的是,没有效果。这是整个脚本:
# 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()
其他改进也值得赞赏!
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()
Other improvements are also appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您有
pylab.ylim
:注意:该命令必须在绘图后执行!
2021 年更新
由于 matplotlib 现在强烈建议不要使用 pylab ,你应该使用 pyplot:
You have
pylab.ylim
: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:
使用 axes 对象 是一个很好的方法。如果您想与多个图形和子图交互,它会很有帮助。要直接添加和操作轴对象:
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:
有时您确实想在绘制数据之前设置轴限制。在这种情况下,您可以设置 Axes 或 AxesSubplot 对象的“自动缩放”功能。感兴趣的函数是
set_autoscale_on
、set_autoscalex_on
和set_autoscaley_on
。在您的情况下,您希望冻结 y 轴的限制,但允许 x 轴扩展以容纳您的数据。因此,您需要将
autoscaley_on
属性更改为False
。以下是代码中 FFT 子图片段的修改版本: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
orAxesSubplot
object. The functions of interest areset_autoscale_on
,set_autoscalex_on
, andset_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 toFalse
. Here is a modified version of the FFT subplot snippet from your code:如果您有多个子图,即
您可以对所有子图使用相同的 y 限制。它从第一个图中获取 y 轴的限制。
If you have multiple subplots, i.e.
You can use the same y limits for all of them. It gets limits of y ax from first plot.
也可以在向现有图形实例添加子图时设置
ylim
/xlim
(plt.subplot
承认ylim = 参数)。
话又说回来,使用面向对象的接口更简洁、更清晰。 Axes 实例定义
set()
方法,可用于设置包括 y-limit/title 等在内的一系列属性。两组代码产生相同的以下输出。
It's also possible to set
ylim
/xlim
at the time of adding a subplot to the existing figure instance (plt.subplot
admitsylim=
argument).Then again, using the object-oriented interface is less verbose and clearer. Axes instances define
set()
method which can be used to set a whole host of properties including y-limit/title etc.Both sets of codes produce the same following output.
如果您知道所需的确切轴,则
pylab.ylim([0,1000])
的工作方式如前面所回答。但是,如果您想要一个更灵活的轴来适应您的确切数据,就像我发现这个问题时所做的那样,那么将轴限制设置为数据集的长度。如果您的数据集是
fft
如问题所示,请在绘图命令后添加以下内容:length = (len(fft))
pylab.ylim([0,长度])
If you know the exact axis you want, then
pylab.ylim([0,1000])
works as answered previously. But if you want a more flexible axis to fit your exact data, as I did when I found this question, then set axis limit to be the length of your dataset. If your dataset is
fft
as in the question, then add this after your plot command:length = (len(fft))
pylab.ylim([0,length])