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

Skip to content

Commit 5d0ca1d

Browse files
committed
Merge pull request #6170 from yongzhez/Issue5792
getter for ticks for colorbar
2 parents 0ddf0bc + 051183e commit 5d0ca1d

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lib/matplotlib/colorbar.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ def draw_all(self):
341341
Calculate any free parameters based on the current cmap and norm,
342342
and do all the drawing.
343343
'''
344+
344345
self._process_values()
345346
self._find_range()
346347
X, Y = self._mesh()
@@ -396,6 +397,10 @@ def set_ticks(self, ticks, update_ticks=True):
396397
self.update_ticks()
397398
self.stale = True
398399

400+
def get_ticks(self, minor=False):
401+
"""Return the x ticks as a list of locations"""
402+
return self._tick_data_values
403+
399404
def set_ticklabels(self, ticklabels, update_ticks=True):
400405
"""
401406
set tick labels. Tick labels are updated immediately unless
@@ -598,6 +603,7 @@ def _ticker(self):
598603
else:
599604
eps = (intv[1] - intv[0]) * 1e-10
600605
b = b[(b <= intv[1] + eps) & (b >= intv[0] - eps)]
606+
self._tick_data_values = b
601607
ticks = self._locate(b)
602608
formatter.set_locs(b)
603609
ticklabels = [formatter(t, i) for i, t in enumerate(b)]

lib/matplotlib/tests/test_colorbar.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,33 @@ def test_colorbar_ticks():
298298
assert len(cbar.ax.xaxis.get_ticklocs()) == len(clevs)
299299

300300

301+
@cleanup
302+
def test_colorbar_get_ticks():
303+
# test feature for #5792
304+
plt.figure()
305+
data = np.arange(1200).reshape(30, 40)
306+
levels = [0, 200, 400, 600, 800, 1000, 1200]
307+
308+
plt.subplot()
309+
plt.contourf(data, levels=levels)
310+
311+
# testing getter for user set ticks
312+
userTicks = plt.colorbar(ticks=[0, 600, 1200])
313+
assert userTicks.get_ticks().tolist() == [0, 600, 1200]
314+
315+
# testing for getter after calling set_ticks
316+
userTicks.set_ticks([600, 700, 800])
317+
assert userTicks.get_ticks().tolist() == [600, 700, 800]
318+
319+
# testing for getter after calling set_ticks with some ticks out of bounds
320+
userTicks.set_ticks([600, 1300, 1400, 1500])
321+
assert userTicks.get_ticks().tolist() == [600]
322+
323+
# testing getter when no ticks are assigned
324+
defTicks = plt.colorbar(orientation='horizontal')
325+
assert defTicks.get_ticks().tolist() == levels
326+
327+
301328
if __name__ == '__main__':
302329
import nose
303330
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)