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
Draw axis lines or the origin for Matplotlib contour plot.
To draw axis lines or the origin for matplotlib contour plot, we can use contourf(), axhline() for horizontal lines at y=0, and axvline() for vertical lines at x=0.
Steps to Create Contour Plot with Origin Lines
Create data points for x, y, and z using numpy
Set the figure properties using plt.rcParams
Use contourf() method to create filled contour plot
Plot x=0 and y=0 lines using axhline() and axvline()
Display the figure using show() method
Example
Let's create a contour plot with origin axis lines ?
import numpy as np
import matplotlib.pyplot as plt
# Set figure properties
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create coordinate data
x = np.linspace(-1.0, 1.0, 10)
x, y = np.meshgrid(x, x)
z = -np.hypot(x, y)
# Turn off axis
plt.axis('off')
# Create contour plot
plt.contourf(x, y, z, 10)
# Add origin lines
plt.axhline(0, color='red', linewidth=1)
plt.axvline(0, color='red', linewidth=1)
plt.show()
Understanding the Components
Key Functions
np.meshgrid() − Creates coordinate matrices from coordinate vectors
np.hypot() − Calculates the Euclidean distance (hypotenuse)
contourf() − Creates filled contour plots
axhline() − Adds horizontal line at specified y-value
axvline() − Adds vertical line at specified x-value
Customizing Origin Lines
You can customize the appearance of the origin lines ?
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [8.00, 6.00]
x = np.linspace(-2.0, 2.0, 20)
x, y = np.meshgrid(x, x)
z = np.sin(x**2 + y**2)
# Create contour plot
plt.contourf(x, y, z, levels=15, cmap='viridis')
# Customize origin lines
plt.axhline(0, color='white', linewidth=2, linestyle='--', alpha=0.8)
plt.axvline(0, color='white', linewidth=2, linestyle='--', alpha=0.8)
plt.colorbar(label='Z values')
plt.title('Contour Plot with Custom Origin Lines')
plt.show()
Parameters for Origin Lines
| Parameter | Description | Example |
|---|---|---|
color |
Line color | 'red', 'white', '#FF0000' |
linewidth |
Line thickness | 1, 2, 0.5 |
linestyle |
Line style | '-', '--', ':', '-.' |
alpha |
Transparency | 0.5, 0.8, 1.0 |
Conclusion
Use axhline(0) and axvline(0) to add origin lines to contour plots. Customize the appearance with color, linewidth, and linestyle parameters for better visualization.
