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
How to mark a specific level in a contour map on Matplotlib?
To mark a specific level in a contour map on Matplotlib, you can use the contour() method with specific level values and highlight them using different colors or line styles. This technique is useful for emphasizing particular data ranges or thresholds in your visualization.
Basic Contour Plot with Labeled Levels
First, let's create a basic contour plot and label the contour lines ?
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
def f(x, y):
return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 40)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
contours = plt.contour(X, Y, Z, 3, colors='black')
plt.clabel(contours, inline=True, fontsize=8)
plt.title("Basic Contour Plot with Labels")
plt.show()
Marking Specific Levels with Different Colors
You can highlight specific contour levels by defining custom levels and assigning different colors ?
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [8.00, 6.00]
plt.rcParams["figure.autolayout"] = True
def f(x, y):
return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 40)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
# Define specific levels to mark
levels = [-1.5, -1.0, -0.5, 0, 0.5, 1.0, 1.5]
# Create contour plot with specific levels
contours = plt.contour(X, Y, Z, levels=levels, colors='gray', alpha=0.6)
# Mark a specific level (0.5) with a different color and style
specific_level = plt.contour(X, Y, Z, levels=[0.5], colors='red', linewidths=3)
# Label all contours
plt.clabel(contours, inline=True, fontsize=8)
plt.clabel(specific_level, inline=True, fontsize=10, fmt='Level: %.1f')
plt.title("Contour Plot with Highlighted Specific Level")
plt.colorbar(contours)
plt.show()
Using Multiple Highlighted Levels
You can mark multiple specific levels with different styling ?
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [8.00, 6.00]
plt.rcParams["figure.autolayout"] = True
# Create sample data
x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
X, Y = np.meshgrid(x, y)
Z = np.exp(-(X**2 + Y**2))
# Regular contour lines
regular_contours = plt.contour(X, Y, Z, levels=10, colors='lightblue', alpha=0.7)
# Mark specific levels with distinct styling
level_0_5 = plt.contour(X, Y, Z, levels=[0.5], colors='red', linewidths=4)
level_0_8 = plt.contour(X, Y, Z, levels=[0.8], colors='blue', linewidths=4, linestyles='dashed')
# Add labels
plt.clabel(regular_contours, inline=True, fontsize=8)
plt.clabel(level_0_5, inline=True, fontsize=12, fmt='Important: %.1f')
plt.clabel(level_0_8, inline=True, fontsize=12, fmt='Threshold: %.1f')
plt.title("Multiple Highlighted Contour Levels")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
Key Parameters for Marking Levels
| Parameter | Description | Example Values |
|---|---|---|
levels |
Specific contour levels to draw |
[0.5, 1.0] or 10
|
colors |
Color of contour lines |
'red', 'blue'
|
linewidths |
Width of contour lines |
1, 3, 5
|
linestyles |
Style of contour lines |
'solid', 'dashed'
|
Conclusion
Use the levels parameter in contour() to specify exact contour values you want to highlight. Combine different colors, line widths, and styles to make specific levels stand out in your contour map.
