• The Python tutorials are written as Jupyter notebooks and run directly in Google Colab—a hosted notebook environment that requires no setup. Click the Run in Google Colab button.


  • Colab link - Open colab


  • Matplotlib is the "grandfather" library of data visualization with Python. It was created by John Hunter. He created it to try to replicate MatLab's (another programming language) plotting capabilities in Python. So if you happen to be familiar with matlab, matplotlib will feel natural to you. Matplotlib is a plotting library for Python. It is used along with NumPy to provide an environment that is an effective open source alternative for MatLab.


  • It is an excellent 2D and 3D graphics library for generating scientific figures.


  • Some of the major Pros of Matplotlib are:


  • * Generally easy to get started for simple plots


  • * Support for custom labels and texts


  • * Great control of every element in a figure


  • * High-quality output in many formats


  • * Very customizable in general


  • **HOW TO USE COLAB ??? SEE : https://www.youtube.com/watch?v=dT_XDTarsjw


  • **MATPLOTLIB TUT : https://www.youtube.com/watch?v=q7Bo_J8x_dw** ##Importing matplotlib


  • To import matplotlib in Colaboratory under the name **plt** from module **matplotlib.pyplot** type the following:


  •  
    import matplotlib.pyplot as plt
    
     
    
  • Note:** If you are using Colaboratory **plt.show()** at the end of all the plooting commands to have the figure pop up in another window.


  • Basic Example Let's walk through a very simple example using two numpy arrays. You can also use lists, but most likely you'll be passing numpy arrays or pandas columns (which essentially also behave like arrays).


  • Let us create the data we want to plot.


  •  
    
    import numpy as np
    x = np.linspace(0, 5, 11)
    y = x ** 2
    
    x
    
    y
     
    
  • We created 2 set of numbers **x** and **y**.


  • Basic Matplotlib Commands We can create a very simple line plot using the following:


  •  
    plt.plot(x, y, 'y') # 'r' is the color red
    
    plt.xlabel('X Axis Title Here')
    plt.ylabel('Y Axis Title Here')
    plt.title('String Title Here')
    plt.show()
     
    
  • Creating Multiplots on Same Canvas


  • Horizontal subplot - Use the code below to create a horizontal subplot


  •  
    # plt.subplot(nrows, ncols, plot_number)
    plt.subplot(2,1,1)
    plt.plot(x, y, 'r--') # More on color options later
    plt.subplot(2,1,2)
    plt.plot(y, x, 'g*-');
     
    
  • Vertical subplot - By changing the subplot parameters we can create a vertical plot


  •  
    # plt.subplot(nrows, ncols, plot_number)
    plt.subplot(1,2,1)
    plt.plot(x, y, 'r--') # More on color options later
    plt.subplot(1,2,2)
    plt.plot(y, x, 'g*-');
     
    
  • Subplot grid - To create a 2×2 grid of plots, you can use this code:


  •  
    # plt.subplot(nrows, ncols, plot_number)
    plt.subplot(2,2,1)
    plt.plot(x, y, 'r') # More on color options later
    plt.subplot(2,2,2)
    plt.plot(x, y, 'g');
    plt.subplot(2,2,3)
    plt.plot(x, y, 'b') # More on color options later
    plt.subplot(2,2,4)
    plt.plot(x, y, 'y');
    
    
     
    
  • Matplotlib Object Oriented Method Now that we've seen the basics, let's break it all down with a more formal introduction of Matplotlib's Object Oriented API. This means we will instantiate figure objects and then call methods or attributes from that object.


  • Introduction to the Object Oriented Method The main idea in using the more formal Object Oriented method is to create figure objects and then just call methods or attributes off of that object. This approach is nicer when dealing with a canvas that has multiple plots on it.


  • To begin we create a figure instance. Then we can add axes to that figure:


  •  
    # Create Figure (empty canvas)
    fig = plt.figure()
    x=[0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. ]
    y=[ 0.  ,  0.25,  1.  ,  2.25,  4.  ,  6.25,  9.  , 12.25, 16.  ,
           20.25, 25.  ]
    # Add set of axes to figure
    axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # left, bottom, width, height (range 0 to 1)
    
    # Plot on that set of axes
    axes.plot(x, y, 'b')
    axes.set_xlabel('Set X Label') # Notice the use of set_ to begin methods
    axes.set_ylabel('Set y Label')
    axes.set_title('Set Title')
     
    
  • Code is a little more complicated, but the advantage is that we now have full control of where the plot axes are placed, and we can easily add more than one axis to the figure:


  •  
    # Creates blank canvas
    fig = plt.figure()
    
    axes1 = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # main axes
    axes2 = fig.add_axes([0.2, 0.5, 0.4, 0.3]) # inset axes
    
    # Larger Figure Axes 1
    axes1.plot(x, y, 'b')
    axes1.set_xlabel('X_label_axes2')
    axes1.set_ylabel('Y_label_axes2')
    axes1.set_title('Axes 2 Title')
    
    # Insert Figure Axes 2
    axes2.plot(y, x, 'r')
    axes2.set_xlabel('X_label_axes2')
    axes2.set_ylabel('Y_label_axes2')
    axes2.set_title('Axes 2 Title');
     
    
  • subplots() The plt.subplots() object will act as a more automatic axis manager.


  • Basic use cases:


  •  
    # Use similar to plt.figure() except use tuple unpacking to grab fig and axes
    fig, axes = plt.subplots()
    
    # Now use the axes object to add stuff to plot
    axes.plot(x, y, 'r')
    axes.set_xlabel('x')
    axes.set_ylabel('y')
    axes.set_title('title');
     
    
  • Then you can specify the number of rows and columns when creating the subplots() object:


  •  
    # Empty canvas of 1 by 2 subplots
    fig, axes = plt.subplots(nrows=1, ncols=2)
    
    # Axes is an array of axes to plot on
    axes
    
     
    
  • Note: Here you can see that **axes** is an **array** type. That means we can iterate through it.


  •  
    for ax in axes:
        ax.plot(x, y, 'b')
        ax.set_xlabel('x')
        ax.set_ylabel('y')
        ax.set_title('title')
    
    # Display the figure object    
    fig 
     
    
  • If you debug your program by simply printing ax, you'll quickly find out that ax is a two-dimensional array: one dimension for the rows, one for the columns.


  • Thus, you need two indices to index ax to retrieve the actual AxesSubplot instance, like ax[r,c].plot(...)


  • A common issue with matplolib is overlapping subplots or figures. We ca use **fig.tight_layout()** or **plt.tight_layout()** method, which automatically adjusts the positions of the axes on the figure canvas so that there is no overlapping content:


  •  
    fig, axes = plt.subplots(nrows=1, ncols=2)
    
    for ax in axes:
        ax.plot(x, y, 'g')
        ax.set_xlabel('x')
        ax.set_ylabel('y')
        ax.set_title('title')
    
    fig    
    plt.tight_layout()
     
    
  • Figure size, aspect ratio and DPI Matplotlib allows the aspect ratio, DPI and figure size to be specified when the Figure object is created. You can use the figsize and dpi keyword arguments.


  • * **figsize** is a tuple of the width and height of the figure in inches


  • * **dpi** is the dots-per-inch (pixel per inch).


  • For example:


  •  
    fig = plt.figure(figsize=(8,4), dpi=100)
     
    
  • The same arguments can also be passed to layout managers, such as the **subplots** function:


  •  
    fig, axes = plt.subplots(figsize=(12,3))
    
    axes.plot(x, y, 'r')
    axes.set_xlabel('x')
    axes.set_ylabel('y')
    axes.set_title('title');
    
     
    
  • Saving figures Matplotlib can generate high-quality output in a number formats, including PNG, JPG, EPS, SVG, PGF and PDF.


  • To save a figure to a file we can use the **savefig** method in the **Figure** class:


  •  
    fig.savefig("filename.png")
     
    
  • Here we can also optionally specify the DPI and choose between different output formats:


  •  
    fig.savefig("filename.png", dpi=200)
     
    
  • Legends, labels and titles Now that we have covered the basics of how to create a figure canvas and add axes instances to the canvas, let's look at how decorate a figure with titles, axis labels, and legends.


  • Figure titles - A title can be added to each axis instance in a figure. To set the title, use the **set_title** method in the axes instance:


  •  
    ax.set_title("title");
    
     
    
  • Axis labels - Similarly, with the methods **set_xlabel** and **set_ylabel**, we can set the labels of the X and Y axes:


  •  
    ax.set_xlabel("x")
    ax.set_ylabel("y");
    
     
    
  • Legends You can use the **label="label text"** keyword argument when plots or other objects are added to the figure, and then using the **legend** method without arguments to add the legend to the figure:


  •  
    import numpy as np
    import matplotlib.pyplot as plt
    
    
    
    x = np.linspace(0, 5, 11)
    y = x ** 2
    fig = plt.figure()
    axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])
    
    ax.plot(x, x ** 2, label="x**2")
    ax.plot(x, y ** 2, label="x**3")
    ax.legend()
     
    
  • The legend function takes an optional keyword argument loc that can be used to specify where in the figure the legend is to be drawn. The allowed values of loc are numerical codes for the various places the legend can be drawn. See the documentation page for details. Some of the most common loc values are:


  •  
    # Lots of options....
    
    ax.legend(loc=1) # upper right corner
    ax.legend(loc=2) # upper left corner
    ax.legend(loc=3) # lower left corner
    ax.legend(loc=4) # lower right corner
    
    # .. many more options are available
    
    # Most common to choose
    ax.legend(loc=0) # let matplotlib decide the optimal location
    fig
    
     
    
  • Setting colors, linewidths, linetypes Matplotlib gives you *a lot* of options for customizing colors, linewidths, and linetypes.


  • With matplotlib, we can define the colors of lines and other graphical elements in a number of ways. First of all, we can use the MATLAB-like syntax where 'b' means blue, 'g' means green, etc. The MATLAB API for selecting line styles are also supported: where, for example, 'b.-' means a blue line with dots:


  •  
    fig, ax = plt.subplots()
    ax.plot(x, x**2, 'b.-') # blue line with dots
    ax.plot(x, x**3, 'g--') # green dashed line
    
     
    
  • Colors with the color= parameter We can also define colors by their names or RGB hex codes and optionally provide an alpha value using the color and alpha keyword arguments. Alpha indicates opacity.


  •  
    fig, ax = plt.subplots()
    
    ax.plot(x, x+1, color="blue", alpha=0.5) # half-transparant
    ax.plot(x, x+2, color="#8B008B")        # RGB hex code
    ax.plot(x, x+3, color="#FF8C00")
    
     
    
  • Line and marker styles To change the line width, we can use the linewidth or lw keyword argument. The line style can be selected using the linestyle or ls keyword arguments:


  •  
    fig, ax = plt.subplots(figsize=(12,6))
    
    ax.plot(x, x+1, color="red", linewidth=0.25)
    ax.plot(x, x+2, color="red", linewidth=0.50)
    ax.plot(x, x+3, color="red", linewidth=1.00)
    ax.plot(x, x+4, color="red", linewidth=2.00)
    
    #possible linestype options ‘-‘, ‘–’, ‘-.’, ‘:’, ‘steps’
    ax.plot(x, x+5, color="green", lw=3, linestyle='-')
    ax.plot(x, x+6, color="green", lw=3, ls='-.')
    ax.plot(x, x+7, color="green", lw=3, ls=':')
    
    # custom dash
    line, = ax.plot(x, x+8, color="black", lw=1.50)
    line.set_dashes([5, 10, 15, 10]) # format: line length, space length, ...
    
    # possible marker symbols: marker = '+', 'o', '*', 's', ',', '.', '1', '2', '3', '4', ...
    ax.plot(x, x+ 9, color="blue", lw=3, ls='-', marker='+')
    ax.plot(x, x+10, color="blue", lw=3, ls='--', marker='o')
    ax.plot(x, x+11, color="blue", lw=3, ls='-', marker='s')
    ax.plot(x, x+12, color="blue", lw=3, ls='--', marker='1')
    
    # marker size and color
    ax.plot(x, x+13, color="purple", lw=1, ls='-', marker='o', markersize=2)
    ax.plot(x, x+14, color="purple", lw=1, ls='-', marker='o', markersize=4)
    ax.plot(x, x+15, color="purple", lw=1, ls='-', marker='o', markersize=8, markerfacecolor="red")
    ax.plot(x, x+16, color="purple", lw=1, ls='-', marker='s', markersize=8, 
           markerfacecolor="yellow", markeredgewidth=3, markeredgecolor="green");
     
    
  • Plot range We can configure the ranges of the axes using the set_ylim and set_xlim methods in the axis object, or axis('tight') for automatically getting "tightly fitted" axes ranges:


  •  
    fig, axes = plt.subplots(1, 3, figsize=(12, 4))
    
    axes[0].plot(x, x**2, x, x**3)
    axes[0].set_title("default axes ranges")
    
    axes[1].plot(x, x**2, x, x**3)
    axes[1].axis('tight')
    axes[1].set_title("tight axes")
    
    axes[2].plot(x, x**2, x, x**3)
    axes[2].set_ylim([0, 60])
    axes[2].set_xlim([2, 5])
    axes[2].set_title("custom axes range");
    
     
    
  • Special Plot Types There are many specialized plots we can create, such as barplots, histograms, scatter plots, and much more. Most of these type of plots we will actually create using seaborn, a statistical plotting library for Python. But here are a few examples of these type of plots:


  •  
    plt.scatter(x,y)
    
    from random import sample
    data = sample(range(1, 1000), 100)
    plt.hist(data)
    
    data = [np.random.normal(0, std, 100) for std in range(1, 4)]
    
    # rectangular box plot
    plt.boxplot(data,vert=True,patch_artist=True); 
     
    
  • # Solution


  •  
    '''
    Exercise 1.1:
    
    Plot the following using Matplotlib
    
    a(0,50,100)
    
    b(50,050)
    
    Give the title "V graph"
    Give green color to the graph
    '''
    
    ### START CODE HERE ###  (10-11 lines of code) - 
    import numpy as np 
    import matplotlib.pyplot as plt
    a=[0,50,100]
    b=[50,0,50]
    plt.plot(a, b, 'g') # 'r' is the color red
    
    plt.xlabel('X Axis Title Here')
    plt.ylabel('Y Axis Title Here')
    plt.title('String Title Here')
    plt.show()
     
    
  • ### END CODE HERE ###


  •  
    '''
    Exercise 1.1:(extention)
    
    Plot the following using Matplotlib
    
    a(0,50,100)
    
    b(50,050)
    
    Give the title "V graph"
    Give red color to the graph
    '''
    ### START CODE HERE ###  (10-11 lines of code)
    import numpy as np 
    import matplotlib.pyplot as plt
    a=[0,50,100]
    b=[50,0,50]
    plt.plot(a, b, 'r') # 'r' is the color red
    
    plt.xlabel('X Axis Title Here')
    plt.ylabel('Y Axis Title Here')
    plt.title('String Title Here')
    plt.show()
     
    
  • ### END CODE HERE ###


  •  
    '''
    Exercise 1.1:(extention)
    
    Plot the following using Matplotlib
    
    a(0,50,100)
    
    b(50,050)
    
    Give the title "L graph"
    Give blue color to a part the graph
    Give green color to b part the graph
    '''
    ### START CODE HERE ###  (10-11 lines of code)
    import numpy as np 
    import matplotlib.pyplot as plt
    a=[0,50,100]
    a1=[0,0,0]
    b=[50,0,50]
    b1=[0,0,0]
    
    plt.plot(a,a1,'b') # 'b' is the color blue
    plt.plot(b1,b,'g') # 'g' is the color grren
    
    plt.xlabel('X Axis Title Here')
    plt.ylabel('Y Axis Title Here')
    plt.title('String Title Here')
    plt.show()
     
    
  • ### END CODE HERE ###


  •  
    '''
    Exercise 1.1:(extention)
    
    create "V graph" 2 times in one graph
    '''
    ### START CODE HERE ###  (10-11 lines of code)
    import numpy as np 
    import matplotlib.pyplot as plt
    a=[0,50,100]
    b=[50,0,50]
    c=[55,0,55]
    
    plt.plot(a, b, 'r') # 'r' is the color red
    plt.plot(a,c,'b') # 'b' is the color blue
    
    plt.xlabel('X Axis Title Here')
    plt.ylabel('Y Axis Title Here')
    plt.title('String Title Here')
    plt.show()
     
    
  • ### END CODE HERE ###


  •  
    '''
    Exercise 1.2:
    
    Create a 3x3 grid of plot for :
    
    x(5,4,2,3,1) and y(4,2,1,3,5)
    '''
    
    ### START CODE HERE ###  (15-20 lines of code)
    import matplotlib.pyplot as plt
    x=[5,4,2,3,1]
    y=[4,2,1,3,5]
    
    plt.subplot(3,3,1)
    plt.plot(x, y, 'r') # More on color options later 
    
  • ### END CODE HERE ###


  •  
    '''
    Exercise 1.2(EXTRA) :
    
    Create a 3x3 grid of plot for :
    
    x(5,4,2,3,1) and y(4,2,1,3,5)
    
    AND also for
    
    x(8,4,5,3,1) and y(9,2,1,1,5)
    '''
    
    ### START CODE HERE ###  (15-20 lines of code)
    import matplotlib.pyplot as plt
    x=[5,4,2,3,1]
    y=[4,2,1,3,5]
    
    plt.subplot(3,3,1)
    plt.plot(x, y, 'r') # More on color options later
    
    x=[8,4,5,3,1]
    y=[9,2,1,1,5]
    
    plt.subplot(3,3,2)
    plt.plot(x, y, 'r') # More on color options later 
    
  • ### END CODE HERE ###


  •  
    '''
    Exercise 1.2(EXTRA) :
    
    Create a 3x3 grid of plot for :
    
    x(5,4,2,3,1) and y(4,2,1,3,5)
    
    AND also for
    
    x(8,4,5,3,1) and y(9,2,1,1,5)
    '''
    
    ### START CODE HERE ###  (15-20 lines of code)
    import matplotlib.pyplot as plt
    x=[5,4,2,3,1]
    y=[4,2,1,3,5]
    
    plt.subplot(3,3,1)
    plt.plot(x, y, 'g') # More on color options later
    
    x=[8,4,5,3,1]
    y=[9,2,1,1,5]
    
    plt.subplot(3,3,2)
    plt.plot(x, y, 'r') # More on color options later 
    
  • ### END CODE HERE ###


  •  
    '''
    Exercise 1.3:
    
    Create a 2x2 grid of plots using object oriented approach for :
    
    x(2,3,1) and y(2,1,3)
    '''
    
    ### START CODE HERE ### (6-7 lines of code)
    import matplotlib.pyplot as plt
    
    fig, axes = plt.subplots(nrows=1, ncols=2)
    x=[2,3,1]
    y=[2,1,3]
    for ax in axes:
        ax.plot(x, y, 'g')
        ax.set_xlabel('x')
        ax.set_ylabel('y')
        ax.set_title('title')
    
    fig    
    plt.tight_layout() 
    
  • ### END CODE HERE ###


  •  
    '''
    Exercise 1.6:
    
    Write a Python program to plot two or more lines with different styles.
    '''
    
    ### START CODE HERE ###    (8-10 lines of code)
    
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0, 5, 11)
    y = x ** 2
    
    fig, ax = plt.subplots(figsize=(12,6))
    
    ax.plot(x, x+5, color="black", lw=3, linestyle='-')
    ax.plot(x, x+6, color="green", lw=3, ls='-.')
    ax.plot(x, x+7, color="red", lw=3, ls=':')
     
    
  • ### END CODE HERE ###


  •  
    '''
    Exercise 1.7:
    
    Write a Python program to plot two or more lines and set different line markers.
    '''
    
    ### START CODE HERE ### (8-10 lines of code )
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0, 5, 11)
    y = x ** 2
    
    fig, ax = plt.subplots(figsize=(12,6))
    
    
    ax.plot(x, x, color="red", lw=1, ls='-', marker='o', markersize=2)
    ax.plot(x, x+2, color="blue", lw=1, ls='-', marker='o', markersize=4)
    ax.plot(x, x+4, color="green", lw=1, ls='-', marker='o', markersize=8, markerfacecolor="red")
    ax.plot(x, x+6, color="black", lw=1, ls='-', marker='s', markersize=8,markerfacecolor="yellow", markeredgewidth=3, markeredgecolor="green"); 
    
     
    
  • ## Further reading


  • * http://www.matplotlib.org - The project web page for matplotlib.


  • * https://github.com/matplotlib/matplotlib - The source code for matplotlib.


  • * http://matplotlib.org/gallery.html - A large gallery showcaseing various types of plots matplotlib can create. Highly recommended!


  • * http://www.loria.fr/~rougier/teaching/matplotlib - A good matplotlib tutorial.


  • * http://scipy-lectures.github.io/matplotlib/matplotlib.html - Another good matplotlib reference.