Skip to content

Commit d5eb1b4

Browse files
committed
regularize processing of rcParams
1 parent 35c8d80 commit d5eb1b4

2 files changed

Lines changed: 43 additions & 34 deletions

File tree

control/freqplot.py

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def bode_plot(
227227
'freqplot', 'wrap_phase', kwargs, _freqplot_defaults, pop=True)
228228
initial_phase = config._get_param(
229229
'freqplot', 'initial_phase', kwargs, None, pop=True)
230-
freqplot_rcParams = config._get_param(
230+
rcParams = config._get_param(
231231
'freqplot', 'rcParams', kwargs, _freqplot_defaults, pop=True)
232232

233233
# Set the default labels
@@ -464,7 +464,8 @@ def bode_plot(
464464
if kw not in kwargs or kwargs[kw] is None:
465465
kwargs[kw] = config.defaults['freqplot.' + kw]
466466

467-
fig, ax_array = _process_ax_keyword(ax, (nrows, ncols), squeeze=False)
467+
fig, ax_array = _process_ax_keyword(ax, (
468+
nrows, ncols), squeeze=False, rcParams=rcParams, clear_text=True)
468469

469470
# Get the values for sharing axes limits
470471
share_magnitude = kwargs.pop('share_magnitude', None)
@@ -787,7 +788,7 @@ def _make_line_label(response, output_index, input_index):
787788
axes_title = ax.get_title()
788789
if axes_title is not None and axes_title != "":
789790
axes_title += "\n"
790-
with plt.rc_context(_freqplot_rcParams):
791+
with plt.rc_context(rcParams):
791792
ax.set_title(
792793
axes_title + f"{sysname}: "
793794
"Gm = %.2f %s(at %.2f %s), "
@@ -907,7 +908,7 @@ def gen_zero_centered_series(val_min, val_max, period):
907908
new_title = old_title + separator + new_title[common_len:]
908909

909910
# Add the title
910-
with plt.rc_context(freqplot_rcParams):
911+
with plt.rc_context(rcParams):
911912
fig.suptitle(new_title)
912913

913914
#
@@ -927,7 +928,7 @@ def gen_zero_centered_series(val_min, val_max, period):
927928
# If we have more than one column, label the individual responses
928929
if (noutputs > 1 and not overlay_outputs or ninputs > 1) \
929930
and not overlay_inputs:
930-
with plt.rc_context(_freqplot_rcParams):
931+
with plt.rc_context(rcParams):
931932
ax_array[0, j].set_title(f"From {data[0].input_labels[j]}")
932933

933934
# Label the frequency axis
@@ -973,7 +974,7 @@ def gen_zero_centered_series(val_min, val_max, period):
973974
fig.text(
974975
0.8 * xpos, ypos, f"To {data[0].output_labels[i]}\n",
975976
rotation=90, ha='left', va='center',
976-
fontsize=_freqplot_rcParams['axes.titlesize'])
977+
fontsize=rcParams['axes.titlesize'])
977978
else:
978979
# Only a single axes => add label to the left
979980
ax_array[i, 0].set_ylabel(
@@ -1024,7 +1025,7 @@ def gen_zero_centered_series(val_min, val_max, period):
10241025

10251026
# Generate the label, if needed
10261027
if len(labels) > 1 and legend_map[i, j] != None:
1027-
with plt.rc_context(freqplot_rcParams):
1028+
with plt.rc_context(rcParams):
10281029
ax.legend(lines, labels, loc=legend_map[i, j])
10291030

10301031
#
@@ -1586,6 +1587,9 @@ def nyquist_plot(
15861587
the second element is used for portions that are scaled (using
15871588
max_curve_magnitude). Default linestyle (['-', '-.']) is
15881589
determined by config.defaults['nyquist.mirror_style'].
1590+
rcParams : dict
1591+
Override the default parameters used for generating plots.
1592+
Default is set by config.default['freqplot.rcParams'].
15891593
return_contour : bool, optional
15901594
(legacy) If 'True', return the encirclement count and Nyquist
15911595
contour used to generate the Nyquist plot.
@@ -1661,6 +1665,8 @@ def nyquist_plot(
16611665
'nyquist', 'max_curve_magnitude', kwargs, _nyquist_defaults, pop=True)
16621666
max_curve_offset = config._get_param(
16631667
'nyquist', 'max_curve_offset', kwargs, _nyquist_defaults, pop=True)
1668+
rcParams = config._get_param(
1669+
'freqplot', 'rcParams', kwargs, _freqplot_defaults, pop=True)
16641670
start_marker = config._get_param(
16651671
'nyquist', 'start_marker', kwargs, _nyquist_defaults, pop=True)
16661672
start_marker_size = config._get_param(
@@ -1747,7 +1753,7 @@ def _parse_linestyle(style_name, allow_false=False):
17471753
return (counts, contours) if return_contour else counts
17481754

17491755
fig, ax = _process_ax_keyword(
1750-
ax, shape=(1, 1), squeeze=True, rcParams=_freqplot_rcParams)
1756+
ax, shape=(1, 1), squeeze=True, rcParams=rcParams)
17511757

17521758
# Create a list of lines for the output
17531759
out = np.empty(len(nyquist_responses), dtype=object)
@@ -1888,7 +1894,8 @@ def _parse_linestyle(style_name, allow_false=False):
18881894
# Add the title
18891895
if title is None:
18901896
title = "Nyquist plot for " + ", ".join(labels)
1891-
fig.suptitle(title)
1897+
with plt.rc_context(rcParams):
1898+
fig.suptitle(title)
18921899

18931900
# Legacy return pocessing
18941901
if plot is True or return_contour is not None:
@@ -2270,7 +2277,7 @@ def singular_values_plot(
22702277
'freqplot', 'Hz', kwargs, _freqplot_defaults, pop=True)
22712278
grid = config._get_param(
22722279
'freqplot', 'grid', kwargs, _freqplot_defaults, pop=True)
2273-
freqplot_rcParams = config._get_param(
2280+
rcParams = config._get_param(
22742281
'freqplot', 'rcParams', kwargs, _freqplot_defaults, pop=True)
22752282

22762283
# If argument was a singleton, turn it into a tuple
@@ -2321,7 +2328,8 @@ def singular_values_plot(
23212328
else:
23222329
return sigmas, omegas
23232330

2324-
fig, ax_sigma = _process_ax_keyword(ax, shape=(1, 1), squeeze=True)
2331+
fig, ax_sigma = _process_ax_keyword(
2332+
ax, shape=(1, 1), squeeze=True, rcParams=rcParams)
23252333
ax_sigma.set_label('control-sigma') # TODO: deprecate?
23262334

23272335
# Handle color cycle manually as all singular values
@@ -2364,14 +2372,12 @@ def singular_values_plot(
23642372

23652373
# Plot the data
23662374
if dB:
2367-
with plt.rc_context(freqplot_rcParams):
2368-
out[idx_sys] = ax_sigma.semilogx(
2369-
omega, 20 * np.log10(sigma), *fmt,
2370-
label=label, **color_arg, **kwargs)
2375+
out[idx_sys] = ax_sigma.semilogx(
2376+
omega, 20 * np.log10(sigma), *fmt,
2377+
label=label, **color_arg, **kwargs)
23712378
else:
2372-
with plt.rc_context(freqplot_rcParams):
2373-
out[idx_sys] = ax_sigma.loglog(
2374-
omega, sigma, label=label, *fmt, **color_arg, **kwargs)
2379+
out[idx_sys] = ax_sigma.loglog(
2380+
omega, sigma, label=label, *fmt, **color_arg, **kwargs)
23752381

23762382
# Plot the Nyquist frequency
23772383
if nyq_freq is not None:
@@ -2386,23 +2392,23 @@ def singular_values_plot(
23862392
# Add a grid to the plot + labeling
23872393
if grid:
23882394
ax_sigma.grid(grid, which='both')
2389-
with plt.rc_context(freqplot_rcParams):
2390-
ax_sigma.set_ylabel(
2391-
"Singular Values [dB]" if dB else "Singular Values")
2392-
ax_sigma.set_xlabel("Frequency [Hz]" if Hz else "Frequency [rad/sec]")
2395+
2396+
ax_sigma.set_ylabel(
2397+
"Singular Values [dB]" if dB else "Singular Values")
2398+
ax_sigma.set_xlabel("Frequency [Hz]" if Hz else "Frequency [rad/sec]")
23932399

23942400
# List of systems that are included in this plot
23952401
lines, labels = _get_line_labels(ax_sigma)
23962402

23972403
# Add legend if there is more than one system plotted
23982404
if len(labels) > 1 and legend_loc is not False:
2399-
with plt.rc_context(freqplot_rcParams):
2405+
with plt.rc_context(rcParams):
24002406
ax_sigma.legend(lines, labels, loc=legend_loc)
24012407

24022408
# Add the title
24032409
if title is None:
24042410
title = "Singular values for " + ", ".join(labels)
2405-
with plt.rc_context(freqplot_rcParams):
2411+
with plt.rc_context(rcParams):
24062412
fig.suptitle(title)
24072413

24082414
# Legacy return processing
@@ -2685,7 +2691,8 @@ def _process_line_labels(label, nsys, ninputs=0, noutputs=0):
26852691
return line_labels
26862692

26872693

2688-
def _process_ax_keyword(axs, shape=(1, 1), rcParams=None, squeeze=False):
2694+
def _process_ax_keyword(
2695+
axs, shape=(1, 1), rcParams=None, squeeze=False, clear_text=False):
26892696
"""Utility function to process ax keyword to plotting commands.
26902697
26912698
This function processes the `ax` keyword to plotting commands. If no
@@ -2719,6 +2726,12 @@ def _process_ax_keyword(axs, shape=(1, 1), rcParams=None, squeeze=False):
27192726
else:
27202727
# Use the existing axes, properly reshaped
27212728
axs = np.asarray(axs).reshape(*shape)
2729+
2730+
if clear_text:
2731+
# Clear out any old text from the current figure
2732+
for text in fig.texts:
2733+
text.set_visible(False) # turn off the text
2734+
del text # get rid of it completely
27222735
else:
27232736
try:
27242737
axs = np.asarray(axs).reshape(shape)

examples/steering.ipynb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@
9090
{
9191
"cell_type": "code",
9292
"execution_count": 3,
93-
"metadata": {
94-
"scrolled": false
95-
},
93+
"metadata": {},
9694
"outputs": [
9795
{
9896
"data": {
@@ -452,9 +450,7 @@
452450
{
453451
"cell_type": "code",
454452
"execution_count": 8,
455-
"metadata": {
456-
"scrolled": false
457-
},
453+
"metadata": {},
458454
"outputs": [
459455
{
460456
"name": "stdout",
@@ -1067,7 +1063,7 @@
10671063
],
10681064
"metadata": {
10691065
"kernelspec": {
1070-
"display_name": "Python 3",
1066+
"display_name": "Python 3 (ipykernel)",
10711067
"language": "python",
10721068
"name": "python3"
10731069
},
@@ -1081,9 +1077,9 @@
10811077
"name": "python",
10821078
"nbconvert_exporter": "python",
10831079
"pygments_lexer": "ipython3",
1084-
"version": "3.9.1"
1080+
"version": "3.12.2"
10851081
}
10861082
},
10871083
"nbformat": 4,
1088-
"nbformat_minor": 2
1084+
"nbformat_minor": 4
10891085
}

0 commit comments

Comments
 (0)