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

Skip to content

Transpose grid_finder tick representation. #27373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 20 additions & 41 deletions lib/mpl_toolkits/axisartist/grid_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,25 +192,28 @@ def get_grid_info(self, x1, y1, x2, y2):

grid_info = {
"extremes": extremes,
"lon_lines": lon_lines,
"lat_lines": lat_lines,
"lon": self._clip_grid_lines_and_find_ticks(
lon_lines, lon_values, lon_levs, bb),
"lat": self._clip_grid_lines_and_find_ticks(
lat_lines, lat_values, lat_levs, bb),
# "lon", "lat", filled below.
}

tck_labels = grid_info["lon"]["tick_labels"] = {}
for direction in ["left", "bottom", "right", "top"]:
levs = grid_info["lon"]["tick_levels"][direction]
tck_labels[direction] = self._format_ticks(
1, direction, lon_factor, levs)

tck_labels = grid_info["lat"]["tick_labels"] = {}
for direction in ["left", "bottom", "right", "top"]:
levs = grid_info["lat"]["tick_levels"][direction]
tck_labels[direction] = self._format_ticks(
2, direction, lat_factor, levs)
for idx, lon_or_lat, levs, factor, values, lines in [
(1, "lon", lon_levs, lon_factor, lon_values, lon_lines),
(2, "lat", lat_levs, lat_factor, lat_values, lat_lines),
]:
grid_info[lon_or_lat] = gi = {
"lines": [[l] for l in lines],
"ticks": {"left": [], "right": [], "bottom": [], "top": []},
}
for (lx, ly), v, level in zip(lines, values, levs):
all_crossings = _find_line_box_crossings(np.column_stack([lx, ly]), bb)
for side, crossings in zip(
["left", "right", "bottom", "top"], all_crossings):
for crossing in crossings:
gi["ticks"][side].append({"level": level, "loc": crossing})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would make sense for this interior dictionary to be a NumPy record array?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conceptually that makes sense, but I'm not overly convinced by the patch, which is just

diff --git i/lib/mpl_toolkits/axisartist/grid_finder.py w/lib/mpl_toolkits/axisartist/grid_finder.py
index 897a4152b0..a01a4df53e 100644
--- i/lib/mpl_toolkits/axisartist/grid_finder.py
+++ w/lib/mpl_toolkits/axisartist/grid_finder.py
@@ -208,12 +208,12 @@ class GridFinder:
                 for side, crossings in zip(
                         ["left", "right", "bottom", "top"], all_crossings):
                     for crossing in crossings:
-                        gi["ticks"][side].append({"level": level, "loc": crossing})
+                        gi["ticks"][side].append((level, *crossing, None))
             for side in gi["ticks"]:
-                levs = [tick["level"] for tick in gi["ticks"][side]]
-                labels = self._format_ticks(idx, side, factor, levs)
-                for tick, label in zip(gi["ticks"][side], labels):
-                    tick["label"] = label
+                gi["ticks"][side] = ticks = np.array(
+                    gi["ticks"][side],
+                    [("level", float), ("loc", float, 2), ("angle", float), ("label", object)])
+                ticks["label"] = self._format_ticks(idx, side, factor, ticks["level"])

         return grid_info

diff --git i/lib/mpl_toolkits/axisartist/grid_helper_curvelinear.py w/lib/mpl_toolkits/axisartist/grid_helper_curvelinear.py
index 451e1159ed..d7ed6e8062 100644
--- i/lib/mpl_toolkits/axisartist/grid_helper_curvelinear.py
+++ w/lib/mpl_toolkits/axisartist/grid_helper_curvelinear.py
@@ -83,7 +83,7 @@ class FixedAxisArtistHelper(_FixedAxisArtistHelperBase):
                     (self.nth_coord_ticks, True), (1 - self.nth_coord_ticks, False)]:
                 gi = self.grid_helper._grid_info[["lon", "lat"][nth_coord]]
                 for tick in gi["ticks"][side]:
-                    yield (*tick["loc"], angle_tangent,
+                    yield (*tick[["loc", "angle"]], angle_tangent,
                            (tick["label"] if show_labels else ""))

         return iter_major(), iter([])
@@ -322,8 +322,7 @@ class GridHelperCurveLinear(GridHelperBase):
         lon_or_lat = ["lon", "lat"][nth_coord]
         if not minor:  # major ticks
             for tick in self._grid_info[lon_or_lat]["ticks"][axis_side]:
-                yield *tick["loc"], angle_tangent, tick["label"]
+                yield *tick[["loc", "angle"]], angle_tangent, tick["label"]
         else:
             for tick in self._grid_info[lon_or_lat]["ticks"][axis_side]:
-                yield *tick["loc"], angle_tangent, ""
+                yield *tick[["loc", "angle"]], angle_tangent, ""

Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better if we actually used the arrays as a whole, but since the users (in grid_helper_curvelinear.py) are just doing a yield on each row, there doesn't seem to be too much benefit here.

for side in gi["ticks"]:
levs = [tick["level"] for tick in gi["ticks"][side]]
labels = self._format_ticks(idx, side, factor, levs)
for tick, label in zip(gi["ticks"][side], labels):
tick["label"] = label

return grid_info

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

return lon_lines, lat_lines

def _clip_grid_lines_and_find_ticks(self, lines, values, levs, bb):
gi = {
"values": [],
"levels": [],
"tick_levels": dict(left=[], bottom=[], right=[], top=[]),
"tick_locs": dict(left=[], bottom=[], right=[], top=[]),
"lines": [],
}

tck_levels = gi["tick_levels"]
tck_locs = gi["tick_locs"]
for (lx, ly), v, lev in zip(lines, values, levs):
tcks = _find_line_box_crossings(np.column_stack([lx, ly]), bb)
gi["levels"].append(v)
gi["lines"].append([(lx, ly)])

for tck, direction in zip(tcks,
["left", "right", "bottom", "top"]):
for t in tck:
tck_levels[direction].append(lev)
tck_locs[direction].append(t)

return gi

def set_transform(self, aux_trans):
if isinstance(aux_trans, Transform):
self._aux_transform = aux_trans
Expand Down
18 changes: 7 additions & 11 deletions lib/mpl_toolkits/axisartist/grid_helper_curvelinear.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ def iter_major():
for nth_coord, show_labels in [
(self.nth_coord_ticks, True), (1 - self.nth_coord_ticks, False)]:
gi = self.grid_helper._grid_info[["lon", "lat"][nth_coord]]
for (xy, angle_normal), l in zip(
gi["tick_locs"][side], gi["tick_labels"][side]):
yield xy, angle_normal, angle_tangent, (l if show_labels else "")
for tick in gi["ticks"][side]:
yield (*tick["loc"], angle_tangent,
(tick["label"] if show_labels else ""))

return iter_major(), iter([])

Expand Down Expand Up @@ -321,12 +321,8 @@ def get_tick_iterator(self, nth_coord, axis_side, minor=False):
angle_tangent = dict(left=90, right=90, bottom=0, top=0)[axis_side]
lon_or_lat = ["lon", "lat"][nth_coord]
if not minor: # major ticks
for (xy, angle_normal), l in zip(
self._grid_info[lon_or_lat]["tick_locs"][axis_side],
self._grid_info[lon_or_lat]["tick_labels"][axis_side]):
yield xy, angle_normal, angle_tangent, l
for tick in self._grid_info[lon_or_lat]["ticks"][axis_side]:
yield *tick["loc"], angle_tangent, tick["label"]
else:
for (xy, angle_normal), l in zip(
self._grid_info[lon_or_lat]["tick_locs"][axis_side],
self._grid_info[lon_or_lat]["tick_labels"][axis_side]):
yield xy, angle_normal, angle_tangent, ""
for tick in self._grid_info[lon_or_lat]["ticks"][axis_side]:
yield *tick["loc"], angle_tangent, ""