Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to handle an asymptote/discontinuity with Matplotlib?
When plotting functions with asymptotes or discontinuities using Matplotlib, we need special handling to avoid connecting points across the discontinuity. The function y = 1/x has a vertical asymptote at x = 0, creating a discontinuity that requires careful plotting.
Basic Approach with Discontinuity Issues
Let's first see what happens when we plot y = 1/x naively ?
import numpy as np
import matplotlib.pyplot as plt
# This will cause issues at x=0
x = np.linspace(-1, 1, 100)
y = 1 / x
plt.figure(figsize=[8, 5])
plt.plot(x, y, 'b-', label='y=1/x')
plt.axhline(y=0, color='red', linestyle='--', alpha=0.7, label='y=0')
plt.axvline(x=0, color='red', linestyle='--', alpha=0.7, label='x=0')
plt.ylim(-10, 10)
plt.legend()
plt.grid(True, alpha=0.3)
plt.title('Function y=1/x with Asymptotes')
plt.show()
Proper Method: Split the Domain
To handle the discontinuity properly, split the domain around the asymptote ?
import numpy as np
import matplotlib.pyplot as plt
# Split domain to avoid x=0
x1 = np.linspace(-2, -0.01, 100) # Left side of asymptote
x2 = np.linspace(0.01, 2, 100) # Right side of asymptote
y1 = 1 / x1
y2 = 1 / x2
plt.figure(figsize=[8, 6])
# Plot both branches separately
plt.plot(x1, y1, 'b-', linewidth=2, label='y=1/x')
plt.plot(x2, y2, 'b-', linewidth=2)
# Add asymptotes
plt.axhline(y=0, color='red', linestyle='--', alpha=0.7, label='Horizontal asymptote (y=0)')
plt.axvline(x=0, color='red', linestyle='--', alpha=0.7, label='Vertical asymptote (x=0)')
# Formatting
plt.ylim(-10, 10)
plt.xlim(-2, 2)
plt.grid(True, alpha=0.3)
plt.legend()
plt.title('Properly Handled Asymptote for y=1/x')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
Advanced Method: Using Masked Arrays
NumPy's masked arrays can automatically handle discontinuities ?
import numpy as np
import matplotlib.pyplot as plt
# Create domain including x=0
x = np.linspace(-2, 2, 400)
# Calculate y, this will produce inf at x=0
y = 1 / x
# Mask values where abs(y) is too large
y_masked = np.ma.masked_where(np.abs(y) > 50, y)
plt.figure(figsize=[8, 6])
# Plot with masked array
plt.plot(x, y_masked, 'b-', linewidth=2, label='y=1/x')
# Add asymptotes
plt.axhline(y=0, color='red', linestyle='--', alpha=0.7)
plt.axvline(x=0, color='red', linestyle='--', alpha=0.7)
# Add text annotations for asymptotes
plt.text(0.1, 8, 'x = 0 (vertical asymptote)', rotation=90, fontsize=10)
plt.text(1.2, 1, 'y = 0 (horizontal asymptote)', fontsize=10)
plt.ylim(-10, 10)
plt.xlim(-2, 2)
plt.grid(True, alpha=0.3)
plt.legend()
plt.title('Using Masked Arrays for Discontinuities')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
Comparison of Methods
| Method | Pros | Cons | Best For |
|---|---|---|---|
| Split Domain | Complete control, clean result | Manual domain calculation | Known asymptote locations |
| Masked Arrays | Automatic handling | Requires threshold tuning | Multiple or unknown asymptotes |
| Basic Plot | Simple code | Connects across discontinuity | Continuous functions only |
Conclusion
For functions with asymptotes, split the domain around discontinuities or use masked arrays to avoid connecting points across the asymptote. Always add reference lines to clearly show the asymptotic behavior to readers.
Advertisements
