Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd459569

Browse files
committed
add show_legend processing
1 parent8f0c227 commitd459569

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+
deftest_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+
assertaxs[0,0].get_legend()._loc==7# center right
602+
assertlen(axs[0,0].get_legend().get_texts())==2
603+
assertaxs[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+
assertaxs[0,0].get_legend()==None
610+
assertaxs[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+
assertaxs[0,0].get_legend()._loc==6# center left
618+
assertlen(axs[0,0].get_legend().get_texts())==2
619+
assertaxs[1,0].get_legend()._loc==7# center right
620+
assertlen(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
iflegend_loc==None:
567572
legend_loc='center right'
573+
else:
574+
show_legend=Trueifshow_legendisNoneelseshow_legend
575+
568576
iftranspose:
569577
if (overlay_signalsorplot_inputs=='overlay')andoverlay_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+
iflegend_map.shape!=ax_array.shape:
626+
raiseValueError("legend_map shape just match axes shape")
627+
628+
# Turn legend on unless overridden by user
629+
show_legend=Trueifshow_legendisNoneelseshow_legend
614630

615631
# Create axis legends
616632
foriinrange(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-
iflen(labels)>1andlegend_map[i,j]!=None:
640+
ifshow_legend!=Falseand \
641+
(len(labels)>1orshow_legend)and \
642+
legend_map[i,j]!=None:
625643
withplt.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)

[8]ページ先頭

©2009-2025 Movatter.jp