I’ve seen how critical clear and precise data visualization is to making sense of complex datasets. Matplotlib is one of my go-to libraries for plotting, but sometimes the default axis scaling doesn’t quite cut it.
Especially when you want to focus on a specific range of your data on the y-axis, knowing how to set the y-axis range effectively can make your charts easier to interpret and more visually appealing. In this article, I’ll walk you through simple, practical ways to set and control the y-axis range in Matplotlib.
Let’s start!
Method to Set the Y-Axis Range
By default, Matplotlib automatically chooses y-axis limits based on your data’s minimum and maximum values. This usually works well, but there are times when you want to:
- Focus on a specific portion of your data for clarity.
- Maintain consistent y-axis scales across multiple plots for comparison.
- Exclude outliers that distort the scale.
- Highlight trends or anomalies within a defined range.
Setting the y-axis range manually gives you better control over how your data is presented.
Check out Python Matplotlib tick_params
Method 1: Use plt.ylim() to Set Y-Axis Limits
The easy way to set the y-axis range is by using the plt.ylim() function from Matplotlib’s pyplot module in Python. This method lets you specify the lower and upper bounds of the y-axis.
Here’s how I typically use it:
import matplotlib.pyplot as plt
# Example: Monthly sales data for a New York retail store
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
sales = [25000, 27000, 30000, 28000, 32000, 31000]
plt.plot(months, sales, marker='o')
plt.title('Monthly Sales in New York Store')
plt.xlabel('Month')
plt.ylabel('Sales ($)')
plt.ylim(20000, 35000) # Set y-axis range from 20,000 to 35,000
plt.show()You can see the output in the screenshot below.

In this example, I set the y-axis to range from 20,000 to 35,000 dollars to zoom in on the sales figures without the axis auto-scaling to unnecessary extremes.
Check out Matplotlib x-axis Label
Method 2: Set Y-Axis Limits on an Axes Object
When working with multiple subplots or more complex figures, you often manipulate axis objects directly. You can set the y-axis limits using the set_ylim() method on an Axes object.
Example:
fig, ax = plt.subplots()
# Example: Daily temperatures in Los Angeles over a week
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
temps = [75, 77, 79, 78, 76, 74, 73]
ax.plot(days, temps, marker='x', color='orange')
ax.set_title('Daily Temperatures in Los Angeles')
ax.set_xlabel('Day')
ax.set_ylabel('Temperature (°F)')
ax.set_ylim(70, 85) # Set y-axis limits between 70°F and 85°F
plt.show()You can see the output in the screenshot below.

This method is especially useful when you need to customize multiple plots independently.
Read Matplotlib Multiple Bar Chart
Method 3: Use axis() to Set Both X and Y Limits
Sometimes, you want to set both x-axis and y-axis limits simultaneously. The plt.axis() function allows you to do this by passing a list of [xmin, xmax, ymin, ymax].
Example:
plt.plot([1, 2, 3, 4, 5], [10, 20, 25, 30, 40])
plt.title('Product Demand Over 5 Weeks')
plt.xlabel('Week')
plt.ylabel('Demand (Units)')
plt.axis([1, 5, 15, 35]) # x-axis from 1 to 5, y-axis from 15 to 35
plt.show()You can see the output in the screenshot below.

This approach is quick when you want to control both axes at once.
Method 4: Autoscale with Margins
If you want Matplotlib to automatically scale the y-axis but with a bit of padding around your data, you can use the margins() function.
Example:
plt.plot([100, 200, 300, 400], [20, 35, 30, 40])
plt.title('Quarterly Profits in Chicago')
plt.xlabel('Quarter')
plt.ylabel('Profit ($ Thousands)')
plt.margins(y=0.1) # Adds 10% margin on y-axis
plt.show()This method keeps your data fully visible but prevents the plot from feeling cramped.
Check out Matplotlib Scatter Plot Legend
Method 5: Dynamic Setting Using Data Statistics
In some cases, you may want to set the y-axis limits dynamically based on your data’s statistics, like mean and standard deviation. This is useful when visualizing financial data or sensor readings where you want to highlight deviations.
Here’s how I do it:
import numpy as np
data = np.array([100, 110, 95, 105, 115, 90, 120])
mean = np.mean(data)
std_dev = np.std(data)
plt.plot(data, marker='s')
plt.title('Weekly Website Traffic in Boston')
plt.xlabel('Day')
plt.ylabel('Visitors')
plt.ylim(mean - 2*std_dev, mean + 2*std_dev) # Set y-axis to mean ± 2 std dev
plt.show()This method helps focus on the typical range of your data while filtering out extreme outliers.
Tips for Setting Y-Axis Range Effectively
- Consistency: When comparing multiple plots (e.g., sales across different states), keep y-axis ranges consistent to avoid misleading interpretations.
- Avoid Clipping: Ensure your y-axis limits include all relevant data points to prevent clipping.
- Use Margins: Add some padding around your data for better readability.
- Label Clearly: Always label your axes with units to provide context for your chosen range.
Setting the y-axis range in Matplotlib is a simple yet useful way to enhance your data visualizations. Whether you’re presenting quarterly sales for a Texas branch or analyzing temperature trends in Florida, controlling the y-axis helps your audience focus on the story your data tells.
With these methods, you can tailor your plots exactly how you want them, making your data clearer and your insights sharper.
If you have any questions or want to share your experiences with Matplotlib axis settings, feel free to leave a comment below!
Other related articles you might find helpful:

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.