0
0
Matplotlibdata~20 mins

Fig, ax = plt.subplots pattern in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the Fig, ax = plt.subplots Pattern in Matplotlib
📖 Scenario: You are a data analyst who wants to create a simple line chart to show sales over a week. You will use the fig, ax = plt.subplots() pattern to create the plot.
🎯 Goal: Build a line plot using fig, ax = plt.subplots() to display sales data for 7 days.
📋 What You'll Learn
Create a list called days with the values: 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'.
Create a list called sales with the values: 150, 200, 170, 220, 180, 190, 210.
Use fig, ax = plt.subplots() to create the figure and axes.
Plot the sales data on the axes with days on the x-axis and sales on the y-axis.
Set the title of the plot to 'Weekly Sales'.
Display the plot using plt.show().
💡 Why This Matters
🌍 Real World
Creating clear and simple charts is important for sharing data insights with others in business, science, and everyday life.
💼 Career
Data analysts and scientists use matplotlib and the fig, ax = plt.subplots() pattern to build flexible and professional plots for reports and presentations.
Progress0 / 4 steps
1
Create the sales data lists
Create a list called days with the values 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'. Also create a list called sales with the values 150, 200, 170, 220, 180, 190, 210.
Matplotlib
Need a hint?

Use square brackets to create lists. Separate each day and sales number with commas.

2
Create the figure and axes using plt.subplots()
Import matplotlib.pyplot as plt. Then create the figure and axes by writing fig, ax = plt.subplots().
Matplotlib
Need a hint?

Use import matplotlib.pyplot as plt to import. Then call plt.subplots() to get fig and ax.

3
Plot the sales data on the axes
Use the ax.plot() method to plot days on the x-axis and sales on the y-axis. Then set the plot title to 'Weekly Sales' using ax.set_title().
Matplotlib
Need a hint?

Use ax.plot(x, y) to draw the line. Use ax.set_title('Weekly Sales') to add the title.

4
Display the plot
Use plt.show() to display the plot on the screen.
Matplotlib
Need a hint?

Call plt.show() to open the plot window.