Skip to content

utils.draw.plot_error_curves

Function ยท Source

mdnc.utils.draw.plot_error_curves(
    gen, x_error_num=10,
    y_error_method='std', plot_method='error',
    xlabel=None, ylabel='value', y_log=False,
    figure_size=(6, 5.5), legend_loc=None, legend_col=None,
    fig=None, ax=None
)

Plot lines with error bars for multiple data groups. Each group is given by:

  • one 1D array and one 2D array, representing the x axis (assuming to have a shape of (N,)) and a stack of data samples (with a shape of (N, M)). Each time point corresponds to M samples in the same distribution.
  • or a 2D array. In this case, we only have the (N, M) data stack.

Arguments

Requries

Argument Type Description
gen object A generator callable object (function), each yield returns a sample. It allows users to provide an extra kwargs dict for each iteration (see Examples). For each iteration, it returns 1D + 2D arrays, or a single 2D array.
x_error_num int The number of displayed error bars.
y_error_method str The method for calculating the error bar. Could be:
  • 'std': use standard error.
  • 'minmax': use the range of the data.

plot_method str The method for plotting the figure. Could be:
  • 'error': use error bar graph.

  • 'fill': use fill_between graph.
xlabel str The x axis label.
ylabel str The y axis label.
y_log bool A flag. Whether to convert the y axis into the logarithmic format.
figure_size (float, float) A tuple with two values representing the (width, height) of the output figure. The unit is inch.
legend_loc str or
int or
(float, float)
The localtion of the legend, see matplotlib.pyplot.legend to view details. (The legend only works when passing label to each iteration).
legend_col int The number of columns of the legend, see matplotlib.pyplot.legend to view details. (The legend only works when passing label to each iteration).
fig object A matplotlib figure instance. If not given, would use plt.gcf() for instead.
ax object A matplotlib subplot instance. If not given, would use plt.gca() for instead.

Examples

Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import numpy as np
import matplotlib.pyplot as plt
import mdnc

@mdnc.utils.draw.setFigure(style='bmh', font_size=16)
def test_error_bar():
    def func_gen():
        size = 100
        x = np.arange(start=0, stop=size)
        for i in range(3):
            begin = 1 + 99.0 * np.random.rand()
            end = 2 + 10 * np.random.rand()
            exp_v = np.square((x - size) / size) - 1.0
            exp_vnoise = np.random.normal(0.0, np.expand_dims((size - x) / (10 * size), axis=-1), (size, 50))
            v = begin * np.exp((np.expand_dims(exp_v, axis=-1) + exp_vnoise) * end)
            yield x, v, {'label': r'$x_{' + str(i + 1) + r'}$'}
    mdnc.utils.draw.plot_error_curves(func_gen(), y_log=True,
                                      y_error_method='minmax',
                                      xlabel='Step', ylabel=r'$\mathcal{L}$')
    plt.show()
    mdnc.utils.draw.plot_error_curves(func_gen(), y_log=True,
                                      y_error_method='minmax', plot_method='fill',
                                      xlabel='Step', ylabel=r'$\mathcal{L}$')
    plt.show()

test_error_bar()


Last update: March 14, 2021

Comments