Hi all,
I think I have finally found a nice way to implement step-plots with
different linestyles. The way this is done required some re-writing of
lines.py, but I think it is done in a consistent manner:
A new property *drawstyle* is introduced. By default this just
connects the datapoints by a straight line. It can be set to "steps-***"
to create step-plots. So, the important point is that "steps-***" is no
longer a linestyle and thus it is possible to set the linestyle
independently. Additionally one can set a combined line- and drawstyle,
e.g. linestyle='steps-mid:'.
The drawstyle property has the advantage, that one can add other ways
to draw the data naturally, for example one can think of a drawstyle
"bspline" that connects data-points using a spline, or something like a
moving-mean plot.
The patch is applied and also an example and its output. If there are
no objections, I will commit the patch soon.
Any comments ???
Manuel
Index: lines.py
===
--- lines.py (revision 6115)
+++ lines.py (working copy)
@@ -11,7 +11,8 @@
from matplotlib import verbose
import artist
from artist import Artist
-from cbook import iterable, is_string_like, is_numlike, ls_mapper, dedent
+from cbook import iterable, is_string_like, is_numlike, ls_mapper, dedent,\
+flatten
from colors import colorConverter
from path import Path
from transforms import Affine2D, Bbox, TransformedPath, IdentityTransform
@@ -76,14 +77,24 @@
'--' : '_draw_dashed',
'-.' : '_draw_dash_dot',
':' : '_draw_dotted',
-'steps' : '_draw_steps_pre',
-'steps-mid' : '_draw_steps_mid',
-'steps-pre' : '_draw_steps_pre',
-'steps-post' : '_draw_steps_post',
'None' : '_draw_nothing',
' ' : '_draw_nothing',
'' : '_draw_nothing',
}
+
+_drawStyles_l = {
+'default': '_draw_lines',
+'steps-mid' : '_draw_steps_mid',
+'steps-pre' : '_draw_steps_pre',
+'steps-post' : '_draw_steps_post',
+}
+
+_drawStyles_s = {
+'steps' : '_draw_steps_pre',
+}
+drawStyles = {}
+drawStyles.update(_drawStyles_l)
+drawStyles.update(_drawStyles_s)
markers = _markers = { # hidden names deprecated
'.' : '_draw_point',
@@ -155,6 +166,7 @@
dash_joinstyle = None,
solid_joinstyle = None,
pickradius = 5,
+ drawstyle = None,
**kwargs
):
"""
@@ -185,6 +197,8 @@
if solid_capstyle is None : solid_capstyle=rcParams['lines.solid_capstyle']
if solid_joinstyle is None : solid_joinstyle=rcParams['lines.solid_joinstyle']
+if drawstyle is None : drawstyle='default'
+
self.set_dash_capstyle(dash_capstyle)
self.set_dash_joinstyle(dash_joinstyle)
self.set_solid_capstyle(solid_capstyle)
@@ -192,6 +206,7 @@
self.set_linestyle(linestyle)
+self.set_drawstyle(drawstyle)
self.set_linewidth(linewidth)
self.set_color(color)
self.set_marker(marker)
@@ -423,8 +438,10 @@
funcname = self._lineStyles.get(self._linestyle, '_draw_nothing')
if funcname != '_draw_nothing':
tpath, affine = self._transformed_path.get_transformed_path_and_affine()
-lineFunc = getattr(self, funcname)
-lineFunc(renderer, gc, tpath, affine.frozen())
+self._lineFunc = getattr(self, funcname)
+funcname = self.drawStyles.get(self._drawstyle, '_draw_lines')
+drawFunc = getattr(self, funcname)
+drawFunc(renderer, gc, tpath, affine.frozen())
if self._marker is not None:
gc = renderer.new_gc()
@@ -442,6 +459,7 @@
def get_antialiased(self): return self._antialiased
def get_color(self): return self._color
+def get_drawstyle(self): return self._drawstyle
def get_linestyle(self): return self._linestyle
def get_linewidth(self): return self._linewidth
@@ -543,6 +561,18 @@
"""
self._color = color
+def set_drawstyle(self, drawstyle):
+"""
+Set the drawstyle of the plot
+
+'default' connects the points with lines. The steps variants
+produce step-plots. 'steps' is equivalent to 'steps-pre' and
+is maintained for backward-compatibility.
+
+ACCEPTS: [ 'default' | 'steps' | 'steps-pre' | 'steps-mid' | 'steps-post' ]
+"""
+self._drawstyle = drawstyle
+
def set_linewidth(self, w):
"""
Set the line width in points
@@ -558,8 +588,20 @@
'steps' is equivalent to 'steps-pre' and is maintained for
backward-compatibility.
-ACCEPTS: [ '-' | '--' | '-.' | ':' | 'steps' | 'steps-pre' | 'steps-mid' | 'steps-post' | 'None' | ' ' | '' ]
+