<Python> Matplotlib (Demo)
  • AI Chat
  • Code
  • Report
  • Beta
    Spinner

    Line chart

    # import packages
    import matplotlib.pyplot as plt
    import numpy as np
    %matplotlib inline
    %config InlineBackend.figure_format = 'retina'
    # Data
    x = range(2014, 2021)
    y = [20, 25, 18, 15, 24, 30, 26]
    
    # Graph
    plt.plot(x[:4], y[:4], marker="o", label="Actual")
    plt.plot(x[3:], y[3:], linestyle="--", label="Forcast")
    
    # Aesthetic
    plt.title("zzz", size=14)
    plt.ylabel("yyy")
    plt.xlabel("xxx")
    plt.legend()
    plt.show()

    Bar chart - Horizontal

    # Data
    x = range(4)
    y = (20, 25, 24, 30)
    xtick = ("mocha", "latte", "espresso", "tea")
    avg = sum(y)/len(y)
    
    # Graph
    plt.bar(x, y)
    plt.axhline(avg, linestyle="--", alpha=0.5)
    
    # Aesthetic
    plt.title("Order by menu\nFeb 2017", size=14)
    plt.xlabel("Menu")
    plt.xticks(x, xtick)
    plt.ylabel("Order")
    plt.show()

    Bar chart - Vertical

    # Data
    x = range(4)
    y = (20, 25, 24, 30)
    xtick = ("mocha", "latte", "espresso", "tea")
    
    # Graph
    plt.barh(x, y)
    
    # Aesthetic
    plt.title("Order by menu\nFeb 2017", size=14)
    plt.ylabel("Menu")
    plt.xlabel("Order")
    plt.yticks(x, xtick)
    plt.show()

    Pie chart

    # Data
    label = np.array(["China", "Russia", "Japan", "Korea"])
    val = (1.8, 1, .8, .5)
    explode = np.zeros(label.size)
    explode[np.where(label == "Japan")] = .1
    colors = ["red", "yellow", "pink", "orange"]
    
    # Graph
    plt.pie(val, labels=label, startangle=90, autopct="%1.2f%%", explode=explode, colors=colors)
    
    # Aesthetic
    plt.axis("equal")
    plt.title("GDP per capita for each country", size=14)
    plt.show()

    Combo chart

    # Data
    labels = np.array(["Jan", "Feb", "Mar", "Apr", "Jun"])
    x = np.arange(labels.size)
    y1 = np.random.normal(80, 10, labels.size) #(mean, std, sample size)
    y2 = np.random.normal(95, 12, labels.size)
    
    # Graph
    plt.bar(x, y1, color="deepskyblue", alpha=.2, label="We")
    plt.plot(x, y2, color="orange", linestyle="--", marker="o", label="Industry")
    
    # Aesthetic
    plt.title("Total Sales", size=14)
    plt.ylabel("Sales amount")
    plt.xticks(x, labels)
    plt.legend()
    plt.show()

    Combo chart - Secondary axis

    # Data
    labels = np.array(["Jan", "Feb", "Mar", "Apr", "May", "Jun"])
    x = np.arange(labels.size)
    y1 = np.random.normal(800, 90, labels.size)
    y2 = np.random.normal(80, 10, labels.size)
    
    # Graph
    fig, ax1 = plt.subplots()
    ax1.bar(x, y1, color="purple", alpha=.2)
    ax1.set_xticks(x)
    ax1.set_xticklabels(labels)
    ax1.tick_params("y", colors="purple")
    ax1.set_ylabel("Sales", color="purple", size=12)
    
    ax2 = ax1.twinx()
    ax2.plot(x, y2, color="orange", linestyle="--", marker="o")
    ax2.tick_params("y", colors="orange")
    ax2.set_ylabel("Profit", color="orange", size=12)
    ax2.set_ylim(ymin=0)
    
    # Aesthetic
    plt.title("Sales VS Profit\n(Jan - Jun, 2017)", size=14)
    plt.show()

    Quality Control chart

    # Data
    n = 100
    x = np.arange(n)
    d = np.random.normal(150, 5, n)
    m, sd = np.mean(d), np.std(d)
    ucl = 2 * sd
    lcl = -ucl
    
    # Graph
    plt.figure(figsize=(15,10))
    plt.plot(x, d, marker="o", color=".7", alpha=.7)
    
    # Line
    plt.axhline(m, linestyle="--", color="green", alpha=.5) ## Average line
    plt.axhline(m+ucl, linestyle="--", color="red", alpha=.5) ## Upper control line
    plt.axhline(m+lcl, linestyle="--", color="red", alpha=.5) ## Lower control line
    filter = np.where((d > m + 2*sd) | (d < m - 2*sd))
    plt.plot(x[filter], d[filter], marker="o", linestyle="", color="red")
    plt.fill_between(x, m + ucl, m + 5*sd, color="red", alpha=.1) ## Upper control area
    plt.fill_between(x, m + lcl, m - 5*sd, color="red", alpha=.1) ## Lower control area
    
    #Aesthetic
    ### t = "n = {}, mean = {:.2f}, sd = {:.2f}".format(n, m, sd)
    t = "n = {}, {} = {:.2f}, sd = {:.2f}".format(n, r"$\bar{x}$", m, sd)
    plt.title(t, size=14)
    plt.ylabel("weight (grams)")
    plt.show()