Skip to content

Commit a2c51d1

Browse files
[Term Entry] Python:Matplotlib Pyplot: .xlim()
* Adding xlim plot * Add files via upload * Delete media/xlim-Plot-1.png * Add clean xlim example with images and markdown * Add clean xlim example with images and markdown * Revise .xlim() documentation for clarity Updated the description and examples for the .xlim() function in Matplotlib documentation for clarity and consistency. * Refine wording in xlim.md for clarity * Minor changes ---------
1 parent b3063ea commit a2c51d1

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
Title: '.xlim()'
3+
Description: 'Gets or sets the x-axis limits of the current axes in a Matplotlib plot.'
4+
Subjects:
5+
- 'Data Science'
6+
- 'Data Visualization'
7+
Tags:
8+
- 'Functions'
9+
- 'Matplotlib'
10+
- 'Subplots'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/data-science'
14+
---
15+
16+
The **`.xlim()`** function in `matplotlib.pyplot` is used to get or set the x-axis limits of the current plot. It helps control the visible range of data on the x-axis, zoom in on specific sections, or maintain consistent axis ranges across multiple plots.
17+
18+
The primary use cases for `.xlim()` include:
19+
20+
- Focusing on a particular range of x-values.
21+
- Maintaining consistent axis scaling between multiple plots.
22+
- Preventing automatic resizing that hides important data.
23+
24+
## Syntax
25+
26+
```pseudo
27+
matplotlib.pyplot.xlim(*args, **kwargs)
28+
```
29+
30+
**Parameters:**
31+
32+
The function accepts the following keyword arguments:
33+
34+
- `left` or `xmin` (float, optional): Lower limit of the x-axis.
35+
- `right` or `xmax` (float, optional): Upper limit of the x-axis.
36+
- `emit` (bool, default: True): Whether observers are notified of the axis limit change (default: True).
37+
- `auto` (bool, default: True): If `True`, turns on automatic scaling.
38+
39+
**Return value:**
40+
41+
The `.xlim()` function returns a tuple (xmin, xmax) representing the current x-axis limits.
42+
43+
## Example 1: Setting Custom X-Axis Limits
44+
45+
This example shows how to set specific x-axis limits using `.xlim()`:
46+
47+
```py
48+
import matplotlib.pyplot as plt
49+
import numpy as np
50+
51+
x = np.linspace(0, 20, 100)
52+
y = np.sin(x)
53+
54+
plt.plot(x, y, color='purple', linewidth=2)
55+
plt.title('Sine Wave with Custom X Limits')
56+
plt.xlabel('X')
57+
plt.ylabel('sin(x)')
58+
59+
# Set x-axis limits
60+
plt.xlim(0, 10)
61+
62+
plt.grid(True)
63+
plt.show()
64+
```
65+
66+
The output generated by this code will be:
67+
68+
![Line plot showing sine wave with limited x-axis range](https://raw.githubusercontent.com/Codecademy/docs/main/media/xlim-Plot-1.png)
69+
70+
This code plots a sine wave from 0 to 20 on the x-axis, but the `plt.xlim(0, 10)` command restricts the visible range to data between x = 0 and x = 10. As a result, only the first half of the sine wave is displayed. The curve appears smooth and purple, with grid lines and labeled axes for clarity.
71+
72+
## Example 2: Getting Current X-Axis Limits
73+
74+
Calling `.xlim()` without arguments retrieves the current x-axis limits:
75+
76+
```py
77+
import matplotlib.pyplot as plt
78+
79+
plt.plot([0, 1, 2, 3], [10, 20, 25, 30])
80+
print(plt.xlim()) # Outputs current x-axis limits
81+
plt.show()
82+
```
83+
84+
The output generated by this code will be:
85+
86+
![Line plot of points (0,10), (1,20), (2,25), (3,30) with automatically determined x-axis limits](https://raw.githubusercontent.com/Codecademy/docs/main/media/xlim-Plot-2.png)
87+
88+
In this case, the output is:
89+
90+
```shell
91+
(np.float64(-0.15), np.float64(3.15))
92+
```
93+
94+
This indicates that Matplotlib automatically adds a small margin around the plotted points for better visualization. The `.xlim()` function can also be applied after plotting to dynamically adjust the x-axis limits, as demonstrated in Example 1.

media/xlim-Plot-1.png

33.7 KB
Loading

media/xlim-Plot-2.png

23.6 KB
Loading

0 commit comments

Comments
 (0)