Skip to content

Commit

Permalink
Add a warning if tickmarks outside axis
Browse files Browse the repository at this point in the history
Fixes #2990
  • Loading branch information
hyanwong authored and mergify[bot] committed Oct 1, 2024
1 parent 58872e9 commit 76ab046
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
19 changes: 19 additions & 0 deletions python/tests/test_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2220,6 +2220,25 @@ def test_y_axis_noticks(self):
assert svg_no_css.count("y-axis") == 1
assert svg_no_css.count('"tick"') == 0

def test_y_axis_tick_warning(sefl, caplog):
tree = msprime.simulate(4, random_seed=2).first()
upper = int(tree.time(tree.root))
with caplog.at_level(logging.WARNING):
tree.draw_svg(
y_axis=True,
y_label="Time",
y_ticks={upper + 100: "above", upper / 3: "inside"},
)
assert (
f"Ticks {{{upper+100}: 'above'}} lie outside the plotted axis"
in caplog.text
)
with caplog.at_level(logging.WARNING):
tree.draw_svg(
y_axis=True, y_label="Time", y_ticks={upper / 2: "inside", -1: "below"}
)
assert "Ticks {-1: 'below'} lie outside the plotted axis" in caplog.text

def test_symbol_size(self):
tree = msprime.simulate(4, random_seed=2, mutation_rate=8).first()
sz = 24
Expand Down
14 changes: 10 additions & 4 deletions python/tskit/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"""
import collections
import itertools
import logging
import math
import numbers
import operator
Expand Down Expand Up @@ -919,12 +920,13 @@ def draw_y_axis(
if self.y_axis:
y_axis.add(dwg.line((x, rnd(lower)), (x, rnd(upper)), class_="ax-line"))
ticks_group = y_axis.add(dwg.g(class_="ticks"))
tick_outside_axis = {}
for y, label in ticks.items():
y_pos = self.timescaling.transform(y)
if y_pos > lower or y_pos < upper: # nb lower > upper in SVG coords
tick_outside_axis[y] = label
tick = ticks_group.add(
dwg.g(
class_="tick",
transform=f"translate({x} {rnd(self.timescaling.transform(y))})",
)
dwg.g(class_="tick", transform=f"translate({x} {rnd(y_pos)})")
)
if gridlines:
tick.add(
Expand All @@ -941,6 +943,10 @@ def draw_y_axis(
class_="lab",
text_anchor="end",
)
if len(tick_outside_axis) > 0:
logging.warning(
f"Ticks {tick_outside_axis} lie outside the plotted axis"
)

def shade_background(
self,
Expand Down

0 comments on commit 76ab046

Please sign in to comment.