8000 Modify axis.py/set_ticklabels to fix Issue 2246; add test and compari… · matplotlib/matplotlib@11759c5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 11759c5

Browse files
committed
Modify axis.py/set_ticklabels to fix Issue 2246; add test and comparision image
1 parent 1fe5b9c commit 11759c5

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

lib/matplotlib/axis.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,8 +1521,20 @@ def set_ticklabels(self, ticklabels, *args, **kwargs):
15211521
tick locations, regardless of the state of label1On and
15221522
label2On.
15231523
1524-
ACCEPTS: sequence of strings
1525-
"""
1524+
ACCEPTS: sequence of strings or Text objects
1525+
"""
1526+
get_labels = []
1527+
for t in ticklabels:
1528+
# try calling get_text() to check whether it is Text object
1529+
# if it is Text, get label content
1530+
try:
1531+
get_labels.append(t.get_text())
1532+
# otherwise add the label to the list directly
1533+
except AttributeError:
1534+
get_labels.append(t)
1535+
# replace the ticklabels list with the processed one
1536+
ticklabels = get_labels
1537+
15261538
minor = kwargs.pop('minor', False)
15271539
if minor:
15281540
self.set_minor_formatter(mticker.FixedFormatter(ticklabels))
Loading

lib/matplotlib/tests/test_axes.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import numpy as np
1414
from numpy import ma
15+
from numpy import arange
1516

1617
import matplotlib
1718
from matplotlib.testing.decorators import image_comparison, cleanup
@@ -3476,6 +3477,28 @@ def test_pie_frame_grid():
34763477
plt.axis('equal')
34773478

34783479

3480+
@image_comparison(baseline_images=['set_get_ticklabels'], extensions=['png'])
3481+
def test_set_get_ticklabels():
3482+
# test issue 2246
3483+
fig, ax = plt.subplots(2)
3484+
ha = ['normal', 'set_x/yticklabels']
3485+
3486+
ax[0].plot(arange(10))
3487+
ax[0].set_title(ha[0])
3488+
3489+
ax[1].plot(arange(10))
3490+
ax[1].set_title(ha[1])
3491+
3492+
# set ticklabel to 1 plot in normal way
3493+
ax[0].set_xticklabels(('a', 'b', 'c', 'd'))
3494+
ax[0].set_yticklabels(('11', '12', '13', '14'))
3495+
3496+
# set ticklabel to the other plot, expect the 2 plots have same label setting
3497+
# pass get_ticklabels return value as ticklabels argument
3498+
ax[1].set_xticklabels(ax[0].get_xticklabels() )
3499+
ax[1].set_yticklabels(ax[0].get_yticklabels() )
3500+
3501+
34793502
@cleanup
34803503
def test_margins():
34813504
# test all ways margins can be called

0 commit comments

Comments
 (0)
0