Sometimes, it can be helpful to zoom in or out of the plot, especially if there is some detail we want to address. To zoom, we can use
plt.axis(). We use
plt.axis() by feeding it a list as input. This list should contain:
- The minimum x-value displayed
- The maximum x-value displayed
- The minimum y-value displayed
- The maximum y-value displayed
For example, if we want to display a plot from
x=3 and from
y=5, we would call
plt.axis([0, 3, 2, 5]).
x = [0, 1, 2, 3, 4] y = [0, 1, 4, 9, 16] plt.plot(x, y) plt.axis([0, 3, 2, 5]) plt.show()
We have plotted a line representing someone’s spending on coffee over the past 12 years.
Run the code to see the resulting plot.
Let’s modify the axes to zoom in a bit more on our line chart. Use
plt.axis() to modify the axes so that the x-axis goes from 0 to 12, and the y-axis goes from 2900 to 3100.