跳转到内容

Octave 编程教程/绘图

来自维基教科书,开放世界中的开放书籍

Octave 可以与 gnuplot、Grace、PLplot 协同工作。一些人认为 PLplot 是 Octave 中传统 gnuplot 的替代品。

二维绘图

[编辑 | 编辑源代码]
plot(y)

如果只提供一个数据参数,它将被视为 Y 坐标集,而 X 坐标将被视为元素的索引,从 1 开始。

plot(x, y)
  • 如果第一个参数是向量,第二个参数是矩阵,则向量将相对于矩阵的列(或行)绘制。(使用匹配的组合,首先尝试列。)
  • 如果第一个参数是矩阵,第二个参数是向量,则矩阵的列(或行)将相对于向量绘制。(使用匹配的组合,首先尝试列。)
  • 如果两个参数都是向量,则 Y 的元素将相对于 X 的元素绘制。
  • 如果两个参数都是矩阵,则 Y 的列将相对于 X 的列绘制。在这种情况下,两个矩阵必须具有相同数量的行和列,并且不会尝试转置参数以使行数匹配。
linspace(b, l, n)

将生成一个包含 b 和 l 之间 n 个线性间隔数字的向量。它经常用于在 plot 的双参数形式中生成向量 X。

x = linspace(0, 10, 100);
y = sin(x);
plot(x, y);

非线性绘图

[编辑 | 编辑源代码]

semilogx、semiology、loglog、polar

格式化

[编辑 | 编辑源代码]

可以使用附加的 FMT 参数对绘图进行格式化。

plot (x, [y,] FMT, )

在参数中,可以出现以下任何选项

    `-'
          Set lines plot style (default).

    `.'
          Set dots plot style.

    `@'
          Set points plot style.

    `-@'
          Set linespoints plot style.

    `^'
          Set impulses plot style.

    `L'
          Set steps plot style.

    `N'
          Interpreted as the plot color if N is an integer in the range
          1 to 6.

    `NM'
          If NM is a two digit integer and M is an integer in the range
          1 to 6, M is interpreted as the point style.  This is only
          valid in combination with the `@' or `-@' specifiers.

    `C'
          If C is one of `"r"', `"g"', `"b"', `"m"', `"c"', or `"w"',
          it is interpreted as the plot color (red, green, blue,
          magenta, cyan, or white).

    `";title;"'
          Here `"title"' is the label for the key.

    `+'
    `*'
    `o'
    `x'
          Used in combination with the points or linespoints styles,
          set the point style.

     The color line styles have the following meanings on terminals that
     support color.

          Number  Gnuplot colors  (lines)points style
            1       red                   *
            2       green                 +
            3       blue                  o
            4       magenta               x
            5       cyan                house
            6       white           there exists

     The FMT argument can also be used to assign key titles.  To do so,
     include the desired title between semi-colons after the formatting
     sequence described above, e.g. "+3;Key Title;" Note that the last
     semi-colon is required and will generate an error if it is left
     out.

此外,还可以同时将一组属性-值对应用于 plot 绘制的每条“线”。

plot (x, [y,] FMT, property, value, )

这里我们不在图形上绘制线,而是为每个数据点放置一个 '+' 标记,选择第一个颜色(红色),并添加图例。

x = linspace(0, 2*pi, 50);
y = sin(x);
plot(x, y, '+1;sin(x);');

第二个示例添加了另一条线以及一些属性-值对。

x = linspace(0, 2*pi, 100);
y = sin(x);
y2 = cos(x);
plot(x, y, '+1;sin(x);', "markersize", 10, x, y2, ";cos(x);", "markersize", 5, "marker", '*');
[编辑 | 编辑源代码]

xlabel、ylabel、title 和 refresh

三维绘图

[编辑 | 编辑源代码]

mesh、meshgrid、surf

等高线图

[编辑 | 编辑源代码]

contour

contour (Z)

contour (Z, VN)

contour (X, Y, Z)

contour (X, Y, Z, VN)

contour (..., STYLE)

contour (H, ...)

[C, H] = contour (...)

    Plot level curves (contour lines) of the matrix Z, using the
    contour matrix C computed by `contourc' from the same arguments;
    see the latter for their interpretation.  The set of contour
    levels, C, is only returned if requested.  For example:
         x = 0:2;
         y = x;
         z = x' * y;
         contour (x, y, z, 2:3)
    The style to use for the plot can be defined with a line style
    STYLE in a similar manner to the line styles used with the `plot'
    command.  Any markers defined by STYLE are ignored.
    The optional input and output argument H allows an axis handle to
    be passed to `contour' and the handles to the contour objects to  be returned.

contourc

[C, LEV] = contourc (X, Y, Z, VN)

    Compute isolines (contour lines) of the matrix Z.  Parameters X, Y
    and VN are optional.
    The return value LEV is a vector of the contour levels.  The
    return value C is a 2 by N matrix containing the contour lines in
    the following format
         C = [lev1, x1, x2, ..., levn, x1, x2, ...
              len1, y1, y2, ..., lenn, y1, y2, ...]
    in which contour line N has a level (height) of LEVN and length of
    LENN.
    If X and Y are omitted they are taken as the row/column index of
    Z.  VN is either a scalar denoting the number of lines to compute
    or a vector containing the values of the lines.  If only one value
    is wanted, set `VN = [val, val]'; If VN is omitted it defaults to
    10.
    For example,
         x = 0:2;
         y = x;
         z = x' * y;
         contourc (x, y, z, 2:3)
              =>   2.0000   2.0000   1.0000   3.0000   1.5000   2.0000
              2.0000   1.0000   2.0000   2.0000   2.0000   1.5000

colormap、image、imshow、hsv2rgb

>> help colormap 'colormap' is a function from the file C:\Octave\Octave-4.0.1\share\octave\4.0.1\ m\image\colormap.m

-- Function File: CMAP = colormap ()
-- Function File: CMAP = colormap (MAP)
-- Function File: CMAP = colormap ("default")
-- Function File: CMAP = colormap ("MAP_NAME")
-- Function File: CMAP = colormap (HAX, ...)
-- Command:  colormap MAP_NAME
-- Function File: CMAPS = colormap ("list")
-- Function File:  colormap ("register", "NAME")
-- Function File:  colormap ("unregister", "NAME")
    Query or set the current colormap.
    With no input arguments, `colormap' returns the current color map.
    `colormap (MAP)' sets the current colormap to MAP.  The colormap
    should be an N row by 3 column matrix.  The columns contain red,
    green, and blue intensities respectively.  All entries must be
    between 0 and 1 inclusive.  The new colormap is returned.
    `colormap ("default")' restores the default colormap (the `jet'
    map with 64 entries).  The default colormap is returned.
    The map may also be specified by a string, "MAP_NAME", where
    MAP_NAME is the name of a function that returns a colormap.
    If the first argument HAX is an axes handle, then the colormap for
    the parent figure of HAX is queried or set.
    For convenience, it is also possible to use this function with the
    command form, `colormap MAP_NAME'.
    `colormap ("list")' returns a cell array with all of the available
    colormaps.  The options "register" and "unregister" add or remove
    the colormap NAME from this list.
    See also: jet.

保存和打印图形

[编辑 | 编辑源代码]

将图形打印到打印机或保存到文件

print("-Pprinter")
print("filename", "-ddevice")

设备

         `ps'
         `ps2'
         `psc'
         `psc2'
               Postscript (level 1 and 2, mono and color)

         `eps'
         `eps2'
         `epsc'
         `epsc2'
               Encapsulated postscript (level 1 and 2, mono and color)

         `ill'
         `aifm'
               Adobe Illustrator

         `cdr'
         `corel'
               CorelDraw

         `hpgl'
               HP plotter language

         `fig'
               XFig

         `dxf'
               AutoCAD

         `mf'
               Metafont

         `png'
               Portable network graphics

         `pbm'
               PBMplus
         `svg`
               Scalar Vector Graphics

如果省略设备,则从文件扩展名推断,或者如果没有文件名,则以 Postscript 格式发送到打印机。

另请参阅

[编辑 | 编辑源代码]

返回 Octave 编程教程索引

华夏公益教科书