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

Skip to content

Commit 76f527c

Browse files
committed
MAINT: Updating all cbar.ax calls to just cbar
This is for clarity that the colorbar is now an Axes itself.
1 parent 9e1280c commit 76f527c

File tree

10 files changed

+57
-59
lines changed

10 files changed

+57
-59
lines changed

examples/axes_grid1/demo_axes_divider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def demo_simple_image(ax):
2222

2323
im = ax.imshow(Z, extent=extent)
2424
cb = plt.colorbar(im)
25-
plt.setp(cb.ax.get_yticklabels(), visible=False)
25+
plt.setp(cb.get_yticklabels(), visible=False)
2626

2727

2828
def demo_locatable_axes_hard(fig):

examples/images_contours_and_fields/contour_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@
103103
# so let's improve its position.
104104

105105
l, b, w, h = ax.get_position().bounds
106-
ll, bb, ww, hh = CB.ax.get_position().bounds
107-
CB.ax.set_position([ll, b + 0.1*h, ww, h*0.8])
106+
ll, bb, ww, hh = CB.get_position().bounds
107+
CB.set_position([ll, b + 0.1*h, ww, h*0.8])
108108

109109
plt.show()
110110

examples/images_contours_and_fields/contourf_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
# Make a colorbar for the ContourSet returned by the contourf call.
5656
cbar = fig1.colorbar(CS)
57-
cbar.ax.set_ylabel('verbosity coefficient')
57+
cbar.set_ylabel('verbosity coefficient')
5858
# Add the contour line levels to the colorbar
5959
cbar.add_lines(CS2)
6060

examples/images_contours_and_fields/image_annotated_heatmap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def heatmap(data, row_labels, col_labels, ax=None,
131131

132132
# Create colorbar
133133
cbar = ax.figure.colorbar(im, ax=ax, **cbar_kw)
134-
cbar.ax.set_ylabel(cbarlabel, rotation=-90, va="bottom")
134+
cbar.set_ylabel(cbarlabel, rotation=-90, va="bottom")
135135

136136
# We want to show all ticks...
137137
ax.set_xticks(np.arange(data.shape[1]))

examples/ticks_and_spines/colorbar_tick_labelling_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
# Add colorbar, make sure to specify tick locations to match desired ticklabels
3131
cbar = fig.colorbar(cax, ticks=[-1, 0, 1])
32-
cbar.ax.set_yticklabels(['< -1', '0', '> 1']) # vertically oriented colorbar
32+
cbar.set_yticklabels(['< -1', '0', '> 1']) # vertically oriented colorbar
3333

3434
###############################################################################
3535
# Make plot with horizontal colorbar
@@ -42,6 +42,6 @@
4242
ax.set_title('Gaussian noise with horizontal colorbar')
4343

4444
cbar = fig.colorbar(cax, ticks=[-1, 0, 1], orientation='horizontal')
45-
cbar.ax.set_xticklabels(['Low', 'Medium', 'High']) # horizontal colorbar
45+
cbar.set_xticklabels(['Low', 'Medium', 'High']) # horizontal colorbar
4646

4747
plt.show()

lib/matplotlib/colorbar.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
Colorbars are a visualization of the mapping from scalar values to colors.
3-
In Matplotlib they are drawn into a dedicated `~.axes.Axes`.
3+
In Matplotlib they are a separate dedicated `~.axes.Axes`.
44
55
.. note::
66
Colorbars are typically created through `.Figure.colorbar` or its pyplot
@@ -233,7 +233,7 @@ class ColorbarAxes(Axes):
233233
over/under colors.
234234
235235
Users should not normally instantiate this class, but it is the class
236-
returned by ``cbar = fig.colorbar(im); cax = cbar.ax``.
236+
that the Colorbar inherits from by ``cbar = fig.colorbar(im);``.
237237
"""
238238
def __init__(self, parent, userax=True):
239239
"""
@@ -339,8 +339,6 @@ class ColorbarBase(ColorbarAxes):
339339
340340
Attributes
341341
----------
342-
ax : `~matplotlib.axes.Axes`
343-
The `~.axes.Axes` instance in which the colorbar is drawn.
344342
lines : list
345343
A list of `.LineCollection` (empty if no lines were drawn).
346344
dividers : `.LineCollection`
@@ -384,7 +382,7 @@ class ColorbarBase(ColorbarAxes):
384382
label : str
385383
386384
userax : boolean
387-
Whether the user created the axes or not. Default True
385+
Whether the user created the axes or not. Default False
388386
"""
389387

390388
n_rasterize = 50 # rasterize solids if number of colors >= n_rasterize

lib/matplotlib/tests/test_colorbar.py

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def test_colorbar_ticks():
284284
colors = ['r', 'g', 'b', 'c']
285285
cs = ax.contourf(X, Y, Z, clevs, colors=colors, extend='neither')
286286
cbar = fig.colorbar(cs, ax=ax, orientation='horizontal', ticks=clevs)
287-
assert len(cbar.ax.xaxis.get_ticklocs()) == len(clevs)
287+
assert len(cbar.xaxis.get_ticklocs()) == len(clevs)
288288

289289

290290
def test_colorbar_minorticks_on_off():
@@ -301,17 +301,17 @@ def test_colorbar_minorticks_on_off():
301301
# testing after minorticks_on()
302302
cbar.minorticks_on()
303303
np.testing.assert_almost_equal(
304-
cbar.ax.yaxis.get_minorticklocs(),
304+
cbar.yaxis.get_minorticklocs(),
305305
[-2.2, -1.8, -1.6, -1.4, -1.2, -0.8, -0.6, -0.4, -0.2,
306306
0.2, 0.4, 0.6, 0.8, 1.2, 1.4, 1.6, 1.8, 2.2, 2.4, 2.6, 2.8, 3.2])
307307
# testing after minorticks_off()
308308
cbar.minorticks_off()
309-
np.testing.assert_almost_equal(cbar.ax.yaxis.get_minorticklocs(), [])
309+
np.testing.assert_almost_equal(cbar.yaxis.get_minorticklocs(), [])
310310

311311
im.set_clim(vmin=-1.2, vmax=1.2)
312312
cbar.minorticks_on()
313313
np.testing.assert_almost_equal(
314-
cbar.ax.yaxis.get_minorticklocs(),
314+
cbar.yaxis.get_minorticklocs(),
315315
[-1.1, -0.9, -0.8, -0.7, -0.6, -0.4, -0.3, -0.2, -0.1,
316316
0.1, 0.2, 0.3, 0.4, 0.6, 0.7, 0.8, 0.9, 1.1, 1.2, 1.3])
317317

@@ -322,20 +322,20 @@ def test_colorbar_minorticks_on_off():
322322
im = ax.pcolormesh(data, norm=LogNorm())
323323
cbar = fig.colorbar(im)
324324
fig.canvas.draw()
325-
default_minorticklocks = cbar.ax.yaxis.get_minorticklocs()
325+
default_minorticklocks = cbar.yaxis.get_minorticklocs()
326326
# test that minorticks turn off for LogNorm
327327
cbar.minorticks_off()
328-
np.testing.assert_equal(cbar.ax.yaxis.get_minorticklocs(), [])
328+
np.testing.assert_equal(cbar.yaxis.get_minorticklocs(), [])
329329

330330
# test that minorticks turn back on for LogNorm
331331
cbar.minorticks_on()
332-
np.testing.assert_equal(cbar.ax.yaxis.get_minorticklocs(),
332+
np.testing.assert_equal(cbar.yaxis.get_minorticklocs(),
333333
default_minorticklocks)
334334

335335
# test issue #13339: minorticks for LogNorm should stay off
336336
cbar.minorticks_off()
337337
cbar.set_ticks([3, 5, 7, 9])
338-
np.testing.assert_equal(cbar.ax.yaxis.get_minorticklocs(), [])
338+
np.testing.assert_equal(cbar.yaxis.get_minorticklocs(), [])
339339

340340

341341
def test_cbar_minorticks_for_rc_xyminortickvisible():
@@ -358,12 +358,12 @@ def test_cbar_minorticks_for_rc_xyminortickvisible():
358358
im = ax.pcolormesh([[1, 2]], vmin=vmin, vmax=vmax)
359359

360360
cbar = fig.colorbar(im, extend='both', orientation='vertical')
361-
assert cbar.ax.yaxis.get_minorticklocs()[0] >= vmin
362-
assert cbar.ax.yaxis.get_minorticklocs()[-1] <= vmax
361+
assert cbar.yaxis.get_minorticklocs()[0] >= vmin
362+
assert cbar.yaxis.get_minorticklocs()[-1] <= vmax
363363

364364
cbar = fig.colorbar(im, extend='both', orientation='horizontal')
365-
assert cbar.ax.xaxis.get_minorticklocs()[0] >= vmin
366-
assert cbar.ax.xaxis.get_minorticklocs()[-1] <= vmax
365+
assert cbar.xaxis.get_minorticklocs()[0] >= vmin
366+
assert cbar.xaxis.get_minorticklocs()[-1] <= vmax
367367

368368

369369
def test_colorbar_autoticks():
@@ -384,7 +384,7 @@ def test_colorbar_autoticks():
384384
cbar2 = fig.colorbar(pcm, ax=ax[1], extend='both',
385385
orientation='vertical', shrink=0.4)
386386
# note only -10 to 10 are visible,
387-
np.testing.assert_almost_equal(cbar.ax.yaxis.get_ticklocs(),
387+
np.testing.assert_almost_equal(cbar.yaxis.get_ticklocs(),
388388
np.arange(-15, 16, 5))
389389
# note only -10 to 10 are visible
390390
np.testing.assert_almost_equal(cbar2.ax.yaxis.get_ticklocs(),
@@ -408,7 +408,7 @@ def test_colorbar_autotickslog():
408408
cbar2 = fig.colorbar(pcm, ax=ax[1], extend='both',
409409
orientation='vertical', shrink=0.4)
410410
# note only -12 to +12 are visible
411-
np.testing.assert_almost_equal(cbar.ax.yaxis.get_ticklocs(),
411+
np.testing.assert_almost_equal(cbar.yaxis.get_ticklocs(),
412412
10**np.arange(-16., 16.2, 4.))
413413
# note only -24 to +24 are visible
414414
np.testing.assert_almost_equal(cbar2.ax.yaxis.get_ticklocs(),
@@ -488,7 +488,7 @@ def test_colorbar_log_minortick_labels():
488488
pcm = ax.imshow([[10000, 50000]], norm=LogNorm())
489489
cb = fig.colorbar(pcm)
490490
fig.canvas.draw()
491-
lb = [l.get_text() for l in cb.ax.yaxis.get_ticklabels(which='both')]
491+
lb = [l.get_text() for l in cb.yaxis.get_ticklabels(which='both')]
492492
expected = [r'$\mathdefault{10^{4}}$',
493493
r'$\mathdefault{2\times10^{4}}$',
494494
r'$\mathdefault{3\times10^{4}}$',
@@ -504,21 +504,21 @@ def test_colorbar_renorm():
504504
fig, ax = plt.subplots()
505505
im = ax.imshow(z)
506506
cbar = fig.colorbar(im)
507-
np.testing.assert_allclose(cbar.ax.yaxis.get_majorticklocs(),
507+
np.testing.assert_allclose(cbar.yaxis.get_majorticklocs(),
508508
np.arange(0, 120000.1, 20000))
509509

510510
cbar.set_ticks([1, 2, 3])
511511
assert isinstance(cbar.locator, FixedLocator)
512512

513513
norm = LogNorm(z.min(), z.max())
514514
im.set_norm(norm)
515-
np.testing.assert_allclose(cbar.ax.yaxis.get_majorticklocs(),
515+
np.testing.assert_allclose(cbar.yaxis.get_majorticklocs(),
516516
np.logspace(-10, 7, 18))
517517
# note that set_norm removes the FixedLocator...
518518
assert np.isclose(cbar.vmin, z.min())
519519
cbar.set_ticks([1, 2, 3])
520520
assert isinstance(cbar.locator, FixedLocator)
521-
np.testing.assert_allclose(cbar.ax.yaxis.get_majorticklocs(),
521+
np.testing.assert_allclose(cbar.yaxis.get_majorticklocs(),
522522
[1.0, 2.0, 3.0])
523523

524524
norm = LogNorm(z.min() * 1000, z.max() * 1000)
@@ -536,18 +536,18 @@ def test_colorbar_format():
536536
im = ax.imshow(z)
537537
cbar = fig.colorbar(im, format='%4.2e')
538538
fig.canvas.draw()
539-
assert cbar.ax.yaxis.get_ticklabels()[4].get_text() == '8.00e+04'
539+
assert cbar.yaxis.get_ticklabels()[4].get_text() == '8.00e+04'
540540

541541
# make sure that if we change the clim of the mappable that the
542542
# formatting is *not* lost:
543543
im.set_clim([4, 200])
544544
fig.canvas.draw()
545-
assert cbar.ax.yaxis.get_ticklabels()[4].get_text() == '2.00e+02'
545+
assert cbar.yaxis.get_ticklabels()[4].get_text() == '2.00e+02'
546546

547547
# but if we change the norm:
548548
im.set_norm(LogNorm(vmin=0.1, vmax=10))
549549
fig.canvas.draw()
550-
assert (cbar.ax.yaxis.get_ticklabels()[0].get_text() ==
550+
assert (cbar.yaxis.get_ticklabels()[0].get_text() ==
551551
r'$\mathdefault{10^{-2}}$')
552552

553553

@@ -559,12 +559,12 @@ def test_colorbar_scale_reset():
559559
pcm = ax.pcolormesh(z, cmap='RdBu_r', rasterized=True)
560560
cbar = fig.colorbar(pcm, ax=ax)
561561
cbar.outline.set_edgecolor('red')
562-
assert cbar.ax.yaxis.get_scale() == 'linear'
562+
assert cbar.yaxis.get_scale() == 'linear'
563563

564564
pcm.set_norm(LogNorm(vmin=1, vmax=100))
565-
assert cbar.ax.yaxis.get_scale() == 'log'
565+
assert cbar.yaxis.get_scale() == 'log'
566566
pcm.set_norm(Normalize(vmin=-20, vmax=20))
567-
assert cbar.ax.yaxis.get_scale() == 'linear'
567+
assert cbar.yaxis.get_scale() == 'linear'
568568

569569
assert cbar.outline.get_edgecolor() == mcolors.to_rgba('red')
570570

@@ -583,7 +583,7 @@ def test_colorbar_inverted_ticks():
583583
pc = ax.pcolormesh(10**np.arange(1, 5).reshape(2, 2), norm=LogNorm())
584584
cbar = fig.colorbar(pc, ax=ax, extend='both')
585585
ticks = cbar.get_ticks()
586-
cbar.ax.invert_yaxis()
586+
cbar.invert_yaxis()
587587
np.testing.assert_allclose(ticks, cbar.get_ticks())
588588

589589
ax = axs[1]
@@ -592,7 +592,7 @@ def test_colorbar_inverted_ticks():
592592
cbar.minorticks_on()
593593
ticks = cbar.get_ticks()
594594
minorticks = cbar.get_ticks(minor=True)
595-
cbar.ax.invert_yaxis()
595+
cbar.invert_yaxis()
596596
np.testing.assert_allclose(ticks, cbar.get_ticks())
597597
np.testing.assert_allclose(minorticks, cbar.get_ticks(minor=True))
598598

@@ -603,7 +603,7 @@ def test_extend_colorbar_customnorm():
603603
fig, (ax0, ax1) = plt.subplots(2, 1)
604604
pcm = ax0.pcolormesh([[0]], norm=TwoSlopeNorm(vcenter=0., vmin=-2, vmax=1))
605605
cb = fig.colorbar(pcm, ax=ax0, extend='both')
606-
np.testing.assert_allclose(cb.ax.get_position().extents,
606+
np.testing.assert_allclose(cb.get_position().extents,
607607
[0.78375, 0.536364, 0.796147, 0.9], rtol=2e-3)
608608

609609

@@ -623,11 +623,11 @@ def test_colorbar_label():
623623
fig, ax = plt.subplots()
624624
im = ax.imshow([[1, 2], [3, 4]])
625625
cbar = fig.colorbar(im, label='cbar')
626-
assert cbar.ax.get_ylabel() == 'cbar'
626+
assert cbar.get_ylabel() == 'cbar'
627627
cbar.set_label(None)
628-
assert cbar.ax.get_ylabel() == ''
628+
assert cbar.get_ylabel() == ''
629629
cbar.set_label('cbar 2')
630-
assert cbar.ax.get_ylabel() == 'cbar 2'
630+
assert cbar.get_ylabel() == 'cbar 2'
631631

632632
cbar2 = fig.colorbar(im, label=None)
633633
assert cbar2.ax.get_ylabel() == ''
@@ -663,7 +663,7 @@ def test_anchored_cbar_position_using_specgrid():
663663
# the top right corner of one ax is (x1, y1)
664664
# p0: the vertical / horizontal position of anchor
665665
x0, y0, x1, y1 = ax.get_position().extents
666-
cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents
666+
cx0, cy0, cx1, cy1 = cbar.get_position().extents
667667
p0 = (y1 - y0) * anchor_y + y0
668668

669669
np.testing.assert_allclose(
@@ -681,7 +681,7 @@ def test_anchored_cbar_position_using_specgrid():
681681
# the top right corner of one ax is (x1, y1)
682682
# p0: the vertical / horizontal position of anchor
683683
x0, y0, x1, y1 = ax.get_position().extents
684-
cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents
684+
cx0, cy0, cx1, cy1 = cbar.get_position().extents
685685
p0 = (y1 - y0) * anchor_y + y0
686686

687687
np.testing.assert_allclose(
@@ -701,7 +701,7 @@ def test_anchored_cbar_position_using_specgrid():
701701
# the top right corner of one ax is (x1, y1)
702702
# p0: the vertical / horizontal position of anchor
703703
x0, y0, x1, y1 = ax.get_position().extents
704-
cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents
704+
cx0, cy0, cx1, cy1 = cbar.get_position().extents
705705
p0 = (x1 - x0) * anchor_x + x0
706706

707707
np.testing.assert_allclose(
@@ -721,7 +721,7 @@ def test_anchored_cbar_position_using_specgrid():
721721
# the top right corner of one ax is (x1, y1)
722722
# p0: the vertical / horizontal position of anchor
723723
x0, y0, x1, y1 = ax.get_position().extents
724-
cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents
724+
cx0, cy0, cx1, cy1 = cbar.get_position().extents
725725
p0 = (x1 - x0) * anchor_x + x0
726726

727727
np.testing.assert_allclose(
@@ -735,16 +735,16 @@ def test_colorbar_change_lim_scale():
735735
fig, ax = plt.subplots(1, 2, constrained_layout=True)
736736
pc = ax[0].pcolormesh(np.arange(100).reshape(10, 10)+1)
737737
cb = fig.colorbar(pc, ax=ax[0], extend='both')
738-
cb.ax.set_yscale('log')
738+
cb.set_yscale('log')
739739

740740
pc = ax[1].pcolormesh(np.arange(100).reshape(10, 10)+1)
741741
cb = fig.colorbar(pc, ax=ax[1], extend='both')
742-
cb.ax.set_ylim([20, 90])
742+
cb.set_ylim([20, 90])
743743

744744

745745
@check_figures_equal(extensions=["png"])
746746
def test_axes_handles_same_functions(fig_ref, fig_test):
747-
# prove that cax and cb.ax are functionally the same
747+
# prove that cax and cb are functionally the same
748748
for nn, fig in enumerate([fig_ref, fig_test]):
749749
ax = fig.add_subplot()
750750
pc = ax.pcolormesh(np.ones(300).reshape(10, 30))
@@ -753,7 +753,7 @@ def test_axes_handles_same_functions(fig_ref, fig_test):
753753
if nn == 0:
754754
caxx = cax
755755
else:
756-
caxx = cb.ax
756+
caxx = cb
757757
caxx.set_yticks(np.arange(20))
758758
caxx.set_yscale('log')
759759
caxx.set_position([0.92, 0.1, 0.02, 0.7])
@@ -768,6 +768,6 @@ def test_inset_colorbar_layout():
768768
fig.draw_no_output()
769769
# make sure this is in the figure. In the colorbar swapping
770770
# it was being dropped from the list of children...
771-
np.testing.assert_allclose(cb.ax.get_position().bounds,
771+
np.testing.assert_allclose(cb.get_position().bounds,
772772
[0.87, 0.342, 0.0237, 0.315], atol=0.01)
773-
assert cb.ax.outer_ax in ax.child_axes
773+
assert cb.outer_ax in ax.child_axes

lib/matplotlib/tests/test_constrainedlayout.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,10 @@ def test_colorbar_align():
422422
cb = fig.colorbar(pc, ax=ax, location=location, shrink=0.6,
423423
pad=0.04)
424424
cbs += [cb]
425-
cb.ax.tick_params(direction='in')
425+
cb.tick_params(direction='in')
426426
if nn != 1:
427-
cb.ax.xaxis.set_ticks([])
428-
cb.ax.yaxis.set_ticks([])
427+
cb.xaxis.set_ticks([])
428+
cb.yaxis.set_ticks([])
429429
ax.set_xticklabels('')
430430
ax.set_yticklabels('')
431431
fig.set_constrained_layout_pads(w_pad=4 / 72, h_pad=4 / 72, hspace=0.1,

0 commit comments

Comments
 (0)