Skip to content

Allow both linestyle definition "accents" and dash-patterns as linestyle #3772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/api/api_changes/2014-11-11_ELF_split_lsmapper.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Split `matplotlib.cbook.ls_mapper` in two
`````````````````````````````````````````

The `matplotlib.cbook.ls_mapper` dictionary is split into two now to
distinguish between qualified linestyle used by backends and
unqualified ones. `ls_mapper` now maps from the short symbols
(e.g. `"--"`) to qualified names (`"solid"`). The new ls_mapper_r is
the reversed mapping.

8 changes: 8 additions & 0 deletions doc/users/whats_new/linestyles.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Mostly unified linestyles for Lines, Patches and Collections
````````````````````````````````````````````````````````````

The handling of linestyles for Lines, Patches and Collections has been
unified. Now they all support defining linestyles with short symbols,
like `"--"`, as well as with full names, like `"dashed"`. Also the
definition using a dash pattern (`(0., [3., 3.])`) is supported for all
methods using Lines, Patches or Collections.
5 changes: 4 additions & 1 deletion lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -2134,7 +2134,10 @@ def unmasked_index_ranges(mask, compressed=True):
(':', 'dotted')]

ls_mapper = dict(_linestyles)
ls_mapper.update([(ls[1], ls[0]) for ls in _linestyles])
# The ls_mapper maps short codes for line style to their full name used
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be neurotic, but should this be documented in api_changes.rst?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are the maintainer. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some order in the file?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be more-or-less by date. Can you actually throw it into this folder (https://github.com/matplotlib/matplotlib/tree/master/doc/api/api_changes)? This is an attempt to alleviate the merge conflicts in the docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argh, to late I just added a note at the end of the Changes 1.4.x

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It definitely should not go there as I don't think this is going to be back-ported to the 1.4 series.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed that.

# by backends
# The reverse mapper is for mapping full names to short ones
ls_mapper_r = dict([(ls[1], ls[0]) for ls in _linestyles])


def align_iterators(func, *iterables):
Expand Down
30 changes: 23 additions & 7 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,27 +482,43 @@ def set_linestyle(self, ls):
"""
Set the linestyle(s) for the collection.

ACCEPTS: ['solid' | 'dashed', 'dashdot', 'dotted' |
(offset, on-off-dash-seq) ]
=========================== =================
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mdboom @efiring This breaks the doc-string scraping code for the tables in the docs. Should we revert the changes to put ACCEPTS : ... back in or re-work the doc-scraping?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a PR to demonstrate docscraping using numpydoc: #3859

linestyle description
=========================== =================
``'-'`` or ``'solid'`` solid line
``'--'`` or ``'dashed'`` dashed line
``'-.'`` or ``'dash_dot'`` dash-dotted line
``':'`` or ``'dotted'`` dotted line
=========================== =================

Alternatively a dash tuple of the following form can be provided::

(offset, onoffseq),

where ``onoffseq`` is an even length tuple of on and off ink
in points.

Parameters
----------
ls : { '-', '--', '-.', ':'} and more see description
The line style.
"""
try:
dashd = backend_bases.GraphicsContextBase.dashd
if cbook.is_string_like(ls):
ls = cbook.ls_mapper.get(ls, ls)
if ls in dashd:
dashes = [dashd[ls]]
elif ls in cbook.ls_mapper:
dashes = [dashd[cbook.ls_mapper[ls]]]
else:
raise ValueError()
elif cbook.iterable(ls):
try:
dashes = []
for x in ls:
if cbook.is_string_like(x):
x = cbook.ls_mapper.get(x, x)
if x in dashd:
dashes.append(dashd[x])
elif x in cbook.ls_mapper:
dashes.append(dashd[cbook.ls_mapper[x]])
else:
raise ValueError()
elif cbook.iterable(x) and len(x) == 2:
Expand All @@ -511,7 +527,7 @@ def set_linestyle(self, ls):
raise ValueError()
except ValueError:
if len(ls) == 2:
dashes = ls
dashes = [ls]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not understand this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well dashes always contains a list of dash patterns, one for each part of the collection. A dash patterns is a 2-tuple, so if you pass in a 2-tuple it is converted into a list of one 2-tuple.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this used to be broken or was this handled someplace else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It used to be broken and throw a SystemError. I added a test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, great!

else:
raise ValueError()
else:
Expand Down
81 changes: 50 additions & 31 deletions lib/matplotlib/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from matplotlib import verbose
from . import artist
from .artist import Artist
from .cbook import iterable, is_string_like, is_numlike, ls_mapper
from .cbook import iterable, is_string_like, is_numlike, ls_mapper_r
from .colors import colorConverter
from .path import Path
from .transforms import Bbox, TransformedPath, IdentityTransform
Expand Down Expand Up @@ -921,55 +921,74 @@ def set_linewidth(self, w):
"""
self._linewidth = w

def set_linestyle(self, linestyle):
def set_linestyle(self, ls):
"""
Set the linestyle of the line (also accepts drawstyles)
Set the linestyle of the line (also accepts drawstyles,
e.g., ``'steps--'``)


================ =================
linestyle description
================ =================
``'-'`` solid
``'--'`` dashed
``'-.'`` dash_dot
``':'`` dotted
``'None'`` draw nothing
``' '`` draw nothing
``''`` draw nothing
================ =================
=========================== =================
linestyle description
=========================== =================
``'-'`` or ``'solid'`` solid line
``'--'`` or ``'dashed'`` dashed line
``'-.'`` or ``'dash_dot'`` dash-dotted line
``':'`` or ``'dotted'`` dotted line
``'None'`` draw nothing
``' '`` draw nothing
``''`` draw nothing
=========================== =================

'steps' is equivalent to 'steps-pre' and is maintained for
backward-compatibility.

Alternatively a dash tuple of the following form can be provided::

(offset, onoffseq),

where ``onoffseq`` is an even length tuple of on and off ink
in points.

.. seealso::

:meth:`set_drawstyle`
To set the drawing style (stepping) of the plot.

ACCEPTS: [``'-'`` | ``'--'`` | ``'-.'`` | ``':'`` | ``'None'`` |
``' '`` | ``''``]

and any drawstyle in combination with a linestyle, e.g., ``'steps--'``.
Parameters
----------
ls : { '-', '--', '-.', ':'} and more see description
The line style.
"""
if not is_string_like(ls):
if len(ls) != 2:
raise ValueError()

self.set_dashes(ls[1])
self._linestyle = "--"
return

for ds in self.drawStyleKeys: # long names are first in the list
if linestyle.startswith(ds):
if ls.startswith(ds):
self.set_drawstyle(ds)
if len(linestyle) > len(ds):
linestyle = linestyle[len(ds):]
if len(ls) > len(ds):
ls = ls[len(ds):]
else:
linestyle = '-'
ls = '-'
break

if linestyle not in self._lineStyles:
if linestyle in ls_mapper:
linestyle = ls_mapper[linestyle]
else:
verbose.report('Unrecognized line style %s, %s' %
(linestyle, type(linestyle)))
if linestyle in [' ', '']:
linestyle = 'None'
self._linestyle = linestyle
if ls in [' ', '', 'none']:
ls = 'None'

if ls not in self._lineStyles:
try:
ls = ls_mapper_r[ls]
except KeyError:
raise ValueError(("You passed in an invalid linestyle, "
"`{}`. See "
"docs of Line2D.set_linestyle for "
"valid values.").format(ls))

self._linestyle = ls

@docstring.dedent_interpd
def set_marker(self, marker):
Expand Down
23 changes: 22 additions & 1 deletion lib/matplotlib/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,31 @@ def set_linestyle(self, ls):
"""
Set the patch linestyle

ACCEPTS: ['solid' | 'dashed' | 'dashdot' | 'dotted']
=========================== =================
linestyle description
=========================== =================
``'-'`` or ``'solid'`` solid line
``'--'`` or ``'dashed'`` dashed line
``'-.'`` or ``'dash_dot'`` dash-dotted line
``':'`` or ``'dotted'`` dotted line
=========================== =================

Alternatively a dash tuple of the following form can be provided::

(offset, onoffseq),

where ``onoffseq`` is an even length tuple of on and off ink
in points.

Parameters
----------
ls : { '-', '--', '-.', ':'} and more see description
The line style.
"""
if ls is None:
ls = "solid"

ls = cbook.ls_mapper.get(ls, ls)
self._linestyle = ls

def set_ls(self, ls):
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading