Skip to content

Commit b7f9632

Browse files
committed
fixes for Plot[]
1 parent 105c325 commit b7f9632

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

mathics/builtin/graphics.py

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ def coords(value):
5454

5555

5656
class Coords(object):
57-
def __init__(self, graphics, expr=None, pos=None, d=None):
57+
def __init__(self, graphics, expr=None, pos=None):
5858
self.graphics = graphics
5959
self.p = pos
60-
self.d = d
6160
if expr is not None:
6261
if expr.has_form('Offset', 1, 2):
6362
self.d = coords(expr.leaves[0])
@@ -71,17 +70,29 @@ def __init__(self, graphics, expr=None, pos=None, d=None):
7170
def pos(self):
7271
p = self.p
7372
p = (cut(p[0]), cut(p[1]))
74-
if self.d is not None:
75-
d = self.graphics.translate_absolute(self.d)
76-
return p[0] + d[0], p[1] + d[1]
77-
else:
78-
return p
73+
return p
7974

8075
def add(self, x, y):
8176
p = (self.p[0] + x, self.p[1] + y)
8277
return Coords(self.graphics, pos=p, d=self.d)
8378

8479

80+
class AxisCoords(Coords):
81+
def __init__(self, graphics, expr=None, pos=None, d=None):
82+
super(AxisCoords, self).__init__(graphics, expr=expr, pos=pos)
83+
self.d = d
84+
85+
def pos(self):
86+
p = self.p
87+
p = self.graphics.translate(p)
88+
p = (cut(p[0]), cut(p[1]))
89+
if self.d is not None:
90+
d = self.graphics.translate_absolute_in_pixels(self.d)
91+
return p[0] + d[0], p[1] + d[1]
92+
else:
93+
return p
94+
95+
8596
def cut(value):
8697
"Cut values in graphics primitives (not displayed otherwise in SVG)"
8798
border = 10 ** 8
@@ -1146,7 +1157,6 @@ def patch_transforms(self, transforms):
11461157

11471158
def extent(self):
11481159
def points():
1149-
#fixed_transforms = [self.graphics.fix_transform(transform) for transform in self.transforms]
11501160
for content in self.contents:
11511161
for transform in self.transforms:
11521162
p = content.extent()
@@ -1156,7 +1166,6 @@ def points():
11561166

11571167
def to_svg(self):
11581168
def instances():
1159-
# fixed_transforms = [self.graphics.fix_transform(transform) for transform in self.transforms]
11601169
for content in self.contents:
11611170
content_svg = content.to_svg()
11621171
for transform in self.transforms:
@@ -1429,18 +1438,27 @@ def set_size(self, xmin, ymin, extent_width, extent_height, pixel_width, pixel_h
14291438
self.elements[0].patch_transforms([transform])
14301439
self.local_to_world = transform
14311440

1441+
def add_axis_element(self, e):
1442+
# axis elements are added after the GeometricTransformationBox and are thus not
1443+
# subject to the transformation from local to pixel space.
1444+
self.elements.append(e)
1445+
14321446
def translate(self, coords):
14331447
if self.local_to_world:
14341448
return list(self.local_to_world.transform([coords]))[0]
14351449
else:
14361450
return coords[0], coords[1]
14371451

14381452
def translate_absolute(self, d):
1453+
s = self.extent_width / self.pixel_width
1454+
x, y = self.translate_absolute_in_pixels(d)
1455+
return x * s, y * s
1456+
1457+
def translate_absolute_in_pixels(self, d):
14391458
if self.local_to_world is None:
14401459
return 0, 0
14411460
else:
1442-
s = self.extent_width / self.pixel_width # d is a pixel size
1443-
l = s * 96.0 / 72
1461+
l = 96.0 / 72 # d is measured in printer's points
14441462
return d[0] * l, (-1 if self.neg_y else 1) * d[1] * l
14451463

14461464
def translate_relative(self, x):
@@ -1794,7 +1812,7 @@ def create_axes(self, elements, graphics_options, xmin, xmax, ymin, ymax):
17941812

17951813
def add_element(element):
17961814
element.is_completely_visible = True
1797-
elements.elements.append(element)
1815+
elements.add_axis_element(element)
17981816

17991817
ticks_x, ticks_x_small, origin_x = self.axis_ticks(xmin, xmax)
18001818
ticks_y, ticks_y_small, origin_y = self.axis_ticks(ymin, ymax)
@@ -1814,26 +1832,24 @@ def add_element(element):
18141832
if axes[index]:
18151833
add_element(LineBox(
18161834
elements, axes_style[index],
1817-
lines=[[Coords(elements, pos=p_origin(min),
1818-
d=p_other0(-axes_extra)),
1819-
Coords(elements, pos=p_origin(max),
1820-
d=p_other0(axes_extra))]]))
1835+
lines=[[AxisCoords(elements, pos=p_origin(min), d=p_other0(-axes_extra)),
1836+
AxisCoords(elements, pos=p_origin(max), d=p_other0(axes_extra))]]))
18211837
ticks_lines = []
18221838
tick_label_style = ticks_style[index].clone()
18231839
tick_label_style.extend(label_style)
18241840
for x in ticks:
1825-
ticks_lines.append([Coords(elements, pos=p_origin(x)),
1826-
Coords(elements, pos=p_origin(x),
1841+
ticks_lines.append([AxisCoords(elements, pos=p_origin(x)),
1842+
AxisCoords(elements, pos=p_origin(x),
18271843
d=p_self0(tick_large_size))])
18281844
add_element(InsetBox(
18291845
elements, tick_label_style,
18301846
content=Real('%g' % x), # fix e.g. 0.6000000000000001
1831-
pos=Coords(elements, pos=p_origin(x),
1847+
pos=AxisCoords(elements, pos=p_origin(x),
18321848
d=p_self0(-tick_label_d)), opos=p_self0(1)))
18331849
for x in ticks_small:
18341850
pos = p_origin(x)
1835-
ticks_lines.append([Coords(elements, pos=pos),
1836-
Coords(elements, pos=pos,
1851+
ticks_lines.append([AxisCoords(elements, pos=pos),
1852+
AxisCoords(elements, pos=pos,
18371853
d=p_self0(tick_small_size))])
18381854
add_element(LineBox(elements, axes_style[0],
18391855
lines=ticks_lines))

0 commit comments

Comments
 (0)