Lesson 19 of 23
โฎPrev Nextโฏ

PY-401 ยท Visualisation

matplotlib basics

โฑ 9 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

19of 23

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    matplotlib is the standard Python library for charts. You create a figure and axes with fig, ax = plt.subplots(), draw on the axes with ax.plot(x, y) or ax.bar(x, y), and label it with ax.set_title, ax.set_xlabel and ax.set_ylabel. pandas, seaborn and most Python chart tools draw with matplotlib underneath.

    • Create a figure and axes
    • Draw a line and a bar chart
    • Add a title and axis labels

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    matplotlib basics, running
    1. import matplotlib.pyplot as plt
    2. months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
    3. sales = [120, 135, 150, 98, 160, 175]
    4. fig, ax = plt.subplots(figsize=(6, 3))
    5. ax.plot(months, sales, marker="o")
    6. ax.set_title("Sales dipped in April, then recovered")
    7. ax.set_xlabel("Month")
    8. ax.set_ylabel("Sales (units)")
    9. worst = months[sales.index(min(sales))]
    10. print("Lowest month:", worst)
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemSix months of sales in a table look flat. Put the same numbers on a line chart and the April dip jumps out before anyone reads a single number.
    2. The Python ideaimport matplotlib.pyplot as pltmatplotlib is the standard Python library for charts.
    3. In real lifeA website team plots daily visitors every morning. When a line suddenly drops to zero, they know the tracking broke before any report is late.

    Start with a question

    Six months of sales in a table look flat. Put the same numbers on a line chart and the April dip jumps out before anyone reads a single number.

    People understand pictures faster than tables. A clear chart is often the whole point of an analysis.

    Explain it like I'm 10

    The figure is the sheet of paper, the axes are the box you draw the chart in, and plot is your pencil joining the dots.

    The technical explanation

    Import it as import matplotlib.pyplot as plt, the name every example uses.

    fig, ax = plt.subplots() gives you a figure (the whole image) and one axes (the chart area). You call drawing methods on ax.

    ax.plot(x, y, marker="o") draws a line with dots, ax.bar(x, y) draws bars. Both take lists, arrays or pandas columns.

    In this course the chart appears under the output when you press Run. On your computer, plt.show() opens a window and fig.savefig("chart.png") saves a file.

    Syntax

    import matplotlib.pyplot as plt
    fig, ax = plt.subplots()
    ax.plot(x, y, marker="o")
    ax.set_title("..."); ax.set_xlabel("..."); ax.set_ylabel("...")

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    A website team plots daily visitors every morning. When a line suddenly drops to zero, they know the tracking broke before any report is late.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. How is pyplot usually imported?
    2. 2. What does plt.subplots() return?
    3. 3. Which draws bars?
    4. 4. Which saves the chart as a file?
    5. 5. What makes a good chart title?

    Questions people ask

    What is the difference between plt.plot and ax.plot?

    plt.plot draws on the current axes that pyplot tracks for you. ax.plot draws on a specific axes you created with plt.subplots, which is clearer and works better when a figure has several charts.

    Why does my matplotlib chart not show?

    In a script you need plt.show() to open the window, or fig.savefig to write a file. Notebooks and this course display the figure for you.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner What is the difference between plt.plot and ax.plot?

    plt.plot draws on the current axes that pyplot tracks for you. ax.plot draws on a specific axes you created with plt.subplots, which is clearer and works better when a figure has several charts.

    beginner Why does my matplotlib chart not show?

    In a script you need plt.show() to open the window, or fig.savefig to write a file. Notebooks and this course display the figure for you.

    intermediate How is pyplot usually imported?

    import matplotlib.pyplot as plt. plt is the shared convention.

    intermediate What does plt.subplots() return?

    A figure and an axes. fig is the image, ax is the chart area you draw on.

    Quick revision

    Remember

    • Create a figure and axes
    • Draw a line and a bar chart
    • Add a title and axis labels
    • Always label the axes and give the chart a title that says the answer, such as "Sales dipped in April", not just "Sales".

    Syntax

    import matplotlib.pyplot as plt
    fig, ax = plt.subplots()
    ax.plot(x, y, marker="o")
    ax.set_title("..."); ax.set_xlabel("..."); ax.set_ylabel("...")

    Official documentation