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

Skip to content

Commit e270359

Browse files
committed
Merge pull request #695 from mdboom/pixel_marker
Fix single pixel markers
2 parents eb95530 + b05aa16 commit e270359

File tree

5 files changed

+115
-42
lines changed

5 files changed

+115
-42
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,10 +1187,10 @@ def writeImages(self):
11871187

11881188
img.flipud_out()
11891189

1190-
def markerObject(self, path, trans, fillp, lw):
1190+
def markerObject(self, path, trans, fillp, lw, joinstyle, capstyle):
11911191
"""Return name of a marker XObject representing the given path."""
11921192
pathops = self.pathOperations(path, trans, simplify=False)
1193-
key = (tuple(pathops), bool(fillp))
1193+
key = (tuple(pathops), bool(fillp), joinstyle, capstyle)
11941194
result = self.markers.get(key)
11951195
if result is None:
11961196
name = Name('M%d' % len(self.markers))
@@ -1204,12 +1204,14 @@ def markerObject(self, path, trans, fillp, lw):
12041204
return name
12051205

12061206
def writeMarkers(self):
1207-
for (pathops, fillp),(name, ob, bbox, lw) in self.markers.iteritems():
1207+
for (pathops, fillp, joinstyle, capstyle),(name, ob, bbox, lw) in self.markers.iteritems():
12081208
bbox = bbox.padded(lw * 0.5)
12091209
self.beginStream(
12101210
ob.id, None,
12111211
{'Type': Name('XObject'), 'Subtype': Name('Form'),
12121212
'BBox': list(bbox.extents) })
1213+
self.output(GraphicsContextPdf.joinstyles[joinstyle], Op.setlinejoin)
1214+
self.output(GraphicsContextPdf.capstyles[capstyle], Op.setlinecap)
12131215
self.output(*pathops)
12141216
if fillp:
12151217
self.output(Op.fill_stroke)
@@ -1402,7 +1404,7 @@ def draw_image(self, gc, x, y, im, dx=None, dy=None, transform=None):
14021404
h = 72.0*h/self.image_dpi
14031405
else:
14041406
h = dy
1405-
1407+
14061408
imob = self.file.imageObject(im)
14071409

14081410
if transform is None:
@@ -1416,7 +1418,7 @@ def draw_image(self, gc, x, y, im, dx=None, dy=None, transform=None):
14161418
tr1, tr2, tr3, tr4, tr5, tr6, Op.concat_matrix,
14171419
w, 0, 0, h, x, y, Op.concat_matrix,
14181420
imob, Op.use_xobject, Op.grestore)
1419-
1421+
14201422

14211423
def draw_path(self, gc, path, transform, rgbFace=None):
14221424
self.check_gc(gc, rgbFace)
@@ -1438,7 +1440,8 @@ def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None)
14381440

14391441
output = self.file.output
14401442
marker = self.file.markerObject(
1441-
marker_path, marker_trans, fillp, self.gc._linewidth)
1443+
marker_path, marker_trans, fillp, self.gc._linewidth,
1444+
gc.get_joinstyle(), gc.get_capstyle())
14421445

14431446
output(Op.gsave)
14441447
lastx, lasty = 0, 0

lib/matplotlib/backends/backend_ps.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def gs_exe(self):
7272
self._cached["gs_exe"] = gs_exe
7373
return gs_exe
7474

75-
75+
7676
@property
7777
def gs_version(self):
7878
"""
@@ -97,7 +97,7 @@ def supports_ps2write(self):
9797
True if the installed ghostscript supports ps2write device.
9898
"""
9999
return self.gs_version[0] >= 9
100-
100+
101101
ps_backend_helper = PsBackendHelper()
102102

103103
papersize = {'letter': (8.5,11),
@@ -582,13 +582,29 @@ def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None)
582582

583583
# construct the generic marker command:
584584
ps_cmd = ['/o {', 'gsave', 'newpath', 'translate'] # dont want the translate to be global
585+
586+
lw = gc.get_linewidth()
587+
stroke = lw != 0.0
588+
if stroke:
589+
ps_cmd.append('%.1f setlinewidth' % lw)
590+
jint = gc.get_joinstyle()
591+
ps_cmd.append('%d setlinejoin' % jint)
592+
cint = gc.get_capstyle()
593+
ps_cmd.append('%d setlinecap' % cint)
594+
585595
ps_cmd.append(self._convert_path(marker_path, marker_trans,
586596
simplify=False))
587597

588598
if rgbFace:
589-
ps_cmd.extend(['gsave', ps_color, 'fill', 'grestore'])
599+
if stroke:
600+
ps_cmd.append('gsave')
601+
ps_cmd.extend([ps_color, 'fill'])
602+
if stroke:
603+
ps_cmd.append('grestore')
590604

591-
ps_cmd.extend(['stroke', 'grestore', '} bind def'])
605+
if stroke:
606+
ps_cmd.append('stroke')
607+
ps_cmd.extend(['grestore', '} bind def'])
592608

593609
for vertices, code in path.iter_segments(trans, simplify=False):
594610
if len(vertices):

lib/matplotlib/backends/backend_svg.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def _write_hatches(self):
370370
writer.end('pattern')
371371
writer.end('defs')
372372

373-
def _get_style(self, gc, rgbFace):
373+
def _get_style_dict(self, gc, rgbFace):
374374
"""
375375
return the style string. style is generated from the
376376
GraphicsContext and rgbFace
@@ -403,7 +403,10 @@ def _get_style(self, gc, rgbFace):
403403
if gc.get_capstyle() != 'projecting':
404404
attrib['stroke-linecap'] = _capstyle_d[gc.get_capstyle()]
405405

406-
return generate_css(attrib)
406+
return attrib
407+
408+
def _get_style(self, gc, rgbFace):
409+
return generate_css(self._get_style_dict(gc, rgbFace))
407410

408411
def _get_clip(self, gc):
409412
cliprect = gc.get_clip_rectangle()
@@ -536,12 +539,18 @@ def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None)
536539
marker_path,
537540
marker_trans + Affine2D().scale(1.0, -1.0),
538541
simplify=False)
539-
dictkey = (path_data)
542+
style = self._get_style_dict(gc, rgbFace)
543+
dictkey = (path_data, generate_css(style))
540544
oid = self._markers.get(dictkey)
545+
for key in style.keys():
546+
if not key.startswith('stroke'):
547+
del style[key]
548+
style = generate_css(style)
549+
541550
if oid is None:
542551
oid = self._make_id('m', dictkey)
543552
writer.start('defs')
544-
writer.element('path', id=oid, d=path_data)
553+
writer.element('path', id=oid, d=path_data, style=style)
545554
writer.end('defs')
546555
self._markers[dictkey] = oid
547556

lib/matplotlib/lines.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,11 +541,16 @@ def draw(self, renderer):
541541
if type(snap) == float:
542542
snap = renderer.points_to_pixels(self._markersize) >= snap
543543
gc.set_snap(snap)
544+
gc.set_joinstyle(marker.get_joinstyle())
545+
gc.set_capstyle(marker.get_capstyle())
544546
marker_path = marker.get_path()
545547
marker_trans = marker.get_transform()
546548
w = renderer.points_to_pixels(self._markersize)
547-
if marker.get_marker() != ',': # Don't scale for pixels
549+
if marker.get_marker() != ',':
550+
# Don't scale for pixels, and don't stroke them
548551
marker_trans = marker_trans.scale(w)
552+
else:
553+
gc.set_linewidth(0)
549554
renderer.draw_markers(
550555
gc, marker_path, marker_trans, subsampled, affine.frozen(),
551556
rgbFace)

0 commit comments

Comments
 (0)