|
| 1 | +--- |
| 2 | +Title: 'ylim()' |
| 3 | +Description: 'Sets or retrieves the y-axis limits of the current plot.' |
| 4 | +Subjects: |
| 5 | + - 'Data Science' |
| 6 | + - 'Data Visualization' |
| 7 | +Tags: |
| 8 | + - 'Graphs' |
| 9 | + - 'Libraries' |
| 10 | + - 'Matplotlib' |
| 11 | + - 'Methods' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/data-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`ylim()`** function in Matplotlib's `pyplot` module sets or retrieves the y-axis limits (minimum and maximum values) of the current plot. This function is essential for controlling the vertical range of data displayed in visualizations, allowing for better focus on specific data ranges or standardized comparisons across multiple plots. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +plt.ylim(bottom, top) |
| 23 | +``` |
| 24 | + |
| 25 | +Or, to get current limits: |
| 26 | + |
| 27 | +```pseudo |
| 28 | +bottom, top = plt.ylim() |
| 29 | +``` |
| 30 | + |
| 31 | +**Parameters:** |
| 32 | + |
| 33 | +- `bottom` (optional): A float representing the lower y-axis limit. If not provided, the current lower limit is returned. |
| 34 | +- `top` (optional): A float representing the upper y-axis limit. If not provided, the current upper limit is returned. |
| 35 | + |
| 36 | +**Return value:** |
| 37 | + |
| 38 | +The function returns: |
| 39 | + |
| 40 | +- A tuple `(bottom, top)` containing the current y-axis limits if no arguments are provided. |
| 41 | +- The new y-axis limits as a tuple if arguments are provided. |
| 42 | + |
| 43 | +## Example 1: Setting Y-Axis Limits |
| 44 | + |
| 45 | +The following example demonstrates how to use `ylim()` to set y-axis limits: |
| 46 | + |
| 47 | +```py |
| 48 | +import matplotlib.pyplot as plt |
| 49 | +import numpy as np |
| 50 | + |
| 51 | +# Generate sample data |
| 52 | +x = np.linspace(0, 10, 100) |
| 53 | +y = np.sin(x) |
| 54 | + |
| 55 | +# Create plot |
| 56 | +plt.plot(x, y) |
| 57 | +plt.ylim(-2, 2) # Set y-axis limits |
| 58 | +plt.xlabel('X-axis') |
| 59 | +plt.ylabel('Y-axis') |
| 60 | +plt.title('Sine Wave with Custom Y-axis Limits') |
| 61 | +plt.grid(True) |
| 62 | +plt.show() |
| 63 | +``` |
| 64 | + |
| 65 | +This code produces a sine wave plot with y-axis limits set from `-2` to `2`, providing extra vertical space above and below the sine wave values: |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +## Example 2: Retrieving Current Limits |
| 70 | + |
| 71 | +The following example shows how to retrieve and modify existing y-axis limits: |
| 72 | + |
| 73 | +```py |
| 74 | +import matplotlib.pyplot as plt |
| 75 | +import numpy as np |
| 76 | + |
| 77 | +# Create sample data |
| 78 | +x = np.arange(0, 10, 0.1) |
| 79 | +y = x ** 2 |
| 80 | + |
| 81 | +# Plot data |
| 82 | +plt.plot(x, y) |
| 83 | + |
| 84 | +# Get current y-axis limits |
| 85 | +current_bottom, current_top = plt.ylim() |
| 86 | + |
| 87 | +# Expand the limits by 20% |
| 88 | +range_y = current_top - current_bottom |
| 89 | +plt.ylim(current_bottom - 0.2 * range_y, current_top + 0.2 * range_y) |
| 90 | + |
| 91 | +plt.xlabel('X values') |
| 92 | +plt.ylabel('Y values') |
| 93 | +plt.title('Quadratic Function with Expanded Y-axis') |
| 94 | +plt.show() |
| 95 | +``` |
| 96 | + |
| 97 | +This example retrieves the automatically calculated y-axis limits and expands them by 20% on both sides, providing more whitespace around the data: |
| 98 | + |
| 99 | + |
0 commit comments