diff --git a/lib/matplotlib/artist.py b/lib/matplotlib/artist.py index d1ef2e59750e..45316346405f 100644 --- a/lib/matplotlib/artist.py +++ b/lib/matplotlib/artist.py @@ -9,6 +9,7 @@ import matplotlib import matplotlib.cbook as cbook from matplotlib import docstring, rcParams +from matplotlib import style from .transforms import Bbox, IdentityTransform, TransformedBbox, \ TransformedPath, Transform from .path import Path @@ -75,6 +76,7 @@ class Artist(object): aname = 'Artist' zorder = 0 + style = style.style_property() def __init__(self): self.figure = None @@ -108,6 +110,7 @@ def __init__(self): self._snap = None self._sketch = rcParams['path.sketch'] self._path_effects = rcParams['path.effects'] + self.style = None def __getstate__(self): d = self.__dict__.copy() diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index aa1b76635643..382e6b577808 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -739,7 +739,7 @@ def stop_filter(self, filter_func): pass -class GraphicsContextBase: +class GraphicsContextBase(object): """ An abstract base class that provides color, line styles, etc... """ @@ -860,7 +860,7 @@ def get_joinstyle(self): """ return self._joinstyle - def get_linestyle(self, style): + def get_linestyle(self): """ Return the linestyle: one of ('solid', 'dashed', 'dashdot', 'dotted'). diff --git a/lib/matplotlib/style/__init__.py b/lib/matplotlib/old_style/__init__.py similarity index 100% rename from lib/matplotlib/style/__init__.py rename to lib/matplotlib/old_style/__init__.py diff --git a/lib/matplotlib/style/core.py b/lib/matplotlib/old_style/core.py similarity index 100% rename from lib/matplotlib/style/core.py rename to lib/matplotlib/old_style/core.py diff --git a/lib/matplotlib/style/stylelib/bmh.mplstyle b/lib/matplotlib/old_style/stylelib/bmh.mplstyle similarity index 100% rename from lib/matplotlib/style/stylelib/bmh.mplstyle rename to lib/matplotlib/old_style/stylelib/bmh.mplstyle diff --git a/lib/matplotlib/style/stylelib/dark_background.mplstyle b/lib/matplotlib/old_style/stylelib/dark_background.mplstyle similarity index 100% rename from lib/matplotlib/style/stylelib/dark_background.mplstyle rename to lib/matplotlib/old_style/stylelib/dark_background.mplstyle diff --git a/lib/matplotlib/style/stylelib/fivethirtyeight.mplstyle b/lib/matplotlib/old_style/stylelib/fivethirtyeight.mplstyle similarity index 100% rename from lib/matplotlib/style/stylelib/fivethirtyeight.mplstyle rename to lib/matplotlib/old_style/stylelib/fivethirtyeight.mplstyle diff --git a/lib/matplotlib/style/stylelib/ggplot.mplstyle b/lib/matplotlib/old_style/stylelib/ggplot.mplstyle similarity index 100% rename from lib/matplotlib/style/stylelib/ggplot.mplstyle rename to lib/matplotlib/old_style/stylelib/ggplot.mplstyle diff --git a/lib/matplotlib/style/stylelib/grayscale.mplstyle b/lib/matplotlib/old_style/stylelib/grayscale.mplstyle similarity index 100% rename from lib/matplotlib/style/stylelib/grayscale.mplstyle rename to lib/matplotlib/old_style/stylelib/grayscale.mplstyle diff --git a/lib/matplotlib/style.py b/lib/matplotlib/style.py new file mode 100644 index 000000000000..79e69d423151 --- /dev/null +++ b/lib/matplotlib/style.py @@ -0,0 +1,105 @@ +from matplotlib.backend_bases import GraphicsContextBase + +def style_property(name = 'style', expected_type = Style): + """ Property for style attributes. Performs type checking """ + storage_name = '_' + name + + @property + def prop(self): + return getattr(self, storage_name) + @prop.setter + def prop(self, value): + if isinstance(value, expected_type) or value is None: + setattr(self, storage_name, value) + else: + raise TypeError('{} must be a {}'.format(name, expected_type)) + + return prop + + +class Style(GraphicsContextBase): + + _lineStyles = { + '-': 'solid', + '--': 'dashed', + '-.': 'dashdot', + ':': 'dotted'} + + def get_graylevel(self): + 'Just returns the foreground color' + return self._rgb + + alpha = property(GraphicsContextBase.get_alpha, GraphicsContextBase.set_alpha) + antialiased = property(GraphicsContextBase.get_antialiased, GraphicsContextBase.set_antialiased) + capstyle = property(GraphicsContextBase.get_capstyle, GraphicsContextBase.set_capstyle) + clip_rectangle = property(GraphicsContextBase.get_clip_rectangle, GraphicsContextBase.set_clip_rectangle) + clip_path = property(GraphicsContextBase.get_clip_path, GraphicsContextBase.set_clip_path) + graylevel = property(get_graylevel, GraphicsContextBase.set_graylevel) + joinstyle = property(GraphicsContextBase.get_joinstyle, GraphicsContextBase.set_joinstyle) + linewidth = property(GraphicsContextBase.get_linewidth, GraphicsContextBase.set_linewidth) + linestyle = property(GraphicsContextBase.get_linestyle, GraphicsContextBase.set_linestyle) + url = property(GraphicsContextBase.get_url, GraphicsContextBase.set_url) + gid = property(GraphicsContextBase.get_gid, GraphicsContextBase.set_gid) + snap = property(GraphicsContextBase.get_snap, GraphicsContextBase.set_snap) + hatch = property(GraphicsContextBase.get_hatch, GraphicsContextBase.set_hatch) + + + # Refactoring of set_dashes into two properties.. + @property + def dash_offset(self): + return self._dashes[0] + @dash_offset.setter + def dash_offset(self, value): + self._dashes[0] = value + + @property + def dashes(self): + return self._dashes[1] + + @dashes.setter + def dashes(self, value): + if value is not None: + dl = np.asarray(value) + if np.any(dl <= 0.0): + raise ValueError("All values in the dash list must be positive") + + self._dashed[1] = value + + #Color property is an alternative to 'set_foreground'. It does the same thing, but makes no allowances for providing a colour already in RGBA format.. + @property + def color(self): + return self._rgb + @color.setter + def color(self, value): + self.set_foreground(value) + + # Defining 3 properties for sketch params + @property + def sketch_scale(self): + return self._sketch[0] + @sketch_scale.setter + def sketch_scale(self, value): + self.set_sketch_params(scale = value) + + @property + def sketch_length(self): + return self._sketch[1] + @sketch_length.setter + def sketch_length(self, value): + self.set_sketch_params(length = value) + + @property + def sketch_randomness(self): + return self._sketch[2] + @sketch_randomness.setter + def sketch_randomness(self, value): + self.set_sketch_params(randomness = value) + + @classmethod + def from_dict(cls, styleDict): + """ Generate a style class from a dictionary """ + st = cls() + for key, value in styleDict.iteritems(): + setattr(st, key, value) + + return st \ No newline at end of file diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index 42a48a4af611..9a8ae7d94ff4 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -998,7 +998,7 @@ def set_fontproperties(self, fp): """ if is_string_like(fp): fp = FontProperties(fp) - self._fontproperties = fp.copy() + self._fontproperties = fp def set_font_properties(self, fp): 'alias for set_fontproperties'