📖 Scenario: You are working with sales data for three products over four months. You want to visualize this data using bar charts to compare sales.
🎯 Goal: Build two bar charts: one using figure-level methods and one using axes-level methods in matplotlib. Understand the difference between these two approaches.
📋 What You'll Learn
Create a dictionary called sales_data with product names as keys and lists of monthly sales as values
Create a list called months with the names of the four months
Create a figure and axes using plt.subplots()
Use axes-level method ax.bar() to plot sales for one product
Use figure-level method plt.bar() to plot sales for another product
Add titles and labels to both plots
Print the figure and axes objects to see their types
💡 Why This Matters
🌍 Real World
Data scientists often need to create clear and informative charts. Knowing when to use figure-level or axes-level methods helps in customizing plots effectively.
💼 Career
Understanding matplotlib's figure and axes concepts is essential for data visualization tasks in data science and analytics jobs.
Progress0 / 4 steps
1
Create the sales data and months list
Create a dictionary called sales_data with these exact entries: 'Product A': [10, 15, 7, 12], 'Product B': [5, 8, 12, 9], and 'Product C': [12, 9, 11, 14]. Also create a list called months with these exact values: 'Jan', 'Feb', 'Mar', 'Apr'.
Matplotlib
Hint
Use curly braces {} to create the dictionary and square brackets [] for the lists.
2
Create a figure and axes with plt.subplots()
Import matplotlib.pyplot as plt. Then create a figure and axes by calling plt.subplots() and assign them to variables fig and ax.
Matplotlib
Hint
Use import matplotlib.pyplot as plt and then fig, ax = plt.subplots().
3
Plot sales for Product A using axes-level method and Product B using figure-level method
Use the axes-level method ax.bar() to plot sales for 'Product A' with months on the x-axis and sales on the y-axis. Then use the figure-level method plt.bar() to plot sales for 'Product B' with the same x-axis. Add titles to both plots using ax.set_title() and plt.title() respectively.
Matplotlib
Hint
Use ax.bar() for Product A and plt.bar() for Product B. Add titles with ax.set_title() and plt.title().
4
Print the figure and axes objects and show the plots
Print the variables fig and ax to see their types. Then call plt.show() to display the plots.
Matplotlib
Hint
Use print(fig) and print(ax) to see the figure and axes objects. Use plt.show() to display the plots.
Practice
(1/5)
1. Which method would you use to set the main title for the entire figure in matplotlib?
easy
A. plt.title()
B. ax.set_title()
C. ax.set_xlabel()
D. fig.suptitle()
Solution
Step 1: Understand figure-level vs axes-level
Figure-level methods control the whole figure, axes-level methods control individual plots.
Step 2: Identify method for main figure title
The method fig.suptitle() sets the main title for the entire figure, while ax.set_title() sets title for one subplot.
Final Answer:
fig.suptitle() -> Option D
Quick Check:
Figure main title = fig.suptitle() [OK]
Hint: Main figure title uses fig.suptitle(), not ax.set_title() [OK]
Common Mistakes:
Using ax.set_title() for main figure title
Using plt.title() which affects current axes
Confusing xlabel with title
2. Which of the following is the correct syntax to set the x-axis label for a single plot (axes) in matplotlib?
easy
A. plt.set_xlabel('X Label')
B. fig.set_xlabel('X Label')
C. ax.set_xlabel('X Label')
D. fig.xlabel('X Label')
Solution
Step 1: Identify the object controlling axis labels
Axis labels belong to individual plots, so axes-level methods are used.
Step 2: Choose correct method and object
The method set_xlabel() is called on an axes object, so ax.set_xlabel('X Label') is correct.
Final Answer:
ax.set_xlabel('X Label') -> Option C
Quick Check:
Axis label = ax.set_xlabel() [OK]
Hint: Use ax.set_xlabel() for x-axis label, not fig or plt [OK]
A. fig.set_title() is not a valid method for figure objects
B. ax.set_xlabel() should be called on fig, not ax
C. plt.show() must be called before setting titles
D. fig, ax = plt.subplots() returns only one object
Solution
Step 1: Check figure method usage
The figure object does not have a method called set_title(). The correct method for figure title is suptitle().
Step 2: Verify other method calls
ax.set_xlabel() is correct for labeling x-axis. plt.show() is correctly placed at the end.
Final Answer:
fig.set_title() is not a valid method for figure objects -> Option A
Quick Check:
Figure title method = fig.suptitle() not fig.set_title() [OK]
Hint: Figure title uses suptitle(), not set_title() [OK]
Common Mistakes:
Using fig.set_title() instead of fig.suptitle()
Confusing axes methods with figure methods
Calling plt.show() too early
5. You want to create a figure with 2 subplots side by side. You want a main title for the figure and individual titles for each subplot. Which code correctly achieves this?