8000 exposed cap and join styles for lines · matplotlib/matplotlib@691eb4e · GitHub
[go: up one dir, main page]

Skip to content

Commit 691eb4e

Browse files
committed
exposed cap and join styles for lines
svn path=/trunk/matplotlib/; revision=1440
1 parent 3e63839 commit 691eb4e

File tree

4 files changed

+145
-37
lines changed

4 files changed

+145
-37
lines changed

.matplotlibrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ lines.markerfacecolor : blue
5353
lines.markeredgecolor : black
5454
lines.markeredgewidth : 0.5 # the line width around the marker symbol
5555
lines.markersize : 6 # markersize, in points
56+
lines.dash_joinstyle : miter # miter|round|bevel
57+
lines.dash_capstyle : butt # butt|round|projecting
58+
lines.solid_joinstyle : miter # miter|round|bevel
59+
lines.solid_capstyle : projecting # butt|round|projecting
5660
lines.antialiased : True # render lines in antialised (no jaggies)
5761
lines.data_clipping : False # Use data clipping in addition to viewport
5862
# clipping. Useful if you plot long data

CHANGELOG

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
New entries should be added at the top
22

3+
2005-06-13 Exposed cap and join style for lines with new rc params and
4+
line properties
5+
6+
lines.dash_joinstyle : miter # miter|round|bevel
7+
lines.dash_capstyle : butt # butt|round|projecting
8+
lines.solid_joinstyle : miter # miter|round|bevel
9+
lines.solid_capstyle : projecting # butt|round|projecting
10+
11+
312
2005-06-13 Added kwargs to Axes init
413

514
2005-06-13 Applied Baptiste's tick patch - JDH

lib/matplotlib/__init__.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,22 @@ def validate_comma_sep_str(s):
415415
except ValueError:
416416
raise ValueError('Could not convert all entries to strings')
417417

418-
def validate_orientation(s):
419-
if s.lower() in ['landscape', 'portrait']:
420-
return True
421-
else:
422-
raise ValueError('orientation must be one of: portrait, landscape')
418+
class ValidateInStrings:
419+
def __init__(self, valid, ignorecase=False):
420+
'valid is a list of legal strings'
421+
self.ignorecase = ignorecase
422+
def func(s):
423+
if ignorecase: return s.lower()
424+
else: return s
425+
self.validd = dict([(func(k),1) for k in valid])
426+
427+
def __call__(self, s):
428+
if self.ignorecase: s = s.lower()
429+
if s in self.validd: return s
430+
raise ValueError('Unrecognized string "%s": valid strings are %s'%(s, self.validd.keys()))
431+
432+
433+
validate_orientation = ValidateInStrings(['landscape', 'portrait'])
423434

424435
def validate_fontsize(s):
425436
if s.lower() in ['xx-small', 'x-small', 'small', 'medium', 'large', 'x-large',
@@ -451,28 +462,19 @@ def validate_verbose_fileo(s):
451462
verbose.fileo = fileo
452463
return verbose.fileo
453464

454-
455-
def validate_ps_papersize(s):
456-
papertypes = [
465+
466+
validate_ps_papersize = ValidateInStrings([
457467
'executive', 'letter', 'legal', 'ledger',
458468
'a0', 'a1', 'a2','a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10',
459469
'b0', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6',
460470
'c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6'
461-
]
462-
s = s.lower()
463-
if s in papertypes:
464-
return s
465-
else:
466-
raise ValueError('ps.papersize must be one of: %s'% ', '.join(papertypes))
467-
471+
], ignorecase=True)
468472

469-
def validate_tex_engine(s):
470-
if s.lower() in ['tex', 'latex']:
471-
return s
472-
else:
473-
raise ValueError('text.tex.engine must be one of: tex, latex')
473+
validate_tex_engine = ValidateInStrings(['tex', 'latex'], ignorecase=True)
474474

475+
validate_joinstyle = ValidateInStrings(['miter', 'round', 'bevel'], ignorecase=True)
475476

477+
validate_capstyle = ValidateInStrings(['butt', 'round', 'projecting'], ignorecase=True)
476478
# a map from key -> value, converter
477479
defaultParams = {
478480
'backend' : ['GTK', str],
@@ -496,6 +498,10 @@ def validate_tex_engine(s):
496498
'lines.markeredgewidth' : [0.5, validate_float],
497499
'lines.markersize' : [6, validate_float], # markersize, in points
498500
'lines.antialiased' : [True, validate_bool], # antialised (no jaggies)
501+
'lines.dash_joinstyle' : ['miter', validate_joinstyle],
502+
'lines.solid_joinstyle' : ['miter', validate_joinstyle],
503+
'lines.dash_capstyle' : ['butt', validate_capstyle],
504+
'lines.solid_capstyle' : ['projecting', validate_capstyle],
499505
'lines.data_clipping' : [False, validate_bool], # clip data
500506

501507
# patch props

lib/matplotlib/lines.py

Lines changed: 106 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,23 @@ class Line2D(Artist):
126126
}
127127

128128
zorder = 2
129+
validCap = ('butt', 'round', 'projecting')
130+
validJoin = ('miter', 'round', 'bevel')
129131

130132
def __init__(self, xdata, ydata,
131-
linewidth = None, # default to rc
132-
linestyle = None, # default to rc
133-
color = None, # default to rc
134-
marker = None, # default to rc
135-
markersize = None, # default to rc
136-
markeredgewidth = None, # default to rc
137-
markeredgecolor = None, # default to rc
138-
markerfacecolor = None, # default to rc
139-
antialiased = None, # default to rc
133+
linewidth = None, # all Nones default to rc
134+
linestyle = None,
135+
color = None,
136+
marker = None,
137+
markersize = None,
138+
markeredgewidth = None,
139+
markeredgecolor = None,
140+
markerfacecolor = None,
141+
antialiased = None,
142+
dash_capstyle = None,
143+
solid_capstyle = None,
144+
dash_joinstyle = None,
145+
solid_joinstyle = None,
140146
):
141147
"""
142148
xdata is a sequence of x data
@@ -170,6 +176,15 @@ def __init__(self, xdata, ydata,
170176

171177
if markersize is None : markersize=rcParams['lines.markersize']
172178
if antialiased is None : antialiased=rcParams['lines.antialiased']
179+
if dash_capstyle is None : dc=rcParams['lines.dash_capstyle']
180+
if dash_joinstyle is None : dj=rcParams['lines.dash_joinstyle']
181+
if solid_capstyle is None : sc=rcParams['lines.solid_capstyle']
182+
if solid_joinstyle is None : sj=rcParams['lines.solid_joinstyle']
183+
184+
self.set_dash_capstyle(dc)
185+
self.set_dash_joinstyle(dj)
186+
self.set_solid_capstyle(sc)
187+
self.set_solid_joinstyle(sj)
173188

174189

175190
self._linestyle = linestyle
@@ -344,6 +359,14 @@ def draw(self, renderer):
344359
if self.get_clip_on():
345360
gc.set_clip_rectangle(self.clipbox.get_bounds())
346361

362+
if self.is_dashed():
363+
cap = self._dashcapstyle
364+
join = self._dashjoinstyle
365+
else:
366+
cap = self._solidcapstyle
367+
join = self._solidjoinstyle
368+
gc.set_joinstyle(join)
369+
gc.set_capstyle(cap)
347370

348371
if self._newstyle:
349372
# transform in backend
@@ -602,7 +625,6 @@ def _draw_steps(self, renderer, gc, xt, yt):
602625
yt2=ones((2*siz,), yt.typecode())
603626
yt2[0:-1:2], yt2[1::2]=yt, yt
604627
gc.set_linestyle('solid')
605-
gc.set_capstyle('projecting')
606628

607629
if self._newstyle:
608630
renderer.draw_lines(gc, xt2, yt2, self._transform)
@@ -612,7 +634,6 @@ def _draw_steps(self, renderer, gc, xt, yt):
612634
def _draw_solid(self, renderer, gc, xt, yt):
613635
if len(xt)<2: return
614636
gc.set_linestyle('solid')
615-
gc.set_capstyle('projecting')
616637
if self._newstyle:
617638
renderer.draw_lines(gc, xt, yt, self._transform)
618639
else:
@@ -624,8 +645,6 @@ def _draw_dashed(self, renderer, gc, xt, yt):
624645
gc.set_linestyle('dashed')
625646
if self._dashSeq is not None:
626647
gc.set_dashes(0, self._dashSeq)
627-
gc.set_capstyle('butt')
628-
gc.set_joinstyle('miter')
629648

630649
if self._newstyle:
631650
renderer.draw_lines(gc, xt, yt, self._transform)
@@ -636,8 +655,6 @@ def _draw_dashed(self, renderer, gc, xt, yt):
636655
def _draw_dash_dot(self, renderer, gc, xt, yt):
637656
if len(xt)<2: return
638657
gc.set_linestyle('dashdot')
639-
gc.set_capstyle('butt')
640-
gc.set_joinstyle('miter')
641658
if self._newstyle:
642659
renderer.draw_lines(gc, xt, yt, self._transform)
643660
else:
@@ -647,8 +664,6 @@ def _draw_dotted(self, renderer, gc, xt, yt):
647664

648665
if len(xt)<2: return
649666
gc.set_linestyle('dotted')
650-
gc.set_capstyle('butt')
651-
gc.set_joinstyle('miter')
652667
if self._newstyle:
653668
renderer.draw_lines(gc, xt, yt, self._transform)
654669
else:
@@ -1205,3 +1220,77 @@ def get_mfc(self):
12051220
def get_ms(self):
12061221
'alias for get_markersize'
12071222
return self.get_markersize()
1223+
1224+
def set_dash_joinstyle(self, s):
1225+
"""
1226+
Set the join style for dashed linestyles
1227+
ACCEPTS: ['miter' | 'round' | 'bevel']
1228+
"""
1229+
s = s.lower()
1230+
if s not in self.validJoin:
1231+
raise ValueError('set_dash_joinstyle passed "%s"; valid joinstyles are %s'%(s, self.validJoin))
1232+
self._dashjoinstyle = s
1233+
1234+
def set_solid_joinstyle(self, s):
1235+
"""
1236+
Set the join style for solid linestyles
1237+
ACCEPTS: ['miter' | 'round' | 'bevel']
1238+
"""
1239+
s = s.lower()
1240+
if s not in self.validJoin:
1241+
raise ValueError('set_solid_joinstyle passed "%s"; valid joinstyles are %s'%(s, self.validJoin))
1242+
self._solidjoinstyle = s
1243+
1244+
1245+
def get_dash_joinstyle(self):
1246+
"""
1247+
Get the join style for dashed linestyles
1248+
"""
1249+
return self._dashjoinstyle
1250+
1251+
def get_solid_joinstyle(self):
1252+
"""
1253+
Get the join style for solid linestyles
1254+
"""
1255+
return self._solidjoinstyle
1256+
1257+
def set_dash_capstyle(self, s):
1258+
"""
1259+
Set the cap style for dashed linestyles
1260+
ACCEPTS: ['butt' | 'round' | 'projecting']
1261+
"""
1262+
s = s.lower()
1263+
if s not in self.validCap:
1264+
raise ValueError('set_dash_capstyle passed "%s"; valid capstyles are %s'%(s, self.validJoin))
1265+
1266+
self._dashcapstyle = s
1267+
1268+
1269+
def set_solid_capstyle(self, s):
1270+
"""
1271+
Set the cap style for solid linestyles
1272+
ACCEPTS: ['butt' | 'round' | 'projecting']
1273+
"""
1274+
s = s.lower()
1275+
if s not in self.validCap:
1276+
raise ValueError('set_solid_capstyle passed "%s"; valid capstyles are %s'%(s, self.validJoin))
1277+
1278+
self._solidcapstyle = s
1279+
1280+
1281+
def get_dash_capstyle(self):
1282+
"""
1283+
Get the cap style for dashed linestyles
1284+
"""
1285+
return self._dashcapstyle
1286+
1287+
def get_solid_capstyle(self):
1288+
"""
1289+
Get the cap style for solid linestyles
1290+
"""
1291+
return self._solidcapstyle
1292+
1293+
def is_dashed(self):
1294+
'return True if line is dashstyle'
1295+
return self._linestyle in ('--', '-.', ':')
1296+

0 commit comments

Comments
 (0)
0