This code creates a figure with one plot area, draws a line graph, adds labels and a title, then shows the figure window.
Matplotlib
import matplotlib.pyplot as plt
# Create a figure and axes
fig, ax = plt.subplots()
# Plot some data
ax.plot([0, 1, 2, 3], [10, 20, 25, 30], label='Line')
# Add title and labels
ax.set_title('Simple Line Plot')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
# Show legend
ax.legend()
# Display the figure
plt.show()
OutputSuccess
Important Notes
Matplotlib first creates a Figure object which is like a blank canvas.
Inside the figure, Axes objects are created where data is drawn.
When you call plt.show(), Matplotlib renders the figure on your screen.
Summary
Matplotlib draws figures by creating a Figure and one or more Axes inside it.
You add data to Axes, then call plt.show() to see the graph.
Understanding this helps you control how your charts look and behave.
Practice
(1/5)
1. What is the first step Matplotlib takes when creating a plot?
easy
A. Add data points directly to the screen
B. Display the plot with plt.show()
C. Save the plot to a file automatically
D. Create a figure and axes to draw on
Solution
Step 1: Understand Matplotlib's drawing process
Matplotlib starts by creating a figure and axes, which are the canvas and area to draw on.
Step 2: Identify the first action in plotting
Before adding data or showing the plot, the figure and axes must exist to hold the plot.
Final Answer:
Create a figure and axes to draw on -> Option D
Quick Check:
First step = create figure and axes [OK]
Hint: Remember: figure and axes come before plotting data [OK]
Common Mistakes:
Thinking plt.show() creates the plot
Assuming data is drawn directly without axes
Believing plot saves automatically without show
2. Which of the following is the correct way to display a Matplotlib figure?
easy
A. plt.show()
B. plt.draw()
C. plt.display()
D. plt.render()
Solution
Step 1: Recall the function to display plots
Matplotlib uses plt.show() to open a window and display the figure.
Step 2: Check other options for correctness
plt.draw() updates the figure but does not display it; plt.display() and plt.render() do not exist in Matplotlib.
Final Answer:
plt.show() -> Option A
Quick Check:
Display function = plt.show() [OK]
Hint: Use plt.show() to see your plot on screen [OK]
Common Mistakes:
Using plt.draw() instead of plt.show()
Trying plt.display() which is not a Matplotlib function