|
8 | 8 | # Note: It might eventually make sense to put the functions here |
9 | 9 | # directly into timeresp.py. |
10 | 10 |
|
11 | | -import numpy as np |
| 11 | +from warnings import warn |
| 12 | + |
12 | 13 | import matplotlib as mpl |
13 | 14 | import matplotlib.pyplot as plt |
14 | | -from os.path import commonprefix |
15 | | -from warnings import warn |
| 15 | +import numpy as np |
16 | 16 |
|
17 | 17 | from . import config |
| 18 | +from .ctrlplot import _make_legend_labels, _update_suptitle |
18 | 19 |
|
19 | | -__all__ = ['time_response_plot', 'combine_time_responses', 'get_plot_axes'] |
| 20 | +__all__ = ['time_response_plot', 'combine_time_responses'] |
20 | 21 |
|
21 | 22 | # Default font dictionary |
22 | 23 | _timeplot_rcParams = mpl.rcParams.copy() |
@@ -157,7 +158,6 @@ def time_response_plot( |
157 | 158 | # |
158 | 159 | # Process keywords and set defaults |
159 | 160 | # |
160 | | - |
161 | 161 | # Set up defaults |
162 | 162 | time_label = config._get_param( |
163 | 163 | 'timeplot', 'time_label', kwargs, _timeplot_defaults, pop=True) |
@@ -597,29 +597,7 @@ def _make_line_label(signal_index, signal_labels, trace_index): |
597 | 597 | # list of systems (e.g., "Step response for sys[1], sys[2]"). |
598 | 598 | # |
599 | 599 |
|
600 | | - if fig is not None and title is not None: |
601 | | - # Get the current title, if it exists |
602 | | - old_title = None if fig._suptitle is None else fig._suptitle._text |
603 | | - new_title = title |
604 | | - |
605 | | - if old_title is not None: |
606 | | - # Find the common part of the titles |
607 | | - common_prefix = commonprefix([old_title, new_title]) |
608 | | - |
609 | | - # Back up to the last space |
610 | | - last_space = common_prefix.rfind(' ') |
611 | | - if last_space > 0: |
612 | | - common_prefix = common_prefix[:last_space] |
613 | | - common_len = len(common_prefix) |
614 | | - |
615 | | - # Add the new part of the title (usually the system name) |
616 | | - if old_title[common_len:] != new_title[common_len:]: |
617 | | - separator = ',' if len(common_prefix) > 0 else ';' |
618 | | - new_title = old_title + separator + new_title[common_len:] |
619 | | - |
620 | | - # Add the title |
621 | | - with plt.rc_context(rcParams): |
622 | | - fig.suptitle(new_title) |
| 600 | + _update_suptitle(fig, title, rcParams=rcParams) |
623 | 601 |
|
624 | 602 | return out |
625 | 603 |
|
@@ -730,62 +708,3 @@ def combine_time_responses(response_list, trace_labels=None, title=None): |
730 | 708 | return_x=base.return_x, squeeze=base.squeeze, sysname=base.sysname, |
731 | 709 | trace_labels=trace_labels, trace_types=trace_types, |
732 | 710 | plot_inputs=base.plot_inputs) |
733 | | - |
734 | | - |
735 | | -# Create vectorized function to find axes from lines |
736 | | -def get_plot_axes(line_array): |
737 | | - """Get a list of axes from an array of lines. |
738 | | -
|
739 | | - This function can be used to return the set of axes corresponding to |
740 | | - the line array that is returned by `time_response_plot`. This is useful for |
741 | | - generating an axes array that can be passed to subsequent plotting |
742 | | - calls. |
743 | | -
|
744 | | - Parameters |
745 | | - ---------- |
746 | | - line_array : array of list of Line2D |
747 | | - A 2D array with elements corresponding to a list of lines appearing |
748 | | - in an axes, matching the return type of a time response data plot. |
749 | | -
|
750 | | - Returns |
751 | | - ------- |
752 | | - axes_array : array of list of Axes |
753 | | - A 2D array with elements corresponding to the Axes assocated with |
754 | | - the lines in `line_array`. |
755 | | -
|
756 | | - Notes |
757 | | - ----- |
758 | | - Only the first element of each array entry is used to determine the axes. |
759 | | -
|
760 | | - """ |
761 | | - _get_axes = np.vectorize(lambda lines: lines[0].axes) |
762 | | - return _get_axes(line_array) |
763 | | - |
764 | | - |
765 | | -# Utility function to make legend labels |
766 | | -def _make_legend_labels(labels, ignore_common=False): |
767 | | - |
768 | | - # Look for a common prefix (up to a space) |
769 | | - common_prefix = commonprefix(labels) |
770 | | - last_space = common_prefix.rfind(', ') |
771 | | - if last_space < 0 or ignore_common: |
772 | | - common_prefix = '' |
773 | | - elif last_space > 0: |
774 | | - common_prefix = common_prefix[:last_space] |
775 | | - prefix_len = len(common_prefix) |
776 | | - |
777 | | - # Look for a common suffice (up to a space) |
778 | | - common_suffix = commonprefix( |
779 | | - [label[::-1] for label in labels])[::-1] |
780 | | - suffix_len = len(common_suffix) |
781 | | - # Only chop things off after a comma or space |
782 | | - while suffix_len > 0 and common_suffix[-suffix_len] != ',': |
783 | | - suffix_len -= 1 |
784 | | - |
785 | | - # Strip the labels of common information |
786 | | - if suffix_len > 0 and not ignore_common: |
787 | | - labels = [label[prefix_len:-suffix_len] for label in labels] |
788 | | - else: |
789 | | - labels = [label[prefix_len:] for label in labels] |
790 | | - |
791 | | - return labels |
0 commit comments