Skip to content

Commit 404fbdf

Browse files
committed
regularize processing of rcParams
1 parent 6f6c70d commit 404fbdf

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:
@@ -2276,7 +2283,7 @@ def singular_values_plot(
22762283
'freqplot', 'Hz', kwargs, _freqplot_defaults, pop=True)
22772284
grid = config._get_param(
22782285
'freqplot', 'grid', kwargs, _freqplot_defaults, pop=True)
2279-
freqplot_rcParams = config._get_param(
2286+
rcParams = config._get_param(
22802287
'freqplot', 'rcParams', kwargs, _freqplot_defaults, pop=True)
22812288

22822289
# If argument was a singleton, turn it into a tuple
@@ -2327,7 +2334,8 @@ def singular_values_plot(
23272334
else:
23282335
return sigmas, omegas
23292336

2330-
fig, ax_sigma = _process_ax_keyword(ax, shape=(1, 1), squeeze=True)
2337+
fig, ax_sigma = _process_ax_keyword(
2338+
ax, shape=(1, 1), squeeze=True, rcParams=rcParams)
23312339
ax_sigma.set_label('control-sigma') # TODO: deprecate?
23322340

23332341
# Handle color cycle manually as all singular values
@@ -2370,14 +2378,12 @@ def singular_values_plot(
23702378

23712379
# Plot the data
23722380
if dB:
2373-
with plt.rc_context(freqplot_rcParams):
2374-
out[idx_sys] = ax_sigma.semilogx(
2375-
omega, 20 * np.log10(sigma), *fmt,
2376-
label=label, **color_arg, **kwargs)
2381+
out[idx_sys] = ax_sigma.semilogx(
2382+
omega, 20 * np.log10(sigma), *fmt,
2383+
label=label, **color_arg, **kwargs)
23772384
else:
2378-
with plt.rc_context(freqplot_rcParams):
2379-
out[idx_sys] = ax_sigma.loglog(
2380-
omega, sigma, label=label, *fmt, **color_arg, **kwargs)
2385+
out[idx_sys] = ax_sigma.loglog(
2386+
omega, sigma, label=label, *fmt, **color_arg, **kwargs)
23812387

23822388
# Plot the Nyquist frequency
23832389
if nyq_freq is not None:
@@ -2392,23 +2398,23 @@ def singular_values_plot(
23922398
# Add a grid to the plot + labeling
23932399
if grid:
23942400
ax_sigma.grid(grid, which='both')
2395-
with plt.rc_context(freqplot_rcParams):
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]")
2401+
2402+
ax_sigma.set_ylabel(
2403+
"Singular Values [dB]" if dB else "Singular Values")
2404+
ax_sigma.set_xlabel("Frequency [Hz]" if Hz else "Frequency [rad/sec]")
23992405

24002406
# List of systems that are included in this plot
24012407
lines, labels = _get_line_labels(ax_sigma)
24022408

24032409
# Add legend if there is more than one system plotted
24042410
if len(labels) > 1 and legend_loc is not False:
2405-
with plt.rc_context(freqplot_rcParams):
2411+
with plt.rc_context(rcParams):
24062412
ax_sigma.legend(lines, labels, loc=legend_loc)
24072413

24082414
# Add the title
24092415
if title is None:
24102416
title = "Singular values for " + ", ".join(labels)
2411-
with plt.rc_context(freqplot_rcParams):
2417+
with plt.rc_context(rcParams):
24122418
fig.suptitle(title)
24132419

24142420
# Legacy return processing
@@ -2691,7 +2697,8 @@ def _process_line_labels(label, nsys, ninputs=0, noutputs=0):
26912697
return line_labels
26922698

26932699

2694-
def _process_ax_keyword(axs, shape=(1, 1), rcParams=None, squeeze=False):
2700+
def _process_ax_keyword(
2701+
axs, shape=(1, 1), rcParams=None, squeeze=False, clear_text=False):
26952702
"""Utility function to process ax keyword to plotting commands.
26962703
26972704
This function processes the `ax` keyword to plotting commands. If no
@@ -2725,6 +2732,12 @@ def _process_ax_keyword(axs, shape=(1, 1), rcParams=None, squeeze=False):
27252732
else:
27262733
# Use the existing axes, properly reshaped
27272734
axs = np.asarray(axs).reshape(*shape)
2735+
2736+
if clear_text:
2737+
# Clear out any old text from the current figure
2738+
for text in fig.texts:
2739+
text.set_visible(False) # turn off the text
2740+
del text # get rid of it completely
27282741
else:
27292742
try:
27302743
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)