# Helper code to plot binary losses.
#
# Eli Bendersky (http://eli.thegreenplace.net)
# This code is in the public domain
from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np


if __name__ == '__main__':
    fig, ax = plt.subplots()
    fig.set_tight_layout(True)

    xs = np.linspace(-2, 2, 500)

    # plot L0/1 loss
    ax.plot(xs, np.where(xs < 0, np.ones_like(xs), np.zeros_like(xs)),
            color='r', linewidth=2.0, label='$L_{01}$')

    # plot square loss
    ax.plot(xs, (xs - 1) ** 2, linestyle='-.', label='$L_2$')

    # plot hinge loss
    ax.plot(xs, np.maximum(np.zeros_like(xs), 1 - xs),
            color='g', linewidth=2.0, label='$L_h$')

    ax.grid(True)

    plt.ylim((-1, 4))
    ax.legend()

    fig.savefig('loss.png', dpi=80)
    plt.show()

[((11, 14, 11, 28), 'matplotlib.pyplot.subplots', 'plt.subplots', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((14, 9, 14, 32), 'numpy.linspace', 'np.linspace', ({(14, 21, 14, 23): '-2', (14, 25, 14, 26): '2', (14, 28, 14, 31): '500'}, {}), '(-2, 2, 500)', True, 'import numpy as np\n'), ((29, 4, 29, 21), 'matplotlib.pyplot.ylim', 'plt.ylim', ({(29, 13, 29, 20): '(-1, 4)'}, {}), '((-1, 4))', True, 'import matplotlib.pyplot as plt\n'), ((33, 4, 33, 14), 'matplotlib.pyplot.show', 'plt.show', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((17, 33, 17, 49), 'numpy.ones_like', 'np.ones_like', ({(17, 46, 17, 48): 'xs'}, {}), '(xs)', True, 'import numpy as np\n'), ((17, 51, 17, 68), 'numpy.zeros_like', 'np.zeros_like', ({(17, 65, 17, 67): 'xs'}, {}), '(xs)', True, 'import numpy as np\n'), ((24, 27, 24, 44), 'numpy.zeros_like', 'np.zeros_like', ({(24, 41, 24, 43): 'xs'}, {}), '(xs)', True, 'import numpy as np\n')]