While working on Matplotlib, we can change the axes size of its output plots. Matplotlib provides us with specific functions to modify individual axes values. So we can write Python programs to modify these axes size.

In our previous tutorial, we created a simple Matplotlib plot of multiple lines along with gridlines. However, in that plot we can see tht the size of each of the two axis where auto-determined. Since we used x & y values ranging between 1-10 & 0-30 respectively, axis size was also so to the same range.

However, we can actually change this. We can use Matplotlib to change axes size by making use of its appropriate features.

## Understanding How Matplotlib Changes Axes Size

So let us go back to our previous plot, which looked like this:

The code we used to generate the above chart looked like this:

```
import matplotlib.pyplot as plt
x = range(1, 10)
plt.plot(x, [xi*1 for xi in x])
plt.plot(x, [xi*2 for xi in x])
plt.plot(x, [xi*3 for xi in x])
plt.grid()
plt.show()
```

As mentioned earlier, we can see from the above code that x-axis values ranges between 1 & 10. On the other hand, values of y-axis is determined by the 3 lines we plotted on the graph. Their values where calculated by multiplying the values of x by 3 different values – 1, 2 & 3.

So by analyzing this, we can see that the highest y value achieved is from line number three. But line is being drawn using the code:

`plt.plot(x, [xi*3 for xi in x])`

So, we can see that the highest value of y it can achieve is when we multiply the highest value of x with 3. But x ranges between 1 & 10. So the highest value that y can achieve is:

```
yi = xi*3 where xi=9 (because it is less than 10)
yi = 9 * 3
yi = 27
```

Hence, the highest value of y is 27. Our graph is also confirming this. We can see that the value of y axis of our 3rd line is not going beyond 27.

So with this knowledge, Matplotlib is drawing the x-axis of the plot to be up to 10. But on the other hand, it is stretching the y-axis to 30. It did this to accommodate the highest y-axis value of 27 of our 3rd plot.

## Programming Matplotlib To Change Axes Size

So now that we understand how Matplotlib calculates the axes values automatically, we will now learn how we can change this. In order for us to achieve this, we will use yet another function of Matplotlib.

### Matplotlib Axis Function

In order to control the size of our plot axes, Matplotlib provides us with another function called the *axis* function. The signature of this function looks like this:

```
matplotlib.pyplot.axis(*args, **kwargs)
where args = [xmin, xmax, ymin, ymax]
```

From the above signature, we can see that we can set the minimum and maximum values of x & y axis using *xmin, xmax, ymin and ymax*. You should also keep in mind that **we need to pass these parameters as a Python list variable**.

However this is not it. There is one another interesting feature of *axis*(). It is that if we simply call it without passing any parameters, it will return the current values of xmin, xmax, ymin ymax!

So *axis()* acts like both a GET function and a POST function.

One more thing to keep in mind while using *axis()* is that we need to call it before calling our *plt.show()*.

Now enough of the theory behind this function. Let us understand it better by exploring it with our example plot.

## Code For Matplotlib Change Axes Size

So we will now modify our code to include *axis()* function call as follows:

```
import matplotlib.pyplot as plt
x = range(1, 10)
plt.plot(x, [xi*1 for xi in x])
plt.plot(x, [xi*2 for xi in x])
plt.plot(x, [xi*3 for xi in x])
plt.grid()
plt.axis()
plt.show()
```

When we run this program, what we get is the current size of the axes of our plot:

```
plt.axis()
(0.59999999999999998, 9.4000000000000004, -0.30000000000000004, 28.300000000000001)
```

So the above code returned us with the current size of our plot. Let us now modify this code further so that it can change the size of our plot axes values. To do this, let us modify our code like this:

```
import matplotlib.pyplot as plt
x = range(1, 10)
plt.plot(x, [xi*1 for xi in x])
plt.plot(x, [xi*2 for xi in x])
plt.plot(x, [xi*3 for xi in x])
plt.grid()
plt.axis([0, 20, 0 , 40])
plt.show()
```

By adding the parameters (0, 20, 0, 40) to our plot axis function, we have increased the size of both our axes. So the x-axis is extended to 20(xmax=20) while the y-axis is extended to 40 (ymax=40).

When we now run this program again, we will finally get this Matplotlib output plot:

From the above plot, we can clearly see that the x-axis is increased upto 20 while the y-axis of the plot is increased to 40. So this is how we can use the *axis()* provided by Matplotlib to change xxes size of our output graph plot.

I hope this tutorial was helpful to you. If you still have any questions about it, do let me know in the comments below. So until next time, ciao! ðŸ™‚

## One reply on “Matplotlib Change Axes Size In Python”

[…] labels to a Matplotlib graph plot. So to do this, we will use the same plot we had got from our previous article. As this plot already has lines drawn along x and y axis, we will now add labels to its […]