Learn
Line Graphs in Matplotlib
Axis and Labels

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:

  1. The minimum x-value displayed
  2. The maximum x-value displayed
  3. The minimum y-value displayed
  4. The maximum y-value displayed

For example, if we want to display a plot from x=0 to x=3 and from y=2 to 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()

axis_zoom

Instructions

1.

We have plotted a line representing someone’s spending on coffee over the past 12 years.

Run the code to see the resulting plot.

2.

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.

Folder Icon

Sign up to start coding

Already have an account?