# How Matplotlib set axis range works in Python : Know in 3 Steps We can apply Matplotlib set axis range using set_xlim([lower_limit, upper_limit]) and set_ylim([lower_limit, upper_limit]) function. In this article, We will implement a simple matplotlib chart and then we add set_xlim() and set_ylim() function over them.

## Matplotlib set axis range (Steps)-

In order to make it more understandable, Lets make implement set axis range ( set_xlim() and set_ylim() ) in steps.

### Step 1:

Let’s import the required package. We basically need a Matplotlib and NumPy module.

``````import matplotlib.pyplot as plt
import numpy as np``````

### Step 2:

Let’s create a sample matplotlib chart. We can create sin and cos waves.

``````import matplotlib.pyplot as plt
import numpy as np
#sample matplotlib chart
fig, ax = plt.subplots(figsize=(10, 5))
data = np.arange(0, 15, 0.3)
grp1 = np.sin(data)
grp2 = np.cos(data)
ax.plot(grp1, color='green', label='A')
ax.plot(grp2, color='red', label='B')
plt.show()``````

This is a sine and cosine way chart. The range is [-1,1] in Y-Axis. Also, Remember it is just for demonstration. You may choose a different chart as per your convenience.  Let’s move towards the third step.

### Step 3:

Now we can add xlim() and ylim() function over the above chart. We can use the same code base and add two other lines.

``````import matplotlib.pyplot as plt
import numpy as np
#sample matplotlib chart
fig, ax = plt.subplots(figsize=(10, 5))
data = np.arange(0, 15, 0.3)
grp1 = np.sin(data)
grp2 = np.cos(data)
ax.plot(grp1, color='green', label='A')
ax.plot(grp2, color='red', label='B')
plt.xlim([0, 50])
plt.ylim([-1, 0])
plt.show()``````

Here we have used the range [0, 50] for X-Axis and [-1, 0] for Y-Axis. Let’s run the above code.

As the above graph is limited to [0,50] in x-Axis and [-1,0] in y Axis. This is called the Matplotlib set axis range. This is a sub clip version of the above chart.

One more important thing, Using xlim() and ylim() together is not important. We may choose them singly any of them.

## End Notes-

In many scenarios, We need to set the maximum and minimum limits on Axis size. These are optional parameters to give a stronghold in the graph formatting. In this article, We have implemented the xlim() and ylim() function. I hope you guys are fine with implementation. If you are okay with the explanation of the steps. Please let us know. also if you need any further clarification, please comment below in the comment box.

Thanks
Data Science Learner Team 