55
66# Code pattern for control system plotting functions:
77#
8- # def name_plot(sysdata, plot=None, **kwargs):
8+ # def name_plot(sysdata, *fmt, plot=None, **kwargs):
99# # Process keywords and set defaults
1010# ax = kwargs.pop('ax', None)
1111# color = kwargs.pop('color', None)
3737# # Plot the data
3838# lines = np.full(ax_array.shape, [])
3939# line_labels = _process_line_labels(label, ntraces, nrows, ncols)
40+ # color_offset, color_cycle = _get_color_offset(ax)
4041# for i, j in itertools.product(range(nrows), range(ncols)):
4142# ax = ax_array[i, j]
42- # color_cycle, color_offset = _process_color_keyword(ax)
4343# for k in range(ntraces):
4444# if color is None:
45- # color = color_cycle[(k + color_offset) % len(color_cycle)]
45+ # color = _get_color(
46+ # color, fmt=fmt, offset=k, color_cycle=color_cycle)
4647# label = line_labels[k, i, j]
4748# lines[i, j] += ax.plot(data.x, data.y, color=color, label=label)
4849#
@@ -656,11 +657,78 @@ def _add_arrows_to_line2D(
656657 return arrows
657658
658659
659- def _get_color (colorspec , ax = None , lines = None , color_cycle = None ):
660+ def _get_color_offset (ax , color_cycle = None ):
661+ """Get color offset based on current lines.
662+
663+ This function determines that the current offset is for the next color
664+ to use based on current colors in a plot.
665+
666+ Parameters
667+ ----------
668+ ax : matplotlib.axes.Axes
669+ Axes containing already plotted lines.
670+ color_cycle : list of matplotlib color specs, optional
671+ Colors to use in plotting lines. Defaults to matplotlib rcParams
672+ color cycle.
673+
674+ Returns
675+ -------
676+ color_offset : matplotlib color spec
677+ Starting color for next line to be drawn.
678+ color_cycle : list of matplotlib color specs
679+ Color cycle used to determine colors.
680+
681+ """
682+ if color_cycle is None :
683+ color_cycle = plt .rcParams ['axes.prop_cycle' ].by_key ()['color' ]
684+
685+ color_offset = 0
686+ if len (ax .lines ) > 0 :
687+ last_color = ax .lines [- 1 ].get_color ()
688+ if last_color in color_cycle :
689+ color_offset = color_cycle .index (last_color ) + 1
690+
691+ return color_offset % len (color_cycle ), color_cycle
692+
693+
694+ def _get_color (
695+ colorspec , offset = None , fmt = None , ax = None , lines = None ,
696+ color_cycle = None ):
697+ """Get color to use for plotting line.
698+
699+ This function returns the color to be used for the line to be drawn (or
700+ None if the detault color cycle for the axes should be used).
701+
702+ Parameters
703+ ----------
704+ colorspec : matplotlib color specification
705+ User-specified color (or None).
706+ offset : int, optional
707+ Offset into the color cycle (for multi-trace plots).
708+ fmt : str, optional
709+ Format string passed to plotting command.
710+ ax : matplotlib.axes.Axes, optional
711+ Axes containing already plotted lines.
712+ lines : list of matplotlib.lines.Line2D, optional
713+ List of plotted lines. If not given, use ax.get_lines().
714+ color_cycle : list of matplotlib color specs, optional
715+ Colors to use in plotting lines. Defaults to matplotlib rcParams
716+ color cycle.
717+
718+ Returns
719+ -------
720+ color : matplotlib color spec
721+ Color to use for this line (or None for matplotlib default).
722+
723+ """
660724 # See if the color was explicitly specified by the user
661725 if isinstance (colorspec , dict ):
662726 if 'color' in colorspec :
663727 return colorspec .pop ('color' )
728+ elif fmt is not None and \
729+ [isinstance (arg , str ) and
730+ any ([c in arg for c in "bgrcmykw#" ]) for arg in fmt ]:
731+ return None # *fmt will set the color
664732 elif colorspec != None :
665733 return colorspec
666734
@@ -673,7 +741,9 @@ def _get_color(colorspec, ax=None, lines=None, color_cycle=None):
673741 lines = ax .lines
674742
675743 # If we were passed a set of lines, try to increment color from previous
676- if lines is not None :
744+ if offset is not None :
745+ return color_cycle [offset ]
746+ elif lines is not None :
677747 color_offset = 0
678748 if len (ax .lines ) > 0 :
679749 last_color = ax .lines [- 1 ].get_color ()
0 commit comments