Skip to content

Use attributes Axes.x/yaxis instead of Axes.get_x/yaxis() #18709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/lines_bars_and_markers/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@
verticalalignment="bottom" if l > 0 else "top")

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

# remove y axis and spines
ax.get_yaxis().set_visible(False)
ax.yaxis.set_visible(False)
for spine in ["left", "top", "right"]:
ax.spines[spine].set_visible(False)

Expand Down
4 changes: 2 additions & 2 deletions examples/showcase/bachelors_degrees_by_gender.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@

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

fig.subplots_adjust(left=.06, right=.75, bottom=.02, top=.94)
# Limit the range of the plot to only where the data is.
Expand Down
2 changes: 1 addition & 1 deletion examples/statistics/customized_violin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def adjacent_values(vals, q1, q3):


def set_axis_style(ax, labels):
ax.get_xaxis().set_tick_params(direction='out')
ax.xaxis.set_tick_params(direction='out')
ax.xaxis.set_ticks_position('bottom')
ax.set_xticks(np.arange(1, len(labels) + 1))
ax.set_xticklabels(labels)
Expand Down
14 changes: 12 additions & 2 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1885,11 +1885,21 @@ def get_lines(self):
return cbook.silent_list('Line2D', self.lines)

def get_xaxis(self):
"""Return the XAxis instance."""
"""
Return the XAxis instance.

The use of this function is discouraged. You should instead directly
access the attribute ``ax.xaxis``.
"""
return self.xaxis

def get_yaxis(self):
"""Return the YAxis instance."""
"""
Return the YAxis instance.

The use of this function is discouraged. You should instead directly
access the attribute ``ax.yaxis``.
"""
return self.yaxis

get_xgridlines = _axis_method_wrapper("xaxis", "get_gridlines")
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/axes/_subplots.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ def label_outer(self):
if not lastrow:
for label in self.get_xticklabels(which="both"):
label.set_visible(False)
self.get_xaxis().get_offset_text().set_visible(False)
self.xaxis.get_offset_text().set_visible(False)
self.set_xlabel("")
if not firstcol:
for label in self.get_yticklabels(which="both"):
label.set_visible(False)
self.get_yaxis().get_offset_text().set_visible(False)
self.yaxis.get_offset_text().set_visible(False)
self.set_ylabel("")

def _make_twin_axes(self, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/testing/jpl_units/StrConverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def convert(value, unit, axis):

# we delay loading to make matplotlib happy
ax = axis.axes
if axis is ax.get_xaxis():
if axis is ax.xaxis:
isXAxis = True
else:
isXAxis = False
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/tests/test_bbox_tight.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ def test_noop_tight_bbox():
ax = plt.Axes(fig, [0., 0., 1., 1.])
fig.add_axes(ax)
ax.set_axis_off()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)

data = np.arange(x_size * y_size).reshape(y_size, x_size)
ax.imshow(data, rasterized=True)
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_polar.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def test_polar_units_2(fig_test, fig_ref):
plt.figure(fig_test.number)
# test {theta,r}units.
plt.polar(xs_deg, ys_km, thetaunits="rad", runits="km")
assert isinstance(plt.gca().get_xaxis().get_major_formatter(),
assert isinstance(plt.gca().xaxis.get_major_formatter(),
units.UnitDblFormatter)

ax = fig_ref.add_subplot(projection="polar")
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/tests/test_subplots.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ def check_shared(axs, x_shared, y_shared):

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

Expand Down
6 changes: 3 additions & 3 deletions lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,18 +527,18 @@ def test_unicode_minus(self, unicode_minus, result):
@pytest.mark.parametrize('left, right, offset', offset_data)
def test_offset_value(self, left, right, offset):
fig, ax = plt.subplots()
formatter = ax.get_xaxis().get_major_formatter()
formatter = ax.xaxis.get_major_formatter()

with (pytest.warns(UserWarning, match='Attempting to set identical')
if left == right else nullcontext()):
ax.set_xlim(left, right)
ax.get_xaxis()._update_ticks()
ax.xaxis._update_ticks()
assert formatter.offset == offset

with (pytest.warns(UserWarning, match='Attempting to set identical')
if left == right else nullcontext()):
ax.set_xlim(right, left)
ax.get_xaxis()._update_ticks()
ax.xaxis._update_ticks()
assert formatter.offset == offset

@pytest.mark.parametrize('use_offset', use_offset_data)
Expand Down
16 changes: 8 additions & 8 deletions lib/mpl_toolkits/tests/test_axes_grid1.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ def test_fill_facecolor():
axins = zoomed_inset_axes(ax[0], 1, loc='upper right')
axins.set_xlim(0, 0.2)
axins.set_ylim(0, 0.2)
plt.gca().axes.get_xaxis().set_ticks([])
plt.gca().axes.get_yaxis().set_ticks([])
plt.gca().axes.xaxis.set_ticks([])
plt.gca().axes.yaxis.set_ticks([])
mark_inset(ax[0], axins, loc1=2, loc2=4, fc="b", ec="0.5")

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

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

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


Expand Down