Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fd4cce7

Browse files
authored
Merge pull request #23170 from jklymak/enh-update-ticks
ENH: update ticks when requesting labels
2 parents 6cba8d9 + bbff015 commit fd4cce7

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
``get_ticklabels`` now always populates labels
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
Previously `.Axis.get_ticklabels` (and `.Axes.get_xticklabels`,
4+
`.Axes.get_yticklabels`) would only return empty strings unless a draw had
5+
already been performed. Now the ticks and their labels are updated when the
6+
labels are requested.

lib/matplotlib/axis.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,13 +1312,15 @@ def get_pickradius(self):
13121312

13131313
def get_majorticklabels(self):
13141314
"""Return this Axis' major tick labels, as a list of `~.text.Text`."""
1315+
self._update_ticks()
13151316
ticks = self.get_major_ticks()
13161317
labels1 = [tick.label1 for tick in ticks if tick.label1.get_visible()]
13171318
labels2 = [tick.label2 for tick in ticks if tick.label2.get_visible()]
13181319
return labels1 + labels2
13191320

13201321
def get_minorticklabels(self):
13211322
"""Return this Axis' minor tick labels, as a list of `~.text.Text`."""
1323+
self._update_ticks()
13221324
ticks = self.get_minor_ticks()
13231325
labels1 = [tick.label1 for tick in ticks if tick.label1.get_visible()]
13241326
labels2 = [tick.label2 for tick in ticks if tick.label2.get_visible()]
@@ -1341,13 +1343,6 @@ def get_ticklabels(self, minor=False, which=None):
13411343
Returns
13421344
-------
13431345
list of `~matplotlib.text.Text`
1344-
1345-
Notes
1346-
-----
1347-
The tick label strings are not populated until a ``draw`` method has
1348-
been called.
1349-
1350-
See also: `~.pyplot.draw` and `~.FigureCanvasBase.draw`.
13511346
"""
13521347
if which is not None:
13531348
if which == 'minor':

lib/matplotlib/tests/test_axes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7766,3 +7766,11 @@ def test_bezier_autoscale():
77667766
# Bottom ylim should be at the edge of the curve (-0.5), and not include
77677767
# the control point (at -1)
77687768
assert ax.get_ylim()[0] == -0.5
7769+
7770+
7771+
def test_get_xticklabel():
7772+
fig, ax = plt.subplots()
7773+
ax.plot(np.arange(10))
7774+
for ind in range(10):
7775+
assert ax.get_xticklabels()[ind].get_text() == f'{ind}'
7776+
assert ax.get_yticklabels()[ind].get_text() == f'{ind}'

0 commit comments

Comments
 (0)