Matplotlib

How Matplotlib Plot Autoscale works in Python ? Only 2 Steps

We can enable the matplotlib plot autoscale feature using autoscale() of matplotlib.pyplot. It accepts enable, axis and tight as parameters. In this article, We will see this implementation with an example.

 

Matplotlib plot autoscale –

We will see this autoscaling with an example. We will break this into steps. In this first step, We will draw a simple matplotlib chart with minimum data. Then in the second step, We will use the autoscale function. So let’s start.

 

Step 1: Simple matplotlib chart –

We will draw a line chart simply. Here is the code for that.

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1, 1, 25)
y = 3**x + 1
plt.plot(x, y)  
plt.show()

Once we run the code we get the below matplotlib chart.

matplotlib plot

 

Step 2: Enable autoscale() function –

In order to enable the autoscaling figures in matplotlib. we need to call autoscale() at the end of the chart. It also supports below parameters.

matplotlib.pyplot.autoscale(enable=True, axis=’both’, tight=None)

1. enable –

While adding this function in the code. we can make it dynamic while loading the chart. Suppose we have used the code but we use enable as False. It will make no impact and inactive the autoscaling feature.

2. axis –

Here we can choose the value from { x,y or both}. If we choose x, It will only autoscale the x-axis and accordingly for the y-axis. I think “both” is self-explanatory.

plt.autoscale(axis="x") 

3. tight –

Again it is a boolean parameter. If it is true, It will set the margin as zero.

Now, let’s add the autoscale() in the existing example.

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1, 1, 25)
y = 3**x + 1
plt.plot(x, y)  
plt.autoscale(axis="x", tight=True, enable=True) 
plt.show()
matplotlib plot autoscale

The above example has autoscaling feature enables with others parameter.

Conclusion –

Autoscaling graphs and chart is really essential. This article has given you a complete overview of autoscale() in matplotlib. If you have any concern over this topic. Please write back to us or comment below in the comment box. Well, Thanks for reading this article completly.

Thanks
Data Science Learner Team