Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df794ec

Browse files
committed
Transpose grid_finder tick representation.
Instead of representing the information about tick and gridlines as ``` { "lon_lines": [line, ...], # unused "lat_lines": [line, ...], "lon": [ "tick_levels": [level, ...], "tick_locs": {"left": [(xy, angle), ...], "bottom": [(xy, angle), ...], ...}, "tick_labels": {"left": [label, ...], "bottom": [label, ...], ...}, "lines": [line, ...], ], "lat": ..., # as for lon } ``` where the locs and labels are implicitly associated by the iteration order, group the information for each tick, and remove the redundant gridlines info as well: ``` { "lon": { "lines": [line, ...], "ticks": { "left": [ {"level": level, "loc": (xy, angle), "label": label}, ... ], "bottom": [...], ..., } } "lat": ..., # as for lon } ```
1 parent 62a5ba4 commit df794ec

File tree

2 files changed

+28
-52
lines changed

2 files changed

+28
-52
lines changed

lib/mpl_toolkits/axisartist/grid_finder.py

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -192,25 +192,28 @@ def get_grid_info(self, x1, y1, x2, y2):
192192

193193
grid_info = {
194194
"extremes": extremes,
195-
"lon_lines": lon_lines,
196-
"lat_lines": lat_lines,
197-
"lon": self._clip_grid_lines_and_find_ticks(
198-
lon_lines, lon_values, lon_levs, bb),
199-
"lat": self._clip_grid_lines_and_find_ticks(
200-
lat_lines, lat_values, lat_levs, bb),
195+
# "lon", "lat", filled below.
201196
}
202197

203-
tck_labels = grid_info["lon"]["tick_labels"] = {}
204-
for direction in ["left", "bottom", "right", "top"]:
205-
levs = grid_info["lon"]["tick_levels"][direction]
206-
tck_labels[direction] = self._format_ticks(
207-
1, direction, lon_factor, levs)
208-
209-
tck_labels = grid_info["lat"]["tick_labels"] = {}
210-
for direction in ["left", "bottom", "right", "top"]:
211-
levs = grid_info["lat"]["tick_levels"][direction]
212-
tck_labels[direction] = self._format_ticks(
213-
2, direction, lat_factor, levs)
198+
for idx, lon_or_lat, levs, factor, values, lines in [
199+
(1, "lon", lon_levs, lon_factor, lon_values, lon_lines),
200+
(2, "lat", lat_levs, lat_factor, lat_values, lat_lines),
201+
]:
202+
grid_info[lon_or_lat] = gi = {
203+
"lines": [[l] for l in lines],
204+
"ticks": {"left": [], "right": [], "bottom": [], "top": []},
205+
}
206+
for (lx, ly), v, level in zip(lines, values, levs):
207+
all_crossings = _find_line_box_crossings(np.column_stack([lx, ly]), bb)
208+
for side, crossings in zip(
209+
["left", "right", "bottom", "top"], all_crossings):
210+
for crossing in crossings:
211+
gi["ticks"][side].append({"level": level, "loc": crossing})
212+
for side in gi["ticks"]:
213+
levs = [tick["level"] for tick in gi["ticks"][side]]
214+
labels = self._format_ticks(idx, side, factor, levs)
215+
for tick, label in zip(gi["ticks"][side], labels):
216+
tick["label"] = label
214217

215218
return grid_info
216219

@@ -228,30 +231,6 @@ def _get_raw_grid_lines(self,
228231

229232
return lon_lines, lat_lines
230233

231-
def _clip_grid_lines_and_find_ticks(self, lines, values, levs, bb):
232-
gi = {
233-
"values": [],
234-
"levels": [],
235-
"tick_levels": dict(left=[], bottom=[], right=[], top=[]),
236-
"tick_locs": dict(left=[], bottom=[], right=[], top=[]),
237-
"lines": [],
238-
}
239-
240-
tck_levels = gi["tick_levels"]
241-
tck_locs = gi["tick_locs"]
242-
for (lx, ly), v, lev in zip(lines, values, levs):
243-
tcks = _find_line_box_crossings(np.column_stack([lx, ly]), bb)
244-
gi["levels"].append(v)
245-
gi["lines"].append([(lx, ly)])
246-
247-
for tck, direction in zip(tcks,
248-
["left", "right", "bottom", "top"]):
249-
for t in tck:
250-
tck_levels[direction].append(lev)
251-
tck_locs[direction].append(t)
252-
253-
return gi
254-
255234
def set_transform(self, aux_trans):
256235
if isinstance(aux_trans, Transform):
257236
self._aux_transform = aux_trans

lib/mpl_toolkits/axisartist/grid_helper_curvelinear.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ def iter_major():
8282
for nth_coord, show_labels in [
8383
(self.nth_coord_ticks, True), (1 - self.nth_coord_ticks, False)]:
8484
gi = self.grid_helper._grid_info[["lon", "lat"][nth_coord]]
85-
for (xy, angle_normal), l in zip(
86-
gi["tick_locs"][side], gi["tick_labels"][side]):
87-
yield xy, angle_normal, angle_tangent, (l if show_labels else "")
85+
for tick in gi["ticks"][side]:
86+
yield (*tick["loc"], angle_tangent,
87+
(tick["label"] if show_labels else ""))
8888

8989
return iter_major(), iter([])
9090

@@ -321,12 +321,9 @@ def get_tick_iterator(self, nth_coord, axis_side, minor=False):
321321
angle_tangent = dict(left=90, right=90, bottom=0, top=0)[axis_side]
322322
lon_or_lat = ["lon", "lat"][nth_coord]
323323
if not minor: # major ticks
324-
for (xy, angle_normal), l in zip(
325-
self._grid_info[lon_or_lat]["tick_locs"][axis_side],
326-
self._grid_info[lon_or_lat]["tick_labels"][axis_side]):
327-
yield xy, angle_normal, angle_tangent, l
324+
for tick in self._grid_info[lon_or_lat]["ticks"][axis_side]:
325+
yield *tick["loc"], angle_tangent, tick["label"]
328326
else:
329-
for (xy, angle_normal), l in zip(
330-
self._grid_info[lon_or_lat]["tick_locs"][axis_side],
331-
self._grid_info[lon_or_lat]["tick_labels"][axis_side]):
332-
yield xy, angle_normal, angle_tangent, ""
327+
for tick in self._grid_info[lon_or_lat]["ticks"][axis_side]:
328+
xy, angle_normal = tick["loc"]
329+
yield *tick["loc"], angle_tangent, ""

0 commit comments

Comments
 (0)