【问题标题】:How to mark co-ordinates of points in Octave using "plot" command?如何使用“绘图”命令在 Octave 中标记点的坐标?
【发布时间】:2014-04-02 18:41:02
【问题描述】:

我在 Octave 中使用 plot 命令绘制 yx 的值。有没有办法让图表也显示图表本身绘制的每个坐标的(x,y) 值?

我尝试使用help 命令,但找不到任何此类选项。

另外,如果不可能,有没有办法使用任何其他绘图功能来做到这一点?

【问题讨论】:

    标签: matlab gnuplot octave


    【解决方案1】:

    您是否尝试过在每个坐标处显示一个文本框?

    假设 xy 是已存储在 MATLAB 中的坐标,您可以执行以下操作:

    plot(x, y, 'b.');
    for i = 1 : numel(x) %// x and y are the same lengths
        text(x(i), y(i), ['(' num2str(x(i)) ',' num2str(y(i)) ')']);
    end
    

    以上代码将获取图表中的每个点,并以(x,y) 的格式放置一个文本框(无边框),其中 x 和 y 是所有点的坐标。

    注意:您可能需要调整文本框的位置,因为上面的代码会将每个文本框放在每对坐标的正上方。您可以通过在text 函数的第一个和第二个参数中添加/减去适当的常量来进行操作。 (即text(x(i) + 1, y(i) - 1, .......); 但是,如果您想要一些快速且用于说明目的的东西,那么上面的代码就可以了。

    【讨论】:

      【解决方案2】:

      这是我用来做类似事情的一种方法。为标签生成字符串是最尴尬的部分,真的(我每个标签只有一个数字,这要简单得多)

      x = rand(1, 10);
      y = rand(1, 10);
      scatter(x, y);
      
      % create a text object for each point
      t = text(x, y, '');
      
      % generate a cell array of labels - x and y must be row vectors in this case
      c = strsplit(sprintf('%.2g,%.2g\n',[x;y]), '\n');
      c(end) = [];  % the final \n gives us an extra empty cell, remove it
      c = c';  % transpose to match the dimensions of t
      
      % assign each label to each text object
      set(t, {'String'}, c);
      

      您可能想要使用各种属性,例如 'HorizontalAlignment''VerticalAlignment''Margin',以根据自己的喜好调整标签位置。


      经过深思熟虑,这里有另一种更强大的方法来生成合适的坐标标签元胞数组:

      c = num2cell([x(:) y(:)], 2);
      c = cellfun(@(x) sprintf('%.2g,%.2g',x), c, 'UniformOutput', false);
      

      【讨论】:

        【解决方案3】:

        有一个非常有用的包labelpoints 在 MathWorks 文件交换中正是这样做的。 它有很多方便的功能。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-30
          • 2020-07-23
          • 1970-01-01
          • 1970-01-01
          • 2019-08-27
          • 1970-01-01
          相关资源
          最近更新 更多