diff --git a/content/matplotlib/concepts/pyplot/terms/xlim/xlim.md b/content/matplotlib/concepts/pyplot/terms/xlim/xlim.md new file mode 100644 index 00000000000..1b73ada5d4f --- /dev/null +++ b/content/matplotlib/concepts/pyplot/terms/xlim/xlim.md @@ -0,0 +1,94 @@ +--- +Title: '.xlim()' +Description: 'Gets or sets the x-axis limits of the current axes in a Matplotlib plot.' +Subjects: + - 'Data Science' + - 'Data Visualization' +Tags: + - 'Functions' + - 'Matplotlib' + - 'Subplots' +CatalogContent: + - 'learn-python-3' + - 'paths/data-science' +--- + +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. + +The primary use cases for `.xlim()` include: + +- Focusing on a particular range of x-values. +- Maintaining consistent axis scaling between multiple plots. +- Preventing automatic resizing that hides important data. + +## Syntax + +```pseudo +matplotlib.pyplot.xlim(*args, **kwargs) +``` + +**Parameters:** + +The function accepts the following keyword arguments: + +- `left` or `xmin` (float, optional): Lower limit of the x-axis. +- `right` or `xmax` (float, optional): Upper limit of the x-axis. +- `emit` (bool, default: True): Whether observers are notified of the axis limit change (default: True). +- `auto` (bool, default: True): If `True`, turns on automatic scaling. + +**Return value:** + +The `.xlim()` function returns a tuple (xmin, xmax) representing the current x-axis limits. + +## Example 1: Setting Custom X-Axis Limits + +This example shows how to set specific x-axis limits using `.xlim()`: + +```py +import matplotlib.pyplot as plt +import numpy as np + +x = np.linspace(0, 20, 100) +y = np.sin(x) + +plt.plot(x, y, color='purple', linewidth=2) +plt.title('Sine Wave with Custom X Limits') +plt.xlabel('X') +plt.ylabel('sin(x)') + +# Set x-axis limits +plt.xlim(0, 10) + +plt.grid(True) +plt.show() +``` + +The output generated by this code will be: + +![Line plot showing sine wave with limited x-axis range](https://raw.githubusercontent.com/Codecademy/docs/main/media/xlim-Plot-1.png) + +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. + +## Example 2: Getting Current X-Axis Limits + +Calling `.xlim()` without arguments retrieves the current x-axis limits: + +```py +import matplotlib.pyplot as plt + +plt.plot([0, 1, 2, 3], [10, 20, 25, 30]) +print(plt.xlim()) # Outputs current x-axis limits +plt.show() +``` + +The output generated by this code will be: + +![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) + +In this case, the output is: + +```shell +(np.float64(-0.15), np.float64(3.15)) +``` + +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. diff --git a/media/xlim-Plot-1.png b/media/xlim-Plot-1.png new file mode 100644 index 00000000000..5f92280bbc0 Binary files /dev/null and b/media/xlim-Plot-1.png differ diff --git a/media/xlim-Plot-2.png b/media/xlim-Plot-2.png new file mode 100644 index 00000000000..6ab4eb9e8f4 Binary files /dev/null and b/media/xlim-Plot-2.png differ