10000 add show_legend processing · python-control/python-control@d459569 · GitHub
[go: up one dir, main page]

Skip to content

Commit d459569

Browse files
committed
add show_legend processing
1 parent 8f0c227 commit d459569

File tree

4 files changed

+82
-32
lines changed

4 files changed

+82
-32
lines changed

control/tests/timeplot_test.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,39 @@ def test_errors():
588588
match="(has no property|unexpected keyword)"):
589589
stepresp.plot(unknown=None)
590590

591+
592+
def test_legend_customization():
593+
sys = ct.rss(4, 2, 1, name='sys')
594+
timepts = np.linspace(0, 10)
595+
U = np.sin(timepts)
596+
resp = ct.input_output_response(sys, timepts, U)
597+
598+
# Generic input/output plot
599+
out = resp.plot(overlay_signals=True)
600+
axs = ct.get_plot_axes(out)
601+
assert axs[0, 0].get_legend()._loc == 7 # center right
602+
assert len(axs[0, 0].get_legend().get_texts()) == 2
603+
assert axs[1, 0].get_legend() == None
604+
plt.close()
605+
606+
# Hide legend
607+
out = resp.plot(overlay_signals=True, show_legend=False)
608+
axs = ct.get_plot_axes(out)
609+
assert axs[0, 0].get_legend() == None
610+
assert axs[1, 0].get_legend() == None
611+
plt.close()
612+
613+
# Put legend in both axes
614+
out = resp.plot(
615+
overlay_signals=True, legend_map=[['center left'], ['center right']])
616+
axs = ct.get_plot_axes(out)
617+
assert axs[0, 0].get_legend()._loc == 6 # center left
618+
assert len(axs[0, 0].get_legend().get_texts()) == 2
619+
assert axs[1, 0].get_legend()._loc == 7 # center right
620+
assert len(axs[1, 0].get_legend().get_texts()) == 1
621+
plt.close()
622+
623+
591624
if __name__ == "__main__":
592625
#
593626
# Interactive mode: generate plots for manual viewing

control/timeplot.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def time_response_plot(
5050
data, *fmt, ax=None, plot_inputs=None, plot_outputs=True,
5151
transpose=False, overlay_traces=False, overlay_signals=False,
5252
legend_map=None, legend_loc=None, add_initial_zero=True, label=None,
53-
trace_labels=None, title=None, relabel=True, **kwargs):
53+
trace_labels=None, title=None, relabel=True, show_legend=None,
54+
**kwargs):
5455
"""Plot the time response of an input/output system.
5556
5657
This function creates a standard set of plots for the input/output
@@ -131,6 +132,10 @@ def time_response_plot(
131132
relabel : bool, optional
132133
By default, existing figures and axes are relabeled when new data
133134
are added. If set to `False`, just plot new data on existing axes.
135+
show_legend : bool, optional
136+
Force legend to be shown if ``True`` or hidden if ``False``. If
137+
``None``, then show legend when there is more than one line on an
138+
axis or ``legend_loc`` or ``legend_map`` have been specified.
134139
time_label : str, optional
135140
Label to use for the time axis.
136141
trace_props : array of dicts
@@ -565,6 +570,9 @@ def _make_line_label(signal_index, signal_labels, trace_index):
565570
legend_map = np.full(ax_array.shape, None, dtype=object)
566571
if legend_loc == None:
567572
legend_loc = 'center right'
573+
else:
574+
show_legend = True if show_legend is None else show_legend
575+
568576
if transpose:
569577
if (overlay_signals or plot_inputs == 'overlay') and overlay_traces:
570578
# Put a legend in each plot for inputs and outputs
@@ -611,6 +619,14 @@ def _make_line_label(signal_index, signal_labels, trace_index):
611619
else:
612620
# Put legend in the upper right
613621
legend_map[0, -1] = legend_loc
622+
else:
623+
# Make sure the legend map is the right size
624+
legend_map = np.atleast_2d(legend_map)
625+
if legend_map.shape != ax_array.shape:
626+
raise ValueError("legend_map shape just match axes shape")
627+
628+
# Turn legend on unless overridden by user
629+
show_legend = True if show_legend is None else show_legend
614630

615631
# Create axis legends
616632
for i in range(nrows):
@@ -621,7 +637,9 @@ def _make_line_label(signal_index, signal_labels, trace_index):
621637
labels = _make_legend_labels(labels, plot_inputs == 'overlay')
622638

623639
# Update the labels to remove common strings
624-
if len(labels) > 1 and legend_map[i, j] != None:
640+
if show_legend != False and \
641+
(len(labels) > 1 or show_legend) and \
642+
legend_map[i, j] != None:
625643
with plt.rc_context(rcParams):
626644
ax.legend(labels, loc=legend_map[i, j])
627645

examples/cds110_lti-systems.ipynb

Lines changed: 29 additions & 30 deletions
Large diffs are not rendered by default.

examples/springmass-coupled.png

57 KB
Loading

0 commit comments

Comments
 (0)
0