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 get coordinates from the contour in matplotlib?
To get coordinates from the contour in matplotlib, you can extract the vertices from the contour paths. This is useful for analyzing contour lines or exporting contour data for further processing.
Basic Contour Coordinate Extraction
Here's how to create a contour plot and extract its coordinates ?
import matplotlib.pyplot as plt
import numpy as np
# Set figure size
plt.rcParams["figure.figsize"] = [8, 6]
plt.rcParams["figure.autolayout"] = True
# Create sample data
x = [1, 2, 3, 4]
y = [1, 2, 3, 4]
z = [[15, 14, 13, 12],
[14, 12, 10, 8],
[13, 10, 7, 4],
[12, 8, 4, 0]]
# Create contour plot
cs = plt.contour(x, y, z)
# Extract coordinates from each contour line
for level_idx, collection in enumerate(cs.collections):
level = cs.levels[level_idx]
print(f"Contour level {level}:")
for path in collection.get_paths():
vertices = path.vertices
x_coords = vertices[:, 0]
y_coords = vertices[:, 1]
print(f" X coordinates: {x_coords}")
print(f" Y coordinates: {y_coords}")
print()
plt.title("Contour Plot with Coordinate Extraction")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
Contour level 4.0: X coordinates: [4.] Y coordinates: [4.] Contour level 6.0: X coordinates: [4. 3.5] Y coordinates: [3.5 4. ] Contour level 8.0: X coordinates: [4. 3.] Y coordinates: [3. 4.] Contour level 10.0: X coordinates: [4. 3.33333333 3. 2.5 ] Y coordinates: [2.5 3. 3.33333333 4. ] Contour level 12.0: X coordinates: [4. 3. 2.66666667 2. ] Y coordinates: [2. 2.66666667 3. 4. ]
Storing Coordinates in a Dictionary
You can organize the extracted coordinates by contour level for easier access ?
import matplotlib.pyplot as plt
# Create sample data
x = [1, 2, 3, 4]
y = [1, 2, 3, 4]
z = [[15, 14, 13, 12],
[14, 12, 10, 8],
[13, 10, 7, 4],
[12, 8, 4, 0]]
# Create contour plot
cs = plt.contour(x, y, z)
# Store coordinates in a dictionary
contour_coords = {}
for level_idx, collection in enumerate(cs.collections):
level = cs.levels[level_idx]
contour_coords[level] = []
for path in collection.get_paths():
vertices = path.vertices
contour_coords[level].append({
'x': vertices[:, 0],
'y': vertices[:, 1]
})
# Display the organized coordinates
for level, paths in contour_coords.items():
print(f"Level {level}: {len(paths)} path(s)")
for i, path in enumerate(paths):
print(f" Path {i}: {len(path['x'])} points")
plt.show()
Level 4.0: 1 path(s) Path 0: 1 points Level 6.0: 1 path(s) Path 0: 2 points Level 8.0: 1 path(s) Path 0: 2 points Level 10.0: 1 path(s) Path 0: 4 points Level 12.0: 1 path(s) Path 0: 4 points
Key Points
- cs.collections − Contains all contour line collections
- get_paths() − Returns path objects for each contour line
- vertices − Array of (x, y) coordinate pairs
- cs.levels − Contains the contour level values
Conclusion
Extract contour coordinates by iterating through cs.collections and accessing the vertices attribute of each path. This allows you to analyze contour data programmatically or export it for further use.
Advertisements
