0
0
Matplotlibdata~30 mins

Why the OO interface matters in Matplotlib - See It in Action

Choose your learning style9 modes available
Why the OO interface matters
📖 Scenario: You are working on a small project to visualize sales data for a local store. You want to create a clear and neat line chart showing sales over a week. Using the Object-Oriented (OO) interface of matplotlib helps you control the plot better and avoid confusion when you add more plots later.
🎯 Goal: Build a simple line chart using matplotlib's OO interface to plot sales data over 7 days. Learn how to create a figure and axes, plot data on the axes, and label the chart properly.
📋 What You'll Learn
Create a list called sales with exact values for 7 days
Create a list called days with exact day names
Create a figure and axes using plt.subplots()
Plot the sales data on the axes using ax.plot()
Set the title of the plot using ax.set_title()
Label the x-axis and y-axis using ax.set_xlabel() and ax.set_ylabel()
Display the plot using plt.show()
💡 Why This Matters
🌍 Real World
Visualizing sales data helps store owners understand trends and make better decisions.
💼 Career
Data scientists and analysts often use matplotlib's OO interface to create clear, customizable charts for reports and presentations.
Progress0 / 4 steps
1
Create the sales and days data
Create a list called sales with these exact values: [150, 200, 170, 220, 180, 210, 190]. Also create a list called days with these exact strings: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"].
Matplotlib
Need a hint?

Use square brackets to create lists. Make sure the values and strings match exactly.

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

Use import matplotlib.pyplot as plt to import. Then call plt.subplots() to get figure and axes.

3
Plot the sales data on the axes
Use ax.plot() to plot days on the x-axis and sales on the y-axis.
Matplotlib
Need a hint?

Use ax.plot(x, y) where x is days and y is sales.

4
Add labels, title, and show the plot
Use ax.set_title() to set the title to "Weekly Sales". Use ax.set_xlabel() to label the x-axis as "Day". Use ax.set_ylabel() to label the y-axis as "Sales". Finally, display the plot using plt.show().
Matplotlib
Need a hint?

Use ax.set_title(), ax.set_xlabel(), and ax.set_ylabel() to add labels. Use plt.show() to display the plot.