Skip to content

Commit caaf3b7

Browse files
authored
Merge pull request #18709 from timhoffm/xaxis-yaxis
Use attributes Axes.x/yaxis instead of Axes.get_x/yaxis()
2 parents b7eb868 + a4ab663 commit caaf3b7

File tree

11 files changed

+37
-27
lines changed

11 files changed

+37
-27
lines changed

examples/lines_bars_and_markers/timeline.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@
8383
verticalalignment="bottom" if l > 0 else "top")
8484

8585
# format xaxis with 4 month intervals
86-
ax.get_xaxis().set_major_locator(mdates.MonthLocator(interval=4))
87-
ax.get_xaxis().set_major_formatter(mdates.DateFormatter("%b %Y"))
86+
ax.xaxis.set_major_locator(mdates.MonthLocator(interval=4))
87+
ax.xaxis.set_major_formatter(mdates.DateFormatter("%b %Y"))
8888
plt.setp(ax.get_xticklabels(), rotation=30, ha="right")
8989

9090
# remove y axis and spines
91-
ax.get_yaxis().set_visible(False)
91+
ax.yaxis.set_visible(False)
9292
for spine in ["left", "top", "right"]:
9393
ax.spines[spine].set_visible(False)
9494

examples/showcase/bachelors_degrees_by_gender.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939

4040
# Ensure that the axis ticks only show up on the bottom and left of the plot.
4141
# Ticks on the right and top of the plot are generally unnecessary.
42-
ax.get_xaxis().tick_bottom()
43-
ax.get_yaxis().tick_left()
42+
ax.xaxis.tick_bottom()
43+
ax.yaxis.tick_left()
4444

4545
fig.subplots_adjust(left=.06, right=.75, bottom=.02, top=.94)
4646
# Limit the range of the plot to only where the data is.

examples/statistics/customized_violin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def adjacent_values(vals, q1, q3):
2828

2929

3030
def set_axis_style(ax, labels):
31-
ax.get_xaxis().set_tick_params(direction='out')
31+
ax.xaxis.set_tick_params(direction='out')
3232
ax.xaxis.set_ticks_position('bottom')
3333
ax.set_xticks(np.arange(1, len(labels) + 1))
3434
ax.set_xticklabels(labels)

lib/matplotlib/axes/_base.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -1885,11 +1885,21 @@ def get_lines(self):
18851885
return cbook.silent_list('Line2D', self.lines)
18861886

18871887
def get_xaxis(self):
1888-
"""Return the XAxis instance."""
1888+
"""
1889+
Return the XAxis instance.
1890+
1891+
The use of this function is discouraged. You should instead directly
1892+
access the attribute ``ax.xaxis``.
1893+
"""
18891894
return self.xaxis
18901895

18911896
def get_yaxis(self):
1892-
"""Return the YAxis instance."""
1897+
"""
1898+
Return the YAxis instance.
1899+
1900+
The use of this function is discouraged. You should instead directly
1901+
access the attribute ``ax.yaxis``.
1902+
"""
18931903
return self.yaxis
18941904

18951905
get_xgridlines = _axis_method_wrapper("xaxis", "get_gridlines")

lib/matplotlib/axes/_subplots.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ def label_outer(self):
137137
if not lastrow:
138138
for label in self.get_xticklabels(which="both"):
139139
label.set_visible(False)
140-
self.get_xaxis().get_offset_text().set_visible(False)
140+
self.xaxis.get_offset_text().set_visible(False)
141141
self.set_xlabel("")
142142
if not firstcol:
143143
for label in self.get_yticklabels(which="both"):
144144
label.set_visible(False)
145-
self.get_yaxis().get_offset_text().set_visible(False)
145+
self.yaxis.get_offset_text().set_visible(False)
146146
self.set_ylabel("")
147147

148148
def _make_twin_axes(self, *args, **kwargs):

lib/matplotlib/testing/jpl_units/StrConverter.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def convert(value, unit, axis):
3535

3636
# we delay loading to make matplotlib happy
3737
ax = axis.axes
38-
if axis is ax.get_xaxis():
38+
if axis is ax.xaxis:
3939
isXAxis = True
4040
else:
4141
isXAxis = False

lib/matplotlib/tests/test_bbox_tight.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ def test_noop_tight_bbox():
120120
ax = plt.Axes(fig, [0., 0., 1., 1.])
121121
fig.add_axes(ax)
122122
ax.set_axis_off()
123-
ax.get_xaxis().set_visible(False)
124-
ax.get_yaxis().set_visible(False)
123+
ax.xaxis.set_visible(False)
124+
ax.yaxis.set_visible(False)
125125

126126
data = np.arange(x_size * y_size).reshape(y_size, x_size)
127127
ax.imshow(data, rasterized=True)

lib/matplotlib/tests/test_polar.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def test_polar_units_2(fig_test, fig_ref):
135135
plt.figure(fig_test.number)
136136
# test {theta,r}units.
137137
plt.polar(xs_deg, ys_km, thetaunits="rad", runits="km")
138-
assert isinstance(plt.gca().get_xaxis().get_major_formatter(),
138+
assert isinstance(plt.gca().xaxis.get_major_formatter(),
139139
units.UnitDblFormatter)
140140

141141
ax = fig_ref.add_subplot(projection="polar")

lib/matplotlib/tests/test_subplots.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ def check_shared(axs, x_shared, y_shared):
2727

2828
def check_visible(axs, x_visible, y_visible):
2929
for i, (ax, vx, vy) in enumerate(zip(axs, x_visible, y_visible)):
30-
for l in ax.get_xticklabels() + [ax.get_xaxis().offsetText]:
30+
for l in ax.get_xticklabels() + [ax.xaxis.offsetText]:
3131
assert l.get_visible() == vx, \
3232
f"Visibility of x axis #{i} is incorrectly {vx}"
33-
for l in ax.get_yticklabels() + [ax.get_yaxis().offsetText]:
33+
for l in ax.get_yticklabels() + [ax.yaxis.offsetText]:
3434
assert l.get_visible() == vy, \
3535
f"Visibility of y axis #{i} is incorrectly {vy}"
3636

lib/matplotlib/tests/test_ticker.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -527,18 +527,18 @@ def test_unicode_minus(self, unicode_minus, result):
527527
@pytest.mark.parametrize('left, right, offset', offset_data)
528528
def test_offset_value(self, left, right, offset):
529529
fig, ax = plt.subplots()
530-
formatter = ax.get_xaxis().get_major_formatter()
530+
formatter = ax.xaxis.get_major_formatter()
531531

532532
with (pytest.warns(UserWarning, match='Attempting to set identical')
533533
if left == right else nullcontext()):
534534
ax.set_xlim(left, right)
535-
ax.get_xaxis()._update_ticks()
535+
ax.xaxis._update_ticks()
536536
assert formatter.offset == offset
537537

538538
with (pytest.warns(UserWarning, match='Attempting to set identical')
539539
if left == right else nullcontext()):
540540
ax.set_xlim(right, left)
541-
ax.get_xaxis()._update_ticks()
541+
ax.xaxis._update_ticks()
542542
assert formatter.offset == offset
543543

544544
@pytest.mark.parametrize('use_offset', use_offset_data)

lib/mpl_toolkits/tests/test_axes_grid1.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ def test_fill_facecolor():
259259
axins = zoomed_inset_axes(ax[0], 1, loc='upper right')
260260
axins.set_xlim(0, 0.2)
261261
axins.set_ylim(0, 0.2)
262-
plt.gca().axes.get_xaxis().set_ticks([])
263-
plt.gca().axes.get_yaxis().set_ticks([])
262+
plt.gca().axes.xaxis.set_ticks([])
263+
plt.gca().axes.yaxis.set_ticks([])
264264
mark_inset(ax[0], axins, loc1=2, loc2=4, fc="b", ec="0.5")
265265

266266
# fill with yellow by setting 'facecolor' field
@@ -276,8 +276,8 @@ def test_fill_facecolor():
276276
axins = zoomed_inset_axes(ax[1], 1, loc='upper right')
277277
axins.set_xlim(0, 0.2)
278278
axins.set_ylim(0, 0.2)
279-
plt.gca().axes.get_xaxis().set_ticks([])
280-
plt.gca().axes.get_yaxis().set_ticks([])
279+
plt.gca().axes.xaxis.set_ticks([])
280+
plt.gca().axes.yaxis.set_ticks([])
281281
mark_inset(ax[1], axins, loc1=2, loc2=4, facecolor="y", ec="0.5")
282282

283283
# fill with green by setting 'color' field
@@ -293,8 +293,8 @@ def test_fill_facecolor():
293293
axins = zoomed_inset_axes(ax[2], 1, loc='upper right')
294294
axins.set_xlim(0, 0.2)
295295
axins.set_ylim(0, 0.2)
296-
plt.gca().axes.get_xaxis().set_ticks([])
297-
plt.gca().axes.get_yaxis().set_ticks([])
296+
plt.gca().axes.xaxis.set_ticks([])
297+
plt.gca().axes.yaxis.set_ticks([])
298298
mark_inset(ax[2], axins, loc1=2, loc2=4, color="g", ec="0.5")
299299

300300
# fill with green but color won't show if set fill to False
@@ -310,8 +310,8 @@ def test_fill_facecolor():
310310
axins = zoomed_inset_axes(ax[3], 1, loc='upper right')
311311
axins.set_xlim(0, 0.2)
312312
axins.set_ylim(0, 0.2)
313-
axins.get_xaxis().set_ticks([])
314-
axins.get_yaxis().set_ticks([])
313+
axins.xaxis.set_ticks([])
314+
axins.yaxis.set_ticks([])
315315
mark_inset(ax[3], axins, loc1=2, loc2=4, fc="g", ec="0.5", fill=False)
316316

317317

0 commit comments

Comments
 (0)