FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

ENH: update ticks when requesting labels by jklymak · Pull Request #23170 · matplotlib/matplotlib · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 6 additions & 0 deletions doc/api/next_api_changes/behavior/23170-JMK.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
``get_ticklabels`` now always populates labels
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Previously `.Axis.get_ticklabels` (and `.Axes.get_xticklabels`,
`.Axes.get_yticklabels`) would only return empty strings unless a draw had
already been performed. Now the ticks and their labels are updated when the
labels are requested.
9 changes: 2 additions & 7 deletions lib/matplotlib/axis.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1312,13 +1312,15 @@ def get_pickradius(self):

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

def get_minorticklabels(self):
"""Return this Axis' minor tick labels, as a list of `~.text.Text`."""
self._update_ticks()
ticks = self.get_minor_ticks()
labels1 = [tick.label1 for tick in ticks if tick.label1.get_visible()]
labels2 = [tick.label2 for tick in ticks if tick.label2.get_visible()]
Expand All @@ -1341,13 +1343,6 @@ def get_ticklabels(self, minor=False, which=None):
Returns
-------
list of `~matplotlib.text.Text`

Notes
-----
The tick label strings are not populated until a ``draw`` method has
been called.

See also: `~.pyplot.draw` and `~.FigureCanvasBase.draw`.
"""
if which is not None:
if which == 'minor':
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_axes.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -7766,3 +7766,11 @@ def test_bezier_autoscale():
# Bottom ylim should be at the edge of the curve (-0.5), and not include
# the control point (at -1)
assert ax.get_ylim()[0] == -0.5


def test_get_xticklabel():
fig, ax = plt.subplots()
ax.plot(np.arange(10))
for ind in range(10):
assert ax.get_xticklabels()[ind].get_text() == f'{ind}'
assert ax.get_yticklabels()[ind].get_text() == f'{ind}'

Back | FazBrowse Home | New Git URL