-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path55_plot_min_z_grid_values.py
More file actions
74 lines (63 loc) · 2.76 KB
/
55_plot_min_z_grid_values.py
File metadata and controls
74 lines (63 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import matplotlib.pyplot as plt
import numpy as np
def find_and_plot_min_z_in_grids(file_name):
# Initialize containers for data and bounds
data = []
x_min, x_max, y_min, y_max = float('inf'), -float('inf'), float('inf'), -float('inf')
# Read data and determine the bounds
with open(file_name, 'r') as f:
for line in f:
x, y, z = map(float, line.split())
data.append((x, y, z))
if x < x_min:
x_min = x
if x > x_max:
x_max = x
if y < y_min:
y_min = y
if y > y_max:
y_max = y
# Initialize dictionary to store min z and corresponding x, y in each grid
grid_min_z = {}
for x in range(int(x_min), int(x_max) + 1):
for y in range(int(y_min), int(y_max) + 1):
grid_min_z[(x, y)] = {'z': float('inf'), 'x': None, 'y': None}
# Update minimum z values in each corresponding grid
for x, y, z in data:
grid_x = int(x)
grid_y = int(y)
if z < grid_min_z[(grid_x, grid_y)]['z']:
grid_min_z[(grid_x, grid_y)] = {'z': z, 'x': x, 'y': y}
# Prepare data for plotting
num_x = int(x_max) - int(x_min) + 1
num_y = int(y_max) - int(y_min) + 1
z_values = np.full((num_y, num_x), np.nan)
labels = np.full((num_y, num_x), "", dtype=object)
for key, value in grid_min_z.items():
if value['z'] != float('inf'):
ix = key[0] - int(x_min)
iy = key[1] - int(y_min)
scaled_z = value['z'] * 4.3597 * 6.022 * 100
z_values[iy, ix] = scaled_z
labels[iy, ix] = f"{value['x']:.1f}\n{value['y']:.1f}\n{scaled_z:.1f}"
# Create a figure with high resolution
fig, ax = plt.subplots(dpi=600)
cax = ax.matshow(z_values, cmap='viridis', origin='lower')
# Add grid lines on the plot
ax.grid(which='both', linestyle='-', linewidth=0.5, color='gray', alpha=0.5)
# Add text annotations with scaled z values
for (i, j), label in np.ndenumerate(labels):
if label:
ax.text(j, i, label, va='center', ha='center', color='white', fontsize=4) # Adjust fontsize as needed
# Add colorbar and labels
fig.colorbar(cax)
plt.xticks(range(num_x), range(int(x_min), int(x_max) + 1))
plt.yticks(range(num_y), range(int(y_min), int(y_max) + 1))
plt.tick_params(axis='both', which='both', length=0) # Hide tick marks, keep labels
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.xaxis.tick_bottom() # Ensure ticks are on the bottom for x-axis
plt.title('Scaled minimum z values in each grid')
plt.show()
# Call the function with the file name
find_and_plot_min_z_in_grids('fes.dat')