【问题标题】:In matlab, how can I zoom in on a plot in my script在 matlab 中,如何放大脚本中的绘图
【发布时间】:2010-08-02 17:20:46
【问题描述】:

我想使用脚本放大绘图。我只对水平约束缩放感兴趣。所以我想做类似的事情

p = plot(myData);
z = zoom;
set(z, 'ZoomInToPoints' , [50 100]);

p = plot(myData);
myZoom([50, 100]);

因此,这些功能中的任何一个都会像使用放大镜工具放大一样放大绘图。我只指定两个点,因为我只想水平缩放。

注意,我已经尝试过为此使用 xlim。虽然它有效,但它不允许我在我需要的地块上使用命令 text

【问题讨论】:

  • 在使用xlim之后“它不允许我使用命令text”是什么意思?函数xlim 只是设置x 轴范围。它不应该影响对text 的调用。
  • 没有任何文字实际出现在情节中。它可能仍然有效,但将文本放在错误的位置。
  • 我很漂亮 zoom 并不能解决你的问题。您必须编辑文本位置。
  • 嗯,当我用放大镜放大时,一切正常。有没有办法通过在我的脚本中发出命令来放大?
  • 这种行为有点奇怪。也许您可以给我们更多代码,以便我们查看是否有其他原因导致此问题。

标签: matlab zooming


【解决方案1】:

调用text 会将文本固定在图表上的一组特定坐标处。您是否尝试在调用 xlim 后更新这些?

编辑:您可以随时调整文本位置:

x=1:.1:10;
y=sin(.1*x);
plot(x,y)
text(6,.8,'test') %#Sample figure

F=get(0,'children'); %#Figure handle
A=get(F,'Children'); %#Axes handle
T=findobj(A,'Type','text'); %# Text handle
oldxlim=xlim; %#grab the original x limits before zoom
oldpos=get(T,'Position'); %#get the old text position
set(A,'xlim',[5 15]); %#Adjust axes
newxlim=xlim;
newpos=[(oldpos(1)-oldxlim(1))*(diff(newxlim))...
/(diff(oldxlim))+newxlim(1) oldpos(2:end)]; 
%#interpolate to place the text at the same spot in the axes
set(T,'Position',newpos) %#Finally reset the text position

不漂亮,但应该可以。如果每个轴或每个图形的轴有多个注释,则始终可以将上述代码放入循环中。

【讨论】:

    【解决方案2】:

    textxlim 有什么问题?这不是您想要的行为类型吗?

    plot(1:100,randn(100,1))
    text(80,1.5,'text')
    set(gca,'XLim',[70 100]) % notice that text stays at same point in "data space" but moves in "axis space"
    text(80,1,'text2'); % new text appears in axis space as well
    

    如果我误解了,并且您希望文本出现在轴空间中的特定点(而不是 text 使用的数据空间),无论您的放大程度如何,您都可以为您创建另一组轴文字:

    inset_h = axes('position',[0.5 0.5 0.2 0.2])
    set(inset_h,'Color','none'); axis off
    text(0,0,'text')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      • 2018-01-05
      • 1970-01-01
      • 2017-01-11
      相关资源
      最近更新 更多