Consider a matplotlib plot with the default toolbar enabled. What is the immediate effect of clicking the zoom button?
import matplotlib.pyplot as plt plt.plot([1, 2, 3, 4], [10, 20, 25, 30]) plt.show()
Think about what zooming usually means in a graph interface.
Clicking the zoom button activates a mode where you can select a rectangular area on the plot to zoom into that specific region.
Given a plot with x-axis limits from 0 to 10, if you pan the plot right by 2 units using the toolbar, what will be the new x-axis limits?
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.set_xlim(0, 10) ax.set_ylim(0, 5) # Assume user pans right by 2 units new_xlim = (2, 12) print(new_xlim)
Panning right shifts the view window to higher x values.
Panning right by 2 units moves both limits 2 units higher, so from (0,10) to (2,12).
Look at the two plots below. The first plot shows the original y-axis limits from 0 to 100. The second plot is after zooming into the y-range 20 to 40. What is the new y-axis limit range?
import matplotlib.pyplot as plt fig, axs = plt.subplots(1, 2, figsize=(8, 4)) axs[0].plot(range(101), range(101)) axs[0].set_ylim(0, 100) axs[0].set_title('Original') axs[1].plot(range(101), range(101)) axs[1].set_ylim(20, 40) axs[1].set_title('Zoomed') plt.show()
Zooming changes the visible range to a smaller subset.
Zooming into y-range 20 to 40 sets the y-axis limits exactly to (20, 40), focusing on that part of the data.
Consider this code snippet to pan a matplotlib plot by changing x-axis limits. Why does the plot not update after running this?
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([1,2,3,4], [10,20,25,30]) ax.set_xlim(0, 10) ax.set_xlim(ax.get_xlim()[0] + 2, ax.get_xlim()[1] + 2) # Missing plt.show() or canvas draw update
Think about what is needed to update a plot after changing properties programmatically.
Changing axis limits alone does not refresh the plot display. You must call plt.show() again or use fig.canvas.draw() to update the view.
You want to programmatically zoom into the rectangle defined by x from 2 to 5 and y from 10 to 20 on an existing matplotlib plot. Which code snippet correctly achieves this?
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot(range(10), [i*3 for i in range(10)])
Zooming programmatically means setting axis limits to the desired rectangle.
Setting x and y axis limits to the rectangle coordinates zooms into that area. plt.draw() refreshes the plot.