Online-Academy
Look, Read, Understand, Apply

MatPlotLib

import matplotlib.pyplot as plt
import numpy as np
if __name__ == '__main__':
    xps = np.array([0, 8])
    yps = np.array([0, 150])
    plt.plot(xps, yps)
    plt.show()

    xps = np.array([1, 10])
    yps = np.array([3, 15])
    #To plot only the markers, you can use shortcut string notation parameter 'o', which means 'rings'.
    # We can plot chart only with the markers, we have to use third parameter, a string notion 'x' or some other character.
    plt.plot(xps, yps, 'x')
    plt.show()

    xps = np.array([1, 2, 6, 8])
    yps = np.array([3, 8, 1, 10])
    plt.plot(xps, yps)
    plt.show()

    yps = np.array([5, 8, 4, 10, 9, 17])
    # We can draw chart with y-coordinates only. Default values for x-coordinates will be automatically assumed.
    plt.plot(yps)
    plt.show()

    yps = np.array([3, 8, 1, 10])
    # Specifying linestyle (or ls can be also used)
    plt.plot(yps, linestyle='dotted')
    plt.show()
    # Specifying marker size (ms = 14), marker as character 'x'
    plt.plot(yps, marker='x', ms=14)
    plt.show()
    #Specifying marker size, and color (marker face color (mfc = 'r'
    plt.plot(yps, marker='o', ms=15, mfc='r')
    plt.show()

    # Histogram shows frequency distributions, hist() function is ued to make histogram, it takes an array of numbers.
    x = np.random.normal(100, 8, 200)  #generating random numbers 200 concentrating around 100 with standard deviation 8
    plt.hist(x)  # creating histogram
    plt.show()

    x = np.array([82, 78, 98, 95, 110, 155, 190, 215, 220, 225])
    y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
    plt.title("Run scored and balls faced")  #specifying title of chart
    plt.xlabel("Average runs")      # Specifying label for x-axis
    plt.ylabel("Average balls faced")  # Specifying label for y-axis
    plt.plot(x, y)

    f1 = {'family': 'serif', 'color': 'red', 'size': 22}
    f2 = {'family': 'serif', 'color': 'blue', 'size': 16}

    plt.title("Sports Watch Data", fontdict=f1) # setting font properties for the title and labels. We can use loc properties to specify location of title (loc='left')
    plt.xlabel("Average Pulse", fontdict=f2)
    plt.ylabel("Calorie Burnage", fontdict=f2)
    plt.plot(x, y)
    plt.show()

    plt.grid() #creating grid chard
    plt.show()

    plt.title("Run scored and balls faced")
    plt.xlabel("Average runs")
    plt.ylabel("Average balls faced")
    plt.plot(x, y)
    plt.grid(axis='x')
    plt.show()

    plt.title("Run scored and balls faced")
    plt.xlabel("Average runs")
    plt.ylabel("Average balls faced")
    plt.plot(x, y)
    plt.grid(color='green', linestyle='--', linewidth=0.5)
    plt.show()

    x = np.array([0, 1, 2, 3])
    y = np.array([3, 8, 1, 10])
    # Drawing multiple plots in one figure: We use subplot fucntion to create multiple plots in one figure.
    plt.subplot(1, 2, 1)  # subplot function takes three arguments: row, column and index of the current plot.
    plt.plot(x, y)

    # plot 2:
    x = np.array([0, 1, 2, 3])
    y = np.array([10, 20, 30, 40])

    plt.subplot(1, 2, 2)
    plt.plot(x, y)
    plt.show()

    # Creating Scatter plot
    x = np.array([2,4,5,6,9,12,12,9,4,10,11,8,6])
    y = np.array([94,84,83,78,101,80,109,81,84,77,78,89,86])
    plt.scatter(x, y)
    plt.show()

    # Creating two scatter plots for comparison
    # first data
    x = np.array([2, 4, 5, 6, 9, 12, 12, 9, 4, 10, 11, 8, 6])
    y = np.array([94, 84, 83, 78, 101, 80, 109, 81, 84, 77, 78, 89, 86])
    plt.scatter(x, y)

    # Second data
    x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
    y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])
    plt.scatter(x, y)
    plt.show()

    # Creating Bar Chart
    x = np.array(["U","W", "X", "Y", "Z"])
    y = np.array([7, 3, 8, 1, 10])
    plt.bar(x, y)
    plt.show()

    # Creating Horizontal Bar chart
    x = np.array(["U", "W", "X", "Y", "Z"])
    y = np.array([7, 3, 8, 1, 10])
    plt.barh(x, y)
    plt.show()
    plt.bar(x, y, color="red") #specifying color for bars
    plt.show()
    plt.bar(x, y, width=0.2) #specifying with of bars
    plt.show()
    plt.barh(x, y, height=0.1) # Specifying height of horizontal bars
    plt.show()

    #Creating Pie Chart
    y = np.array([33, 21, 55, 25])
    plt.pie(y)
    plt.show()
    mylabels = ["Balls", "Bats", "ShuttleCorks", "Rackets"]
    plt.pie(y, labels=mylabels) # Specifying labels for pie chart
    plt.show()

    plt.pie(y, labels=mylabels, startangle=80) # specifying start angle
    plt.show()