PY-401 ยท Visualisation
matplotlib basics
โฑ 9 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก 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.
import matplotlib.pyplot as pltmonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]sales = [120, 135, 150, 98, 160, 175]fig, ax = plt.subplots(figsize=(6, 3))ax.plot(months, sales, marker="o")ax.set_title("Sales dipped in April, then recovered")ax.set_xlabel("Month")ax.set_ylabel("Sales (units)")worst = months[sales.index(min(sales))]print("Lowest month:", worst)
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
Lowest month: Apr ๐ฏ Draw a bar chart of visits per day on axes ax, with the title "Visits per day".
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
ax.bar(day, visits) then ax.set_title("Visits per day")
- 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.
- The Python idea
import matplotlib.pyplot as pltmatplotlib is the standard Python library for charts. - 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("...") 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.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Charts straight from pandas โ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
- matplotlib quick start guide (docs.python.org)