In [ ]:
# Init matplotlib

%matplotlib inline

import matplotlib
from IPython.display import Image

In [ ]:
FIG_SIZE = 5

def plot_linear_function(a, b, c, bounds=None, ax=None, color='r', alpha=1):
    
    # Check arguments #############
    
    if bounds is None:
        if ax is None:
            bounds = np.array([[-10, 10],
                               [-10, 10]])
        else:
            bounds = np.array(ax.axis()).reshape([2,2])
    
    assert bounds.ndim == 2
    assert bounds.shape == (2, 2)
    assert bounds[0,0] < bounds[0,1]
    assert bounds[1,0] < bounds[1,1]
    
    # Setup the plot ##############
    
    if ax is None:
        fig, ax = plt.subplots(figsize=(FIG_SIZE, FIG_SIZE))
        
    ax.axis('equal')

    ax.axis(bounds.flatten())
    ax.set_xticks(np.arange(*bounds[0], 1))
    ax.set_yticks(np.arange(*bounds[0], 1))

    ax.axhline(y=0, color='k')
    ax.axvline(x=0, color='k')
    
    # Plot ########################
    
    x = bounds[0]            # horizontal axis limits
    
    if b != 0:
        yp1 = (c - a * x[0]) / b
        yp2 = (c - a * x[1]) / b
        plt.plot(x, np.array([yp1, yp2]), "-{}".format(color), alpha=alpha)
    else:
        plt.axvline(c / a, color=color, alpha=alpha)
    
    # TODO: add label (e.g. "2x + 3y = 1")
    plt.title(r"{:0.1f} $x_1$ + {:0.1f} $x_2$ = {:0.1f}".format(a, b, c))

    plt.grid(True)
    
    return ax

In [ ]:
plot_linear_function(1, 2, 3);

Make an animation for $a$


In [ ]:
a_array = np.concatenate((np.linspace(0., 3., 31), np.linspace(3., -3., 61), np.linspace(-3., 0., 31)))
a_array

In [ ]:
for i, a in enumerate(a_array):
    ax = plot_linear_function(a, 2, 3)
    plt.savefig("img_{:04d}.png".format(i))
    plt.close()                            # <- to don't show the plot

In [ ]:
!convert img_*.png imga.gif

In [ ]:
Image("imga.gif")

Make an animation for $b$


In [ ]:
b_array = np.concatenate((np.linspace(0., 3., 31), np.linspace(3., -3., 61), np.linspace(-3., 0., 31)))
b_array

In [ ]:
for i, b in enumerate(b_array):
    ax = plot_linear_function(1, b, 3)
    plt.savefig("img_{:04d}.png".format(i))
    plt.close()                            # <- to don't show the plot

In [ ]:
!convert img_*.png imgb.gif

In [ ]:
Image("imgb.gif")

Make an animation for $c$


In [ ]:
c_array = np.concatenate((np.linspace(0., 3., 31), np.linspace(3., -3., 61), np.linspace(-3., 0., 31)))
c_array

In [ ]:
for i, c in enumerate(c_array):
    ax = plot_linear_function(1, 2, c)
    plt.savefig("img_{:04d}.png".format(i))
    plt.close()                            # <- to don't show the plot

In [ ]:
!convert img_*.png imgc.gif

In [ ]:
Image("imgc.gif")