4293ec98创建于 2022年12月11日历史提交

Line Plot

Draw line plots, either of one vector :math:y, or two vectors :math:x and :math:y against each other. Optionally the axes may also be provided.

Examples

  1. Plot four lines with different styles.

::

let x = linspace<Float64>(0.0, 2.0 * PI64)
let y = x.apply(sin)
plot(x, y, line_spec:"-o")
hold(true)
plot(x, y.apply({y => -y}), line_spec:"--xr")
plot(x, x.apply({x => x / PI64 - 1.0}), line_spec:"-:gs")
plot(vector<Float64>([1.0, 0.7, 0.4, 0.0, -0.4, -0.7, -1.0]), line_spec:"k")

Result:

.. image:: ../../../tests/imgs/line_plot/plot_1.png :align: center :width: 360

  1. Plot four lines with different (default) colors.

::

let y = [
    vector([16.0, 5.0, 9.0, 4.0]),
    vector([2.0, 11.0, 7.0, 14.0]),
    vector([3.0, 10.0, 6.0, 15.0]),
    vector([13.0, 8.0, 12.0, 1.0])
]
plot(y[0])
hold(true)
plot(y[1])
plot(y[2])
plot(y[3])

Result:

.. image:: ../../../tests/imgs/line_plot/plot_2.png :align: center :width: 360

  1. Plot three sine waves with different phases.

::

let x = linspace<Float64>(0.0, 2.0 * PI64)
let y1 = x.apply(sin)
let y2 = x.apply({x => sin(x - 0.25)})
let y3 = x.apply({x => sin(x - 0.5)})
plot(x, y1)
hold(true)
plot(x, y2, line_spec:"--")
plot(x, y3, line_spec:":")

Result:

.. image:: ../../../tests/imgs/line_plot/plot_3.png :align: center :width: 360

  1. Another set of sine waves with different phases and styles.

::

let x = linspace<Float64>(0.0, 2.0 * PI64)
let y1 = x.apply(sin)
let y2 = x.apply({x => sin(x - 0.25)})
let y3 = x.apply({x => sin(x - 0.5)})
plot(x, y1, line_spec:"g")
hold(true)
plot(x, y2, line_spec:"b--o")
plot(x, y3, line_spec:"c*")

Result:

.. image:: ../../../tests/imgs/line_plot/plot_4.png :align: center :width: 360

  1. Line plots with markers.

::

let x = linspace<Float64>(0.0, 10.0)
let y = x.apply(sin)
plot(x, y, line_spec:"-o").marker_indices(vector<Int64>(
    [0, 5, 10, 15, 20, 25, 30, 35, 40, 45,
     50, 55, 60, 65, 70, 75, 80, 85, 90, 95]))

Result:

.. image:: ../../../tests/imgs/line_plot/plot_5.png :align: center :width: 360

  1. Line plots with markers, different styles.

::

let x = linspace<Float64>(-PI64, PI64, num:20)
let y = x.apply({x => tan(sin(x)) - sin(tan(x))})
var line = plot(x, y, line_spec:"--gs").line_width(2.0)
    .marker_size(10.0).marker_color("b")
    .marker_face_color(0.5, 0.5, 0.5)

Result:

.. image:: ../../../tests/imgs/line_plot/plot_6.png :align: center :width: 360

  1. Line plot with title, x-label and y-label.

::

let x = linspace<Float64>(0.0, 10.0, num:150)
let y = x.apply({x => cos(5.0 * x)})
plot(x, y).color(0.0, 0.7, 0.9)
title("2-D Line Plot")
xlabel("x")
ylabel("cos(5x)")

Result:

.. image:: ../../../tests/imgs/line_plot/plot_7.png :align: center :width: 360

  1. Line plot with time on the x-axis.

::

let x = linspace<Float64>(0.0, 180.0, num:7)
let y = vector<Float64>([0.8, 0.9, 0.1, 0.9, 0.6, 0.1, 0.3])
plot(x, y)
title("Time Plot")
xlabel("Time")
yrange(0.0, 1.0)
xticks(vector<Float64>([0.0, 30.0, 60.0, 90.0, 120.0, 150.0, 180.0]))
xticklabels(Array<String>(["00:00s", "00:30", "01:00", "01:30", "02:00", "02:30", "03:00"]))

Result:

.. image:: ../../../tests/imgs/line_plot/plot_8.png :align: center :width: 360

  1. Line plots on tiled layout.

::

let x = linspace<Float64>(0.0, 3.0)
let y1 = x.apply({x => sin(5.0 * x)})
let y2 = x.apply({x => sin(15.0 * x)})

tiledlayout(2, 1)
let ax1 = nexttile()
plot(ax1, x, y1)
title(ax1, "Top Plot")
ylabel(ax1, "sin(5x)")

let ax2 = nexttile()
plot(ax2, x, y2)
title(ax2, "Bottom Plot")
ylabel(ax2, "sin(15x)")

Result:

.. image:: ../../../tests/imgs/line_plot/plot_9.png :align: center :width: 360

  1. Line plot with different line width and marker style.

::

let x = linspace<Float64>(-2.0 * PI64, 3.0)
let y1 = x.apply(sin)
let y2 = x.apply(cos)

let line1 = plot(x, y1)
hold(true)
let line2 = plot(x, y2)
line1.line_width(2.0)
line2.marker(MarkerStyle.Asterisk)

Result:

.. image:: ../../../tests/imgs/line_plot/plot_10.png :align: center :width: 360

  1. Drawing a circle with line plot, setting equal scale for x and y-axis.

::

let r: Float64 = 2.0
let xc: Float64 = 4.0
let yc: Float64 = 3.0
let theta = linspace<Float64>(0.0, 2.0 * PI64)
let x = theta.apply({theta => r * cos(theta) + xc})
let y = theta.apply({theta => r * sin(theta) + yc})
plot(x, y)
axis(AxisStyle.Equal)

Result:

.. image:: ../../../tests/imgs/line_plot/plot_11.png :align: center :width: 360

  1. Plotting a list of integers.

::

let y = vector<Int32>([2, 4, 7, 7, 6, 3, 9, 7, 3, 5])
plot(y)

Result:

.. image:: ../../../tests/imgs/line_plot/plot_12.png :align: center :width: 360

Methods

======================= ================================= Method Description ======================= ================================= plot 4 methods Line plot. ======================= =================================

Description

.. function:: plot(x, y[, line_spec])

:param x: Vector<Float64>
:param y: Vector<Float64>
:param line_spec: String
:return: Line

Line plot with :math:`y` versus :math:`x`.

.. function:: plot(y[, line_spec])

:param y: Vector<Float64>
:param line_spec: String
:return: Line

Line plot with :math:`y`.

.. function:: plot(axes, x, y[, line_spec])

:param axes: AxesType
:param x: Vector<Float64>
:param y: Vector<Float64>
:return: Line

Line plot with :math:`y` versus :math:`x` on the given axes.

.. function:: plot(axes, y[, line_spec])

:param axes: AxesType
:param y: Vector<Float64>
:return: Line

Line plot with :math:`y` on the given axes.