Skip to content

Commit 65663ee

Browse files
committed
Adapting sgrid and zgrid for subplots
1 parent 9d3551f commit 65663ee

File tree

2 files changed

+170
-203
lines changed

2 files changed

+170
-203
lines changed

control/grid.py

+45-29
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,18 @@ def __call__(self, transform_xy, x1, y1, x2, y2):
6464
return lon_min, lon_max, lat_min, lat_max
6565

6666

67-
def sgrid(ax=None):
68-
"""Draws continuous time damping and frequency grid"""
67+
def sgrid(fig=None, position=None):
68+
"""Creates a plot axis with an s-plane (continuous-time) grid
69+
of constant damping factors and natural frequencies.
70+
"""
6971
# From matplotlib demos:
7072
# https://matplotlib.org/gallery/axisartist/demo_curvelinear_grid.html
7173
# https://matplotlib.org/gallery/axisartist/demo_floating_axis.html
7274

73-
if ax is None:
74-
ax = plt.gca()
75-
fig = ax.figure
75+
if fig is None:
76+
fig = plt.gcf()
77+
if position is None:
78+
position = (1, 1, 1)
7679

7780
# PolarAxes.PolarTransform takes radian. However, we want our coordinate
7881
# system in degree
@@ -82,19 +85,24 @@ def sgrid(ax=None):
8285
# (min, max of the coordinate within the view).
8386

8487
# 20, 20 : number of sampling points along x, y direction
85-
sampling_points = 20
86-
extreme_finder = ModifiedExtremeFinderCycle(
87-
sampling_points, sampling_points, lon_cycle=360, lat_cycle=None,
88-
lon_minmax=(90, 270), lat_minmax=(0, np.inf),)
88+
n_points = 20
89+
extreme_finder = ModifiedExtremeFinderCycle(n_points, n_points,
90+
lon_cycle=360,
91+
lat_cycle=None,
92+
lon_minmax=(90,270),
93+
lat_minmax=(0, np.inf))
8994

9095
grid_locator1 = angle_helper.LocatorDMS(15)
9196
tick_formatter1 = FormatterDMS()
92-
grid_helper = GridHelperCurveLinear(
93-
tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1,
94-
tick_formatter1=tick_formatter1)
97+
grid_helper = GridHelperCurveLinear(tr,
98+
extreme_finder=extreme_finder,
99+
grid_locator1=grid_locator1,
100+
tick_formatter1=tick_formatter1
101+
)
95102

96-
fig = plt.gcf()
97-
ax = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper)
103+
# TODO: Allow positions of type '211', '212' etc.
104+
ax = SubplotHost(fig, position[0], position[1], position[2],
105+
grid_helper=grid_helper)
98106

99107
# make ticklabels of right invisible, and top axis visible.
100108
visible = True
@@ -124,27 +132,28 @@ def sgrid(ax=None):
124132

125133
fig.add_subplot(ax)
126134

127-
# RECTANGULAR X Y AXES WITH SCALE
128-
# par2 = ax.twiny()
129-
# par2.axis["top"].toggle(all=False)
130-
# par2.axis["right"].toggle(all=False)
131-
# new_fixed_axis = par2.get_grid_helper().new_fixed_axis
132-
# par2.axis["left"] = new_fixed_axis(loc="left",
135+
### RECTANGULAR X Y AXES WITH SCALE
136+
#par2 = ax.twiny()
137+
#par2.axis["top"].toggle(all=False)
138+
#par2.axis["right"].toggle(all=False)
139+
#new_fixed_axis = par2.get_grid_helper().new_fixed_axis
140+
#par2.axis["left"] = new_fixed_axis(loc="left",
133141
# axes=par2,
134142
# offset=(0, 0))
135-
# par2.axis["bottom"] = new_fixed_axis(loc="bottom",
143+
#par2.axis["bottom"] = new_fixed_axis(loc="bottom",
136144
# axes=par2,
137145
# offset=(0, 0))
138-
# FINISH RECTANGULAR
146+
### FINISH RECTANGULAR
139147

140-
ax.grid(True, zorder=0, linestyle='dotted')
148+
ax.grid(True, zorder=0,linestyle='dotted')
141149

142150
_final_setup(ax)
143151
return ax, fig
144152

145153

146154
def _final_setup(ax, axcolor='black', axlinestyle='-',
147155
axlinewidth=1, equal=True):
156+
# Add axes labels and lines through the origin
148157
ax.set_xlabel('Real')
149158
ax.set_ylabel('Imaginary')
150159
ax.axhline(y=0, linestyle=axlinestyle, color=axcolor, lw=axlinewidth,
@@ -163,12 +172,19 @@ def nogrid(ax=None, axlinestyle='-', axlinewidth=1):
163172
return ax, fig
164173

165174

166-
def zgrid(zetas=None, wns=None, ax=None):
167-
'''Draws discrete time damping and frequency grid'''
168-
169-
if ax is None:
170-
ax = plt.gca()
171-
fig = ax.figure
175+
def zgrid(zetas=None, wns=None, fig=None, ax=None, position=None):
176+
"""Creates a plot axis with a z-plane (discrete-time) grid of
177+
constant damping factors and natural frequencies.
178+
"""
179+
if fig is None:
180+
if ax is None:
181+
ax = plt.gca()
182+
fig = ax.figure
183+
else:
184+
# Create the axis if only figure is provided
185+
if position is None:
186+
position = (1, 1, 1)
187+
ax = fig.add_subplot(position[0], position[1], position[2])
172188

173189
# Constant damping lines
174190
if zetas is None:

0 commit comments

Comments
 (0)