Skip to content

Commit

Permalink
Merge pull request #4141 from MirandaXM/2246
Browse files Browse the repository at this point in the history
ENH : set_ticklabels() can take list of Text objects as input

fixes #2246
  • Loading branch information
tacaswell committed Feb 22, 2015
2 parents 1fe5b9c + 11759c5 commit c412e2e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1521,8 +1521,20 @@ def set_ticklabels(self, ticklabels, *args, **kwargs):
tick locations, regardless of the state of label1On and
label2On.
ACCEPTS: sequence of strings
"""
ACCEPTS: sequence of strings or Text objects
"""
get_labels = []
for t in ticklabels:
# try calling get_text() to check whether it is Text object
# if it is Text, get label content
try:
get_labels.append(t.get_text())
# otherwise add the label to the list directly
except AttributeError:
get_labels.append(t)
# replace the ticklabels list with the processed one
ticklabels = get_labels

minor = kwargs.pop('minor', False)
if minor:
self.set_minor_formatter(mticker.FixedFormatter(ticklabels))
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import numpy as np
from numpy import ma
from numpy import arange

import matplotlib
from matplotlib.testing.decorators import image_comparison, cleanup
Expand Down Expand Up @@ -3476,6 +3477,28 @@ def test_pie_frame_grid():
plt.axis('equal')


@image_comparison(baseline_images=['set_get_ticklabels'], extensions=['png'])
def test_set_get_ticklabels():
# test issue 2246
fig, ax = plt.subplots(2)
ha = ['normal', 'set_x/yticklabels']

ax[0].plot(arange(10))
ax[0].set_title(ha[0])

ax[1].plot(arange(10))
ax[1].set_title(ha[1])

# set ticklabel to 1 plot in normal way
ax[0].set_xticklabels(('a', 'b', 'c', 'd'))
ax[0].set_yticklabels(('11', '12', '13', '14'))

# set ticklabel to the other plot, expect the 2 plots have same label setting
# pass get_ticklabels return value as ticklabels argument
ax[1].set_xticklabels(ax[0].get_xticklabels() )
ax[1].set_yticklabels(ax[0].get_yticklabels() )


@cleanup
def test_margins():
# test all ways margins can be called
Expand Down

0 comments on commit c412e2e

Please sign in to comment.