Skip to content

[ENH] Initial support for linestyle cycling on plot() #3818

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,7 @@ def tk_window_focus():
'matplotlib.tests.test_triangulation',
'mpl_toolkits.tests.test_mplot3d',
'matplotlib.tests.test_widgets',
'matplotlib.tests.test_cycles',
]


Expand Down
4 changes: 3 additions & 1 deletion lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,9 @@ def plot(self, *args, **kwargs):

By default, each line is assigned a different color specified by a
'color cycle'. To change this behavior, you can edit the
axes.color_cycle rcParam.
axes.color_cycle rcParam. Similarly, the default linestyle is
also specified by a 'linestyle cycle'. Edit the axes.linestyle_cycle
rcParam to specify a non-trivial cycle.

The following format string characters are accepted to control
the line style or marker:
Expand Down
17 changes: 17 additions & 0 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def __init__(self, axes, command='plot'):
self.axes = axes
self.command = command
self.set_color_cycle()
self.set_linestyle_cycle()

def __getstate__(self):
# note: it is not possible to pickle a itertools.cycle instance
Expand All @@ -148,12 +149,18 @@ def __getstate__(self):
def __setstate__(self, state):
self.__dict__ = state.copy()
self.set_color_cycle()
self.set_linestyle_cycle()

def set_color_cycle(self, clist=None):
if clist is None:
clist = rcParams['axes.color_cycle']
self.color_cycle = itertools.cycle(clist)

def set_linestyle_cycle(self, clist=None):
if clist is None:
clist = rcParams['axes.linestyle_cycle']
self.linestyle_cycle = itertools.cycle(clist)

def __call__(self, *args, **kwargs):

if self.axes.xaxis is not None and self.axes.yaxis is not None:
Expand Down Expand Up @@ -236,6 +243,8 @@ def _makeline(self, x, y, kw, kwargs):
kw['color'] = six.next(self.color_cycle)
# (can't use setdefault because it always evaluates
# its second argument)
if 'linestyle' not in kw and 'linestyle' not in kwargs:
kw['linestyle'] = six.next(self.linestyle_cycle)
seg = mlines.Line2D(x, y,
axes=self.axes,
**kw
Expand Down Expand Up @@ -955,6 +964,14 @@ def set_color_cycle(self, clist):
self._get_lines.set_color_cycle(clist)
self._get_patches_for_fill.set_color_cycle(clist)

def set_linestyle_cycle(self, clist):
"""
Set the linestyle cycle for any future plot commands on this Axes.

*clist* is a list of mpl linestyle specifiers.
"""
self._get_lines.set_linestyle_cycle(clist)

def ishold(self):
"""return the HOLD status of the axes"""
return self._hold
Expand Down
3 changes: 3 additions & 0 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,9 @@ def __call__(self, s):
'axes.color_cycle': [['b', 'g', 'r', 'c', 'm', 'y', 'k'],
validate_colorlist], # cycle of plot
# line colors
'axes.linestyle_cycle': [['-'],
validate_stringlist], # cycle of plot
# line styles
'axes.xmargin': [0, ValidateInterval(0, 1,
closedmin=True,
closedmax=True)], # margin added to xaxis
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