Figure versus Axes
A Figure is the whole canvas. An Axes is one plotting area inside that canvas, with its own x-axis, y-axis, data, title, and legend. A figure can contain one axes or many; most first charts use one.
- Figure: size, layout, and the space that holds axes.
- Axes: the coordinate system where data is drawn.
- Axis: the x or y scale, ticks, and labels belonging to an axes.
Your first chart
Pass x values and y values to ax.plot. Matplotlib connects the points in order, so the observation order matters. Here the week number is on the x-axis and study hours are on the y-axis.
Python
A titled, labelled line chart
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
This runner captures open figures after the code finishes, so the chart appears below the output. In a regular Python script you would finish with plt.show().
Labels are part of the chart
A title states the question. Axis labels name the units. Without them a reader has to infer what the numbers mean, and a chart that needs a paragraph of explanation is not finished.
Python
A fixed y range makes comparisons honest
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
Size and layout
figsize is measured in inches before rendering. tight_layout adjusts spacing so labels do not collide with the figure edge. Neither changes the data; they change whether the chart is legible.
Python
A figure sized for its labels
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
The next lesson compares series on one axes and uses bars for categorical totals. The same figure-and-axes pattern stays in place.
Practice
Make it stick
Run the tests as often as you like. Hints and the solution are available when you need them.
Exercise
Create fig and ax with plt.subplots(figsize=(7, 4)). Plot weeks against hours with marker "o", then set the title to "Study hours by week", xlabel to "Week", and ylabel to "Hours".
Your solution
ReadyResults
Run the tests to check your solution. The first run downloads Python; later runs reuse the session.
Quiz
Check your understanding
Lesson progress