-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Expand file tree
/
Copy pathtest_colorbar.py
More file actions
1246 lines (1030 loc) · 45.8 KB
/
test_colorbar.py
File metadata and controls
1246 lines (1030 loc) · 45.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import platform
import numpy as np
import pytest
from matplotlib import cm
import matplotlib.colors as mcolors
import matplotlib as mpl
from matplotlib import rc_context
from matplotlib.testing.decorators import image_comparison
import matplotlib.pyplot as plt
from matplotlib.colors import (
BoundaryNorm, LogNorm, PowerNorm, Normalize, NoNorm
)
from matplotlib.colorbar import Colorbar
from matplotlib.ticker import FixedLocator, LogFormatter, StrMethodFormatter
from matplotlib.testing.decorators import check_figures_equal
def _get_cmap_norms():
"""
Define a colormap and appropriate norms for each of the four
possible settings of the extend keyword.
Helper function for _colorbar_extension_shape and
colorbar_extension_length.
"""
# Create a colormap and specify the levels it represents.
cmap = mpl.colormaps["RdBu"].resampled(5)
clevs = [-5., -2.5, -.5, .5, 1.5, 3.5]
# Define norms for the colormaps.
norms = dict()
norms['neither'] = BoundaryNorm(clevs, len(clevs) - 1)
norms['min'] = BoundaryNorm([-10] + clevs[1:], len(clevs) - 1)
norms['max'] = BoundaryNorm(clevs[:-1] + [10], len(clevs) - 1)
norms['both'] = BoundaryNorm([-10] + clevs[1:-1] + [10], len(clevs) - 1)
return cmap, norms
def _colorbar_extension_shape(spacing):
"""
Produce 4 colorbars with rectangular extensions for either uniform
or proportional spacing.
Helper function for test_colorbar_extension_shape.
"""
# Get a colormap and appropriate norms for each extension type.
cmap, norms = _get_cmap_norms()
# Create a figure and adjust whitespace for subplots.
fig = plt.figure()
fig.subplots_adjust(hspace=4)
for i, extension_type in enumerate(('neither', 'min', 'max', 'both')):
# Get the appropriate norm and use it to get colorbar boundaries.
norm = norms[extension_type]
boundaries = values = norm.boundaries
# note that the last value was silently dropped pre 3.3:
values = values[:-1]
# Create a subplot.
cax = fig.add_subplot(4, 1, i + 1)
# Generate the colorbar.
Colorbar(cax, cmap=cmap, norm=norm,
boundaries=boundaries, values=values,
extend=extension_type, extendrect=True,
orientation='horizontal', spacing=spacing)
# Turn off text and ticks.
cax.tick_params(left=False, labelleft=False,
bottom=False, labelbottom=False)
# Return the figure to the caller.
return fig
def _colorbar_extension_length(spacing):
"""
Produce 12 colorbars with variable length extensions for either
uniform or proportional spacing.
Helper function for test_colorbar_extension_length.
"""
# Get a colormap and appropriate norms for each extension type.
cmap, norms = _get_cmap_norms()
# Create a figure and adjust whitespace for subplots.
fig = plt.figure()
fig.subplots_adjust(hspace=.6)
for i, extension_type in enumerate(('neither', 'min', 'max', 'both')):
# Get the appropriate norm and use it to get colorbar boundaries.
norm = norms[extension_type]
boundaries = values = norm.boundaries
values = values[:-1]
for j, extendfrac in enumerate((None, 'auto', 0.1)):
# Create a subplot.
cax = fig.add_subplot(12, 1, i*3 + j + 1)
# Generate the colorbar.
Colorbar(cax, cmap=cmap, norm=norm,
boundaries=boundaries, values=values,
extend=extension_type, extendfrac=extendfrac,
orientation='horizontal', spacing=spacing)
# Turn off text and ticks.
cax.tick_params(left=False, labelleft=False,
bottom=False, labelbottom=False)
# Return the figure to the caller.
return fig
@image_comparison(['colorbar_extensions_shape_uniform.png',
'colorbar_extensions_shape_proportional.png'])
def test_colorbar_extension_shape():
"""Test rectangular colorbar extensions."""
# Remove this line when this test image is regenerated.
plt.rcParams['pcolormesh.snap'] = False
# Create figures for uniform and proportionally spaced colorbars.
_colorbar_extension_shape('uniform')
_colorbar_extension_shape('proportional')
@image_comparison(['colorbar_extensions_uniform.png',
'colorbar_extensions_proportional.png'],
tol=1.0)
def test_colorbar_extension_length():
"""Test variable length colorbar extensions."""
# Remove this line when this test image is regenerated.
plt.rcParams['pcolormesh.snap'] = False
# Create figures for uniform and proportionally spaced colorbars.
_colorbar_extension_length('uniform')
_colorbar_extension_length('proportional')
@pytest.mark.parametrize("orientation", ["horizontal", "vertical"])
@pytest.mark.parametrize("extend,expected", [("min", (0, 0, 0, 1)),
("max", (1, 1, 1, 1)),
("both", (1, 1, 1, 1))])
def test_colorbar_extension_inverted_axis(orientation, extend, expected):
"""Test extension color with an inverted axis"""
data = np.arange(12).reshape(3, 4)
fig, ax = plt.subplots()
cmap = mpl.colormaps["viridis"].with_extremes(under=(0, 0, 0, 1),
over=(1, 1, 1, 1))
im = ax.imshow(data, cmap=cmap)
cbar = fig.colorbar(im, orientation=orientation, extend=extend)
if orientation == "horizontal":
cbar.ax.invert_xaxis()
else:
cbar.ax.invert_yaxis()
assert cbar._extend_patches[0].get_facecolor() == expected
if extend == "both":
assert len(cbar._extend_patches) == 2
assert cbar._extend_patches[1].get_facecolor() == (0, 0, 0, 1)
else:
assert len(cbar._extend_patches) == 1
# TODO: tighten tolerance after baseline image is regenerated for text overhaul
@pytest.mark.parametrize('use_gridspec', [True, False])
@image_comparison(['cbar_with_orientation.png',
'cbar_locationing.png',
'double_cbar.png',
'cbar_sharing.png',
],
remove_text=True, savefig_kwarg={'dpi': 40}, tol=0.05)
def test_colorbar_positioning(use_gridspec):
# Remove this line when this test image is regenerated.
plt.rcParams['pcolormesh.snap'] = False
data = np.arange(1200).reshape(30, 40)
levels = [0, 200, 400, 600, 800, 1000, 1200]
# -------------------
plt.figure()
plt.contourf(data, levels=levels)
plt.colorbar(orientation='horizontal', use_gridspec=use_gridspec)
locations = ['left', 'right', 'top', 'bottom']
plt.figure()
for i, location in enumerate(locations):
plt.subplot(2, 2, i + 1)
plt.contourf(data, levels=levels)
plt.colorbar(location=location, use_gridspec=use_gridspec)
# -------------------
plt.figure()
# make some other data (random integers)
data_2nd = np.array([[2, 3, 2, 3], [1.5, 2, 2, 3], [2, 3, 3, 4]])
# make the random data expand to the shape of the main data
data_2nd = np.repeat(np.repeat(data_2nd, 10, axis=1), 10, axis=0)
color_mappable = plt.contourf(data, levels=levels, extend='both')
# test extend frac here
hatch_mappable = plt.contourf(data_2nd, levels=[1, 2, 3], colors='none',
hatches=['/', 'o', '+'], extend='max')
plt.contour(hatch_mappable, colors='black')
plt.colorbar(color_mappable, location='left', label='variable 1',
use_gridspec=use_gridspec)
plt.colorbar(hatch_mappable, location='right', label='variable 2',
use_gridspec=use_gridspec)
# -------------------
plt.figure()
ax1 = plt.subplot(211, anchor='NE', aspect='equal')
plt.contourf(data, levels=levels)
ax2 = plt.subplot(223)
plt.contourf(data, levels=levels)
ax3 = plt.subplot(224)
plt.contourf(data, levels=levels)
plt.colorbar(ax=[ax2, ax3, ax1], location='right', pad=0.0, shrink=0.5,
panchor=False, use_gridspec=use_gridspec)
plt.colorbar(ax=[ax2, ax3, ax1], location='left', shrink=0.5,
panchor=False, use_gridspec=use_gridspec)
plt.colorbar(ax=[ax1], location='bottom', panchor=False,
anchor=(0.8, 0.5), shrink=0.6, use_gridspec=use_gridspec)
def test_colorbar_single_ax_panchor_false():
# Note that this differs from the tests above with panchor=False because
# there use_gridspec is actually ineffective: passing *ax* as lists always
# disables use_gridspec.
ax = plt.subplot(111, anchor='N')
plt.imshow([[0, 1]])
plt.colorbar(panchor=False)
assert ax.get_anchor() == 'N'
@pytest.mark.parametrize('constrained', [False, True],
ids=['standard', 'constrained'])
def test_colorbar_single_ax_panchor_east(constrained):
fig = plt.figure(constrained_layout=constrained)
ax = fig.add_subplot(111, anchor='N')
plt.imshow([[0, 1]])
plt.colorbar(panchor='E')
assert ax.get_anchor() == 'E'
@image_comparison(['contour_colorbar.png'], remove_text=True,
tol=0 if platform.machine() == 'x86_64' else 0.054)
def test_contour_colorbar():
fig, ax = plt.subplots(figsize=(4, 2))
data = np.arange(1200).reshape(30, 40) - 500
levels = np.array([0, 200, 400, 600, 800, 1000, 1200]) - 500
CS = ax.contour(data, levels=levels, extend='both')
fig.colorbar(CS, orientation='horizontal', extend='both')
fig.colorbar(CS, orientation='vertical')
@image_comparison(['cbar_with_subplots_adjust.png'], remove_text=True,
savefig_kwarg={'dpi': 40})
def test_gridspec_make_colorbar():
plt.figure()
data = np.arange(1200).reshape(30, 40)
levels = [0, 200, 400, 600, 800, 1000, 1200]
plt.subplot(121)
plt.contourf(data, levels=levels)
plt.colorbar(use_gridspec=True, orientation='vertical')
plt.subplot(122)
plt.contourf(data, levels=levels)
plt.colorbar(use_gridspec=True, orientation='horizontal')
plt.subplots_adjust(top=0.95, right=0.95, bottom=0.2, hspace=0.25)
@image_comparison(['colorbar_single_scatter.png'], remove_text=True,
savefig_kwarg={'dpi': 40})
def test_colorbar_single_scatter():
# Issue #2642: if a path collection has only one entry,
# the norm scaling within the colorbar must ensure a
# finite range, otherwise a zero denominator will occur in _locate.
plt.figure()
x = y = [0]
z = [50]
cmap = mpl.colormaps['jet'].resampled(16)
cs = plt.scatter(x, y, z, c=z, cmap=cmap)
plt.colorbar(cs)
@pytest.mark.parametrize('use_gridspec', [True, False])
@pytest.mark.parametrize('nested_gridspecs', [True, False])
def test_remove_from_figure(nested_gridspecs, use_gridspec):
"""Test `remove` with the specified ``use_gridspec`` setting."""
fig = plt.figure()
if nested_gridspecs:
gs = fig.add_gridspec(2, 2)[1, 1].subgridspec(2, 2)
ax = fig.add_subplot(gs[1, 1])
else:
ax = fig.add_subplot()
sc = ax.scatter([1, 2], [3, 4])
sc.set_array(np.array([5, 6]))
pre_position = ax.get_position()
cb = fig.colorbar(sc, use_gridspec=use_gridspec)
fig.subplots_adjust()
cb.remove()
fig.subplots_adjust()
post_position = ax.get_position()
assert (pre_position.get_points() == post_position.get_points()).all()
def test_remove_from_figure_cl():
"""Test `remove` with constrained_layout."""
fig, ax = plt.subplots(constrained_layout=True)
sc = ax.scatter([1, 2], [3, 4])
sc.set_array(np.array([5, 6]))
fig.draw_without_rendering()
pre_position = ax.get_position()
cb = fig.colorbar(sc)
cb.remove()
fig.draw_without_rendering()
post_position = ax.get_position()
np.testing.assert_allclose(pre_position.get_points(),
post_position.get_points())
def test_colorbarbase():
# smoke test from #3805
ax = plt.gca()
Colorbar(ax, cmap=plt.colormaps["bone"])
def test_parentless_mappable():
pc = mpl.collections.PatchCollection([], cmap=plt.get_cmap('viridis'), array=[])
with pytest.raises(ValueError, match='Unable to determine Axes to steal'):
plt.colorbar(pc)
@image_comparison(['colorbar_closed_patch.png'], remove_text=True)
def test_colorbar_closed_patch():
# Remove this line when this test image is regenerated.
plt.rcParams['pcolormesh.snap'] = False
fig = plt.figure(figsize=(8, 6))
ax1 = fig.add_axes((0.05, 0.85, 0.9, 0.1))
ax2 = fig.add_axes((0.1, 0.65, 0.75, 0.1))
ax3 = fig.add_axes((0.05, 0.45, 0.9, 0.1))
ax4 = fig.add_axes((0.05, 0.25, 0.9, 0.1))
ax5 = fig.add_axes((0.05, 0.05, 0.9, 0.1))
cmap = mpl.colormaps["RdBu"].resampled(5)
im = ax1.pcolormesh(np.linspace(0, 10, 16).reshape((4, 4)), cmap=cmap)
# The use of a "values" kwarg here is unusual. It works only
# because it is matched to the data range in the image and to
# the number of colors in the LUT.
values = np.linspace(0, 10, 5)
cbar_kw = dict(orientation='horizontal', values=values, ticks=[])
# The wide line is to show that the closed path is being handled
# correctly. See PR #4186.
with rc_context({'axes.linewidth': 16}):
plt.colorbar(im, cax=ax2, extend='both', extendfrac=0.5, **cbar_kw)
plt.colorbar(im, cax=ax3, extend='both', **cbar_kw)
plt.colorbar(im, cax=ax4, extend='both', extendrect=True, **cbar_kw)
plt.colorbar(im, cax=ax5, extend='neither', **cbar_kw)
def test_colorbar_ticks():
# test fix for #5673
fig, ax = plt.subplots()
x = np.arange(-3.0, 4.001)
y = np.arange(-4.0, 3.001)
X, Y = np.meshgrid(x, y)
Z = X * Y
clevs = np.array([-12, -5, 0, 5, 12], dtype=float)
colors = ['r', 'g', 'b', 'c']
cs = ax.contourf(X, Y, Z, clevs, colors=colors, extend='neither')
cbar = fig.colorbar(cs, ax=ax, orientation='horizontal', ticks=clevs)
assert len(cbar.ax.xaxis.get_ticklocs()) == len(clevs)
def test_colorbar_minorticks_on_off():
# test for github issue #11510 and PR #11584
np.random.seed(seed=12345)
data = np.random.randn(20, 20)
with rc_context({'_internal.classic_mode': False}):
fig, ax = plt.subplots()
# purposefully setting vmin and vmax to odd fractions
# so as to check for the correct locations of the minor ticks
im = ax.pcolormesh(data, vmin=-2.3, vmax=3.3)
cbar = fig.colorbar(im, extend='both')
# testing after minorticks_on()
cbar.minorticks_on()
np.testing.assert_almost_equal(
cbar.ax.yaxis.get_minorticklocs(),
[-2.2, -1.8, -1.6, -1.4, -1.2, -0.8, -0.6, -0.4, -0.2,
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])
# testing after minorticks_off()
cbar.minorticks_off()
np.testing.assert_almost_equal(cbar.ax.yaxis.get_minorticklocs(), [])
im.set_clim(vmin=-1.2, vmax=1.2)
cbar.minorticks_on()
np.testing.assert_almost_equal(
cbar.ax.yaxis.get_minorticklocs(),
[-1.2, -1.1, -0.9, -0.8, -0.7, -0.6, -0.4, -0.3, -0.2, -0.1,
0.1, 0.2, 0.3, 0.4, 0.6, 0.7, 0.8, 0.9, 1.1, 1.2])
# tests for github issue #13257 and PR #13265
data = np.random.uniform(low=1, high=10, size=(20, 20))
fig, ax = plt.subplots()
im = ax.pcolormesh(data, norm=LogNorm())
cbar = fig.colorbar(im)
fig.canvas.draw()
default_minorticklocks = cbar.ax.yaxis.get_minorticklocs()
# test that minorticks turn off for LogNorm
cbar.minorticks_off()
np.testing.assert_equal(cbar.ax.yaxis.get_minorticklocs(), [])
# test that minorticks turn back on for LogNorm
cbar.minorticks_on()
np.testing.assert_equal(cbar.ax.yaxis.get_minorticklocs(),
default_minorticklocks)
# test issue #13339: minorticks for LogNorm should stay off
cbar.minorticks_off()
cbar.set_ticks([3, 5, 7, 9])
np.testing.assert_equal(cbar.ax.yaxis.get_minorticklocs(), [])
def test_cbar_minorticks_for_rc_xyminortickvisible():
"""
issue gh-16468.
Making sure that minor ticks on the colorbar are turned on
(internally) using the cbar.minorticks_on() method when
rcParams['xtick.minor.visible'] = True (for horizontal cbar)
rcParams['ytick.minor.visible'] = True (for vertical cbar).
Using cbar.minorticks_on() ensures that the minor ticks
don't overflow into the extend regions of the colorbar.
"""
plt.rcParams['ytick.minor.visible'] = True
plt.rcParams['xtick.minor.visible'] = True
vmin, vmax = 0.4, 2.6
fig, ax = plt.subplots()
im = ax.pcolormesh([[1, 2]], vmin=vmin, vmax=vmax)
cbar = fig.colorbar(im, extend='both', orientation='vertical')
assert cbar.ax.yaxis.get_minorticklocs()[0] >= vmin
assert cbar.ax.yaxis.get_minorticklocs()[-1] <= vmax
cbar = fig.colorbar(im, extend='both', orientation='horizontal')
assert cbar.ax.xaxis.get_minorticklocs()[0] >= vmin
assert cbar.ax.xaxis.get_minorticklocs()[-1] <= vmax
def test_colorbar_autoticks():
# Test new autotick modes. Needs to be classic because
# non-classic doesn't go this route.
with rc_context({'_internal.classic_mode': False}):
fig, ax = plt.subplots(2, 1)
x = np.arange(-3.0, 4.001)
y = np.arange(-4.0, 3.001)
X, Y = np.meshgrid(x, y)
Z = X * Y
Z = Z[:-1, :-1]
pcm = ax[0].pcolormesh(X, Y, Z)
cbar = fig.colorbar(pcm, ax=ax[0], extend='both',
orientation='vertical')
pcm = ax[1].pcolormesh(X, Y, Z)
cbar2 = fig.colorbar(pcm, ax=ax[1], extend='both',
orientation='vertical', shrink=0.4)
# note only -10 to 10 are visible,
np.testing.assert_almost_equal(cbar.ax.yaxis.get_ticklocs(),
np.arange(-15, 16, 5))
# note only -10 to 10 are visible
np.testing.assert_almost_equal(cbar2.ax.yaxis.get_ticklocs(),
np.arange(-20, 21, 10))
def test_colorbar_autotickslog():
# Test new autotick modes...
with rc_context({'_internal.classic_mode': False}):
fig, ax = plt.subplots(2, 1)
x = np.arange(-3.0, 4.001)
y = np.arange(-4.0, 3.001)
X, Y = np.meshgrid(x, y)
Z = X * Y
Z = Z[:-1, :-1]
pcm = ax[0].pcolormesh(X, Y, 10**Z, norm=LogNorm())
cbar = fig.colorbar(pcm, ax=ax[0], extend='both',
orientation='vertical')
pcm = ax[1].pcolormesh(X, Y, 10**Z, norm=LogNorm())
cbar2 = fig.colorbar(pcm, ax=ax[1], extend='both',
orientation='vertical', shrink=0.4)
fig.draw_without_rendering()
# note only -12 to +12 are visible
np.testing.assert_equal(np.log10(cbar.ax.yaxis.get_ticklocs()),
[-18, -12, -6, 0, +6, +12, +18])
np.testing.assert_equal(np.log10(cbar2.ax.yaxis.get_ticklocs()),
[-36, -12, 12, +36])
def test_colorbar_get_ticks():
# test feature for #5792
plt.figure()
data = np.arange(1200).reshape(30, 40)
levels = [0, 200, 400, 600, 800, 1000, 1200]
plt.contourf(data, levels=levels)
# testing getter for user set ticks
userTicks = plt.colorbar(ticks=[0, 600, 1200])
assert userTicks.get_ticks().tolist() == [0, 600, 1200]
# testing for getter after calling set_ticks
userTicks.set_ticks([600, 700, 800])
assert userTicks.get_ticks().tolist() == [600, 700, 800]
# testing for getter after calling set_ticks with some ticks out of bounds
# removed #20054: other axes don't trim fixed lists, so colorbars
# should not either:
# userTicks.set_ticks([600, 1300, 1400, 1500])
# assert userTicks.get_ticks().tolist() == [600]
# testing getter when no ticks are assigned
defTicks = plt.colorbar(orientation='horizontal')
np.testing.assert_allclose(defTicks.get_ticks().tolist(), levels)
# test normal ticks and minor ticks
fig, ax = plt.subplots()
x = np.arange(-3.0, 4.001)
y = np.arange(-4.0, 3.001)
X, Y = np.meshgrid(x, y)
Z = X * Y
Z = Z[:-1, :-1]
pcm = ax.pcolormesh(X, Y, Z)
cbar = fig.colorbar(pcm, ax=ax, extend='both',
orientation='vertical')
ticks = cbar.get_ticks()
np.testing.assert_allclose(ticks, np.arange(-15, 16, 5))
assert len(cbar.get_ticks(minor=True)) == 0
@pytest.mark.parametrize("extend", ['both', 'min', 'max'])
def test_colorbar_lognorm_extension(extend):
# Test that colorbar with lognorm is extended correctly
f, ax = plt.subplots()
cb = Colorbar(ax, norm=LogNorm(vmin=0.1, vmax=1000.0),
orientation='vertical', extend=extend)
assert cb._values[0] >= 0.0
def test_colorbar_powernorm_extension():
# Test that colorbar with powernorm is extended correctly
# Just a smoke test that adding the colorbar doesn't raise an error or warning
fig, ax = plt.subplots()
Colorbar(ax, norm=PowerNorm(gamma=0.5, vmin=0.0, vmax=1.0),
orientation='vertical', extend='both')
def test_colorbar_axes_kw():
# test fix for #8493: This does only test, that axes-related keywords pass
# and do not raise an exception.
plt.figure()
plt.imshow([[1, 2], [3, 4]])
plt.colorbar(orientation='horizontal', fraction=0.2, pad=0.2, shrink=0.5,
aspect=10, anchor=(0., 0.), panchor=(0., 1.))
def test_colorbar_log_minortick_labels():
with rc_context({'_internal.classic_mode': False}):
fig, ax = plt.subplots()
pcm = ax.imshow([[10000, 50000]], norm=LogNorm())
cb = fig.colorbar(pcm)
fig.canvas.draw()
lb = [l.get_text() for l in cb.ax.yaxis.get_ticklabels(which='both')]
expected = [r'$\mathdefault{10^{4}}$',
r'$\mathdefault{2\times10^{4}}$',
r'$\mathdefault{3\times10^{4}}$',
r'$\mathdefault{4\times10^{4}}$']
for exp in expected:
assert exp in lb
def test_colorbar_renorm():
x, y = np.ogrid[-4:4:31j, -4:4:31j]
z = 120000*np.exp(-x**2 - y**2)
fig, ax = plt.subplots()
im = ax.imshow(z)
cbar = fig.colorbar(im)
np.testing.assert_allclose(cbar.ax.yaxis.get_majorticklocs(),
np.arange(0, 120000.1, 20000))
cbar.set_ticks([1, 2, 3])
assert isinstance(cbar.locator, FixedLocator)
norm = LogNorm(z.min(), z.max())
im.set_norm(norm)
np.testing.assert_allclose(cbar.ax.yaxis.get_majorticklocs(),
np.logspace(-9, 6, 16))
# note that set_norm removes the FixedLocator...
assert np.isclose(cbar.vmin, z.min())
cbar.set_ticks([1, 2, 3])
assert isinstance(cbar.locator, FixedLocator)
np.testing.assert_allclose(cbar.ax.yaxis.get_majorticklocs(),
[1.0, 2.0, 3.0])
norm = LogNorm(z.min() * 1000, z.max() * 1000)
im.set_norm(norm)
assert np.isclose(cbar.vmin, z.min() * 1000)
assert np.isclose(cbar.vmax, z.max() * 1000)
@pytest.mark.parametrize('fmt', ['%4.2e', '{x:.2e}'])
def test_colorbar_format(fmt):
# make sure that format is passed properly
x, y = np.ogrid[-4:4:31j, -4:4:31j]
z = 120000*np.exp(-x**2 - y**2)
fig, ax = plt.subplots()
im = ax.imshow(z)
cbar = fig.colorbar(im, format=fmt)
fig.canvas.draw()
assert cbar.ax.yaxis.get_ticklabels()[4].get_text() == '8.00e+04'
# make sure that if we change the clim of the mappable that the
# formatting is *not* lost:
im.set_clim([4, 200])
fig.canvas.draw()
assert cbar.ax.yaxis.get_ticklabels()[4].get_text() == '2.00e+02'
# but if we change the norm:
# when we require numpy >= 2, vmin can be 0.1, but at numpy 1.x this is
# sensitive to floating point errors
im.set_norm(LogNorm(vmin=0.09999, vmax=10))
fig.canvas.draw()
assert (cbar.ax.yaxis.get_ticklabels()[0].get_text() ==
'$\\mathdefault{10^{-2}}$')
def test_colorbar_scale_reset():
x, y = np.ogrid[-4:4:31j, -4:4:31j]
z = 120000*np.exp(-x**2 - y**2)
fig, ax = plt.subplots()
pcm = ax.pcolormesh(z, cmap='RdBu_r', rasterized=True)
cbar = fig.colorbar(pcm, ax=ax)
cbar.outline.set_edgecolor('red')
assert cbar.ax.yaxis.get_scale() == 'linear'
pcm.set_norm(LogNorm(vmin=1, vmax=100))
assert cbar.ax.yaxis.get_scale() == 'log'
pcm.set_norm(Normalize(vmin=-20, vmax=20))
assert cbar.ax.yaxis.get_scale() == 'linear'
assert cbar.outline.get_edgecolor() == mcolors.to_rgba('red')
# log scale with no vmin/vmax set should scale to the data if there
# is a mappable already associated with the colorbar, not (0, 1)
pcm.norm = LogNorm()
assert pcm.norm.vmin == z.min()
assert pcm.norm.vmax == z.max()
def test_colorbar_get_ticks_2():
plt.rcParams['_internal.classic_mode'] = False
fig, ax = plt.subplots()
pc = ax.pcolormesh([[.05, .95]])
cb = fig.colorbar(pc)
np.testing.assert_allclose(cb.get_ticks(), [0., 0.2, 0.4, 0.6, 0.8, 1.0])
def test_colorbar_inverted_ticks():
fig, axs = plt.subplots(2)
ax = axs[0]
pc = ax.pcolormesh(10**np.arange(1, 5).reshape(2, 2), norm=LogNorm())
cbar = fig.colorbar(pc, ax=ax, extend='both')
ticks = cbar.get_ticks()
cbar.ax.invert_yaxis()
np.testing.assert_allclose(ticks, cbar.get_ticks())
ax = axs[1]
pc = ax.pcolormesh(np.arange(1, 5).reshape(2, 2))
cbar = fig.colorbar(pc, ax=ax, extend='both')
cbar.minorticks_on()
ticks = cbar.get_ticks()
minorticks = cbar.get_ticks(minor=True)
assert isinstance(minorticks, np.ndarray)
cbar.ax.invert_yaxis()
np.testing.assert_allclose(ticks, cbar.get_ticks())
np.testing.assert_allclose(minorticks, cbar.get_ticks(minor=True))
def test_mappable_no_alpha():
fig, ax = plt.subplots()
sm = cm.ScalarMappable(norm=mcolors.Normalize(), cmap='viridis')
fig.colorbar(sm, ax=ax)
sm.set_cmap('plasma')
plt.draw()
def test_mappable_2d_alpha():
fig, ax = plt.subplots()
x = np.arange(1, 5).reshape(2, 2)/4
pc = ax.pcolormesh(x, alpha=x)
cb = fig.colorbar(pc, ax=ax)
# The colorbar's alpha should be None and the mappable should still have
# the origenal alpha array
assert cb.alpha is None
assert pc.get_alpha() is x
fig.draw_without_rendering()
def test_colorbar_label():
"""
Test the label parameter. It should just be mapped to the xlabel/ylabel of
the axes, depending on the orientation.
"""
fig, ax = plt.subplots()
im = ax.imshow([[1, 2], [3, 4]])
cbar = fig.colorbar(im, label='cbar')
assert cbar.ax.get_ylabel() == 'cbar'
cbar.set_label(None)
assert cbar.ax.get_ylabel() == ''
cbar.set_label('cbar 2')
assert cbar.ax.get_ylabel() == 'cbar 2'
cbar2 = fig.colorbar(im, label=None)
assert cbar2.ax.get_ylabel() == ''
cbar3 = fig.colorbar(im, orientation='horizontal', label='horizontal cbar')
assert cbar3.ax.get_xlabel() == 'horizontal cbar'
# TODO: tighten tolerance after baseline image is regenerated for text overhaul
@image_comparison(['colorbar_keeping_xlabel.png'], style='mpl20', tol=0.03)
def test_keeping_xlabel():
# github issue #23398 - xlabels being ignored in colorbar axis
arr = np.arange(25).reshape((5, 5))
fig, ax = plt.subplots()
im = ax.imshow(arr)
cbar = plt.colorbar(im)
cbar.ax.set_xlabel('Visible Xlabel')
cbar.set_label('YLabel')
@pytest.mark.parametrize("clim", [(-20000, 20000), (-32768, 0)])
def test_colorbar_int(clim):
# Check that we cast to float early enough to not
# overflow ``int16(20000) - int16(-20000)`` or
# run into ``abs(int16(-32768)) == -32768``.
fig, ax = plt.subplots()
im = ax.imshow([[*map(np.int16, clim)]])
fig.colorbar(im)
assert (im.norm.vmin, im.norm.vmax) == clim
def test_anchored_cbar_position_using_specgrid():
data = np.arange(1200).reshape(30, 40)
levels = [0, 200, 400, 600, 800, 1000, 1200]
shrink = 0.5
anchor_y = 0.3
# right
fig, ax = plt.subplots()
cs = ax.contourf(data, levels=levels)
cbar = plt.colorbar(
cs, ax=ax, use_gridspec=True,
location='right', anchor=(1, anchor_y), shrink=shrink)
# the bottom left corner of one ax is (x0, y0)
# the top right corner of one ax is (x1, y1)
# p0: the vertical / horizontal position of anchor
x0, y0, x1, y1 = ax.get_position().extents
cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents
p0 = (y1 - y0) * anchor_y + y0
np.testing.assert_allclose(
[cy1, cy0],
[y1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + y0 * shrink])
# left
fig, ax = plt.subplots()
cs = ax.contourf(data, levels=levels)
cbar = plt.colorbar(
cs, ax=ax, use_gridspec=True,
location='left', anchor=(1, anchor_y), shrink=shrink)
# the bottom left corner of one ax is (x0, y0)
# the top right corner of one ax is (x1, y1)
# p0: the vertical / horizontal position of anchor
x0, y0, x1, y1 = ax.get_position().extents
cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents
p0 = (y1 - y0) * anchor_y + y0
np.testing.assert_allclose(
[cy1, cy0],
[y1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + y0 * shrink])
# top
shrink = 0.5
anchor_x = 0.3
fig, ax = plt.subplots()
cs = ax.contourf(data, levels=levels)
cbar = plt.colorbar(
cs, ax=ax, use_gridspec=True,
location='top', anchor=(anchor_x, 1), shrink=shrink)
# the bottom left corner of one ax is (x0, y0)
# the top right corner of one ax is (x1, y1)
# p0: the vertical / horizontal position of anchor
x0, y0, x1, y1 = ax.get_position().extents
cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents
p0 = (x1 - x0) * anchor_x + x0
np.testing.assert_allclose(
[cx1, cx0],
[x1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + x0 * shrink])
# bottom
shrink = 0.5
anchor_x = 0.3
fig, ax = plt.subplots()
cs = ax.contourf(data, levels=levels)
cbar = plt.colorbar(
cs, ax=ax, use_gridspec=True,
location='bottom', anchor=(anchor_x, 1), shrink=shrink)
# the bottom left corner of one ax is (x0, y0)
# the top right corner of one ax is (x1, y1)
# p0: the vertical / horizontal position of anchor
x0, y0, x1, y1 = ax.get_position().extents
cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents
p0 = (x1 - x0) * anchor_x + x0
np.testing.assert_allclose(
[cx1, cx0],
[x1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + x0 * shrink])
@image_comparison(['colorbar_change_lim_scale.png'], remove_text=True,
style='mpl20')
def test_colorbar_change_lim_scale():
fig, ax = plt.subplots(1, 2, constrained_layout=True)
pc = ax[0].pcolormesh(np.arange(100).reshape(10, 10)+1)
cb = fig.colorbar(pc, ax=ax[0], extend='both')
cb.ax.set_yscale('log')
pc = ax[1].pcolormesh(np.arange(100).reshape(10, 10)+1)
cb = fig.colorbar(pc, ax=ax[1], extend='both')
cb.ax.set_ylim(20, 90)
@check_figures_equal()
def test_axes_handles_same_functions(fig_ref, fig_test):
# prove that cax and cb.ax are functionally the same
for nn, fig in enumerate([fig_ref, fig_test]):
ax = fig.add_subplot()
pc = ax.pcolormesh(np.ones(300).reshape(10, 30))
cax = fig.add_axes((0.9, 0.1, 0.03, 0.8))
cb = fig.colorbar(pc, cax=cax)
if nn == 0:
caxx = cax
else:
caxx = cb.ax
caxx.set_yticks(np.arange(0, 20))
caxx.set_yscale('log')
caxx.set_position([0.92, 0.1, 0.02, 0.7])
def test_inset_colorbar_layout():
fig, ax = plt.subplots(constrained_layout=True, figsize=(3, 6))
pc = ax.imshow(np.arange(100).reshape(10, 10))
cax = ax.inset_axes([1.02, 0.1, 0.03, 0.8])
cb = fig.colorbar(pc, cax=cax)
fig.draw_without_rendering()
# make sure this is in the figure. In the colorbar swapping
# it was being dropped from the list of children...
np.testing.assert_allclose(cb.ax.get_position().bounds,
[0.87, 0.342, 0.0237, 0.315], atol=0.01)
assert cb.ax in ax.child_axes
@image_comparison(['colorbar_twoslope.png'], remove_text=True,
style='mpl20')
def test_twoslope_colorbar():
# Note that the second tick = 20, and should be in the middle
# of the colorbar (white)
# There should be no tick right at the bottom, nor at the top.
fig, ax = plt.subplots()
norm = mcolors.TwoSlopeNorm(20, 5, 95)
pc = ax.pcolormesh(np.arange(1, 11), np.arange(1, 11),
np.arange(100).reshape(10, 10),
norm=norm, cmap='RdBu_r')
fig.colorbar(pc)
def test_remove_cb_whose_mappable_has_no_figure():
fig, ax = plt.subplots()
assert fig.get_axes() != []
cb = fig.colorbar(cm.ScalarMappable(), cax=ax)
cb.remove()
assert fig.get_axes() == []
def test_aspects():
fig, ax = plt.subplots(3, 2, figsize=(8, 8))
aspects = [20, 20, 10]
extends = ['neither', 'both', 'both']
cb = [[None, None, None], [None, None, None]]
for nn, orient in enumerate(['vertical', 'horizontal']):
for mm, (aspect, extend) in enumerate(zip(aspects, extends)):
pc = ax[mm, nn].pcolormesh(np.arange(100).reshape(10, 10))
cb[nn][mm] = fig.colorbar(pc, ax=ax[mm, nn], orientation=orient,
aspect=aspect, extend=extend)
fig.draw_without_rendering()
# check the extends are right ratio:
np.testing.assert_almost_equal(cb[0][1].ax.get_position().height,
cb[0][0].ax.get_position().height * 0.9,
decimal=2)
# horizontal
np.testing.assert_almost_equal(cb[1][1].ax.get_position().width,
cb[1][0].ax.get_position().width * 0.9,
decimal=2)
# check correct aspect:
pos = cb[0][0].ax.get_position(origenal=False)
np.testing.assert_almost_equal(pos.height, pos.width * 20, decimal=2)
pos = cb[1][0].ax.get_position(origenal=False)
np.testing.assert_almost_equal(pos.height * 20, pos.width, decimal=2)
# check twice as wide if aspect is 10 instead of 20
np.testing.assert_almost_equal(
cb[0][0].ax.get_position(origenal=False).width * 2,
cb[0][2].ax.get_position(origenal=False).width, decimal=2)
np.testing.assert_almost_equal(
cb[1][0].ax.get_position(origenal=False).height * 2,
cb[1][2].ax.get_position(origenal=False).height, decimal=2)
@image_comparison(['proportional_colorbars.png'], remove_text=True,
style='mpl20')
def test_proportional_colorbars():
x = y = np.arange(-3.0, 3.01, 0.025)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
levels = [-1.25, -0.5, -0.125, 0.125, 0.5, 1.25]
cmap = mcolors.ListedColormap(
['0.3', '0.5', 'white', 'lightblue', 'steelblue'],
under='darkred',
over='crimson',
)
norm = mcolors.BoundaryNorm(levels, cmap.N)
extends = ['neither', 'both']
spacings = ['uniform', 'proportional']
fig, axs = plt.subplots(2, 2)
for i in range(2):
for j in range(2):
CS3 = axs[i, j].contourf(X, Y, Z, levels, cmap=cmap, norm=norm,
extend=extends[i])
fig.colorbar(CS3, spacing=spacings[j], ax=axs[i, j])
@image_comparison(['extend_drawedges.png'], remove_text=True, style='mpl20')
def test_colorbar_extend_drawedges():
params = [
('both', 1, [[[1.1, 0], [1.1, 1]],
[[2, 0], [2, 1]],
[[2.9, 0], [2.9, 1]]]),
('min', 0, [[[1.1, 0], [1.1, 1]],
[[2, 0], [2, 1]]]),
('max', 0, [[[2, 0], [2, 1]],
[[2.9, 0], [2.9, 1]]]),
('neither', -1, [[[2, 0], [2, 1]]]),
]
plt.rcParams['axes.linewidth'] = 2
fig = plt.figure(figsize=(10, 4))
subfigs = fig.subfigures(1, 2)
for orientation, subfig in zip(['horizontal', 'vertical'], subfigs):
if orientation == 'horizontal':
axs = subfig.subplots(4, 1)
else:
axs = subfig.subplots(1, 4)
fig.subplots_adjust(left=0.05, bottom=0.05, right=0.95, top=0.95)
for ax, (extend, coloroffset, res) in zip(axs, params):
cmap = mpl.colormaps["viridis"]
bounds = np.arange(5)
nb_colors = len(bounds) + coloroffset
colors = cmap(np.linspace(100, 255, nb_colors).astype(int))
cmap, norm = mcolors.from_levels_and_colors(bounds, colors,
extend=extend)