@@ -1414,7 +1414,7 @@ class BivarColormap(ColormapBase):
1414
1414
lookup table. To be used with `~matplotlib.cm.VectorMappable`.
1415
1415
"""
1416
1416
1417
- def __init__ (self , name , N = 256 , M = 256 , shape = 'square' ):
1417
+ def __init__ (self , name , N = 256 , M = 256 , shape = 'square' , origin = ( 0 , 0 ) ):
1418
1418
"""
1419
1419
Parameters
1420
1420
----------
@@ -1436,6 +1436,10 @@ def __init__(self, name, N=256, M=256, shape='square'):
1436
1436
- If 'circleignore' a circular mask is applied, but the data is not
1437
1437
clipped and instead assigned the 'outside' color
1438
1438
1439
+ origin: (int, int)
1440
+ The relative origin of the colormap. Typically (0, 0), for colormaps
1441
+ that are linear on both axis, and (int(N*0.5), int(.5*M) for
1442
+ circular colormaps.
1439
1443
"""
1440
1444
1441
1445
self .name = name
@@ -1446,6 +1450,7 @@ def __init__(self, name, N=256, M=256, shape='square'):
1446
1450
self ._rgba_outside = (1.0 , 0.0 , 1.0 , 1.0 )
1447
1451
self ._isinit = False
1448
1452
self .n_variates = 2
1453
+ self ._origin = origin
1449
1454
'''#: When this colormap exists on a scalar mappable and colorbar_extend
1450
1455
#: is not False, colorbar creation will pick up ``colorbar_extend`` as
1451
1456
#: the default value for the ``extend`` keyword in the
@@ -1692,6 +1697,30 @@ def _clip(self, X):
1692
1697
X [0 ][mask_outside ] = - 1
1693
1698
X [1 ][mask_outside ] = - 1
1694
1699
1700
+ def __getitem__ (self , item ):
1701
+ """Creates and returns a colorbar along the selected axis"""
1702
+ if not self ._isinit :
1703
+ self ._init ()
1704
+ if item == 0 :
1705
+ cmap = Colormap (self .name + '0' , self .N )
1706
+ one_d_lut = self ._lut [:, self ._origin [1 ]]
1707
+ elif item == 1 :
1708
+ cmap = Colormap (self .name + '1' , self .M )
1709
+ one_d_lut = self ._lut [self ._origin [0 ], :]
1710
+ else :
1711
+ raise KeyError (f"only 0 or 1 are"
1712
+ f" valid keys for BivarColormap, not { item !r} " )
1713
+ cmap ._lut = np .zeros ((self .N + 3 , 4 ), float )
1714
+ cmap ._lut [:- 3 ] = one_d_lut
1715
+ cmap .set_bad (self ._rgba_bad )
1716
+ self ._rgba_outside
1717
+ if self .shape in ['ignore' , 'circleignore' ]:
1718
+ cmap .set_under (self ._rgba_outside )
1719
+ cmap .set_over (self ._rgba_outside )
1720
+ cmap ._set_extremes ()
1721
+ cmap ._isinit = True
1722
+ return cmap
1723
+
1695
1724
def _repr_png_ (self ):
1696
1725
"""Generate a PNG representation of the BivarColormap."""
1697
1726
if not self ._isinit :
@@ -1769,11 +1798,15 @@ class SegmentedBivarColormap(BivarColormap):
1769
1798
'outside' color
1770
1799
- If 'circleignore' a circular mask is applied, but the data is not clipped
1771
1800
1801
+ origin: (int, int)
1802
+ The relative origin of the colormap. Typically (0, 0), for colormaps
1803
+ that are linear on both axis, and (int(N*0.5), int(.5*M) for
1804
+ circular colormaps.
1772
1805
"""
1773
1806
1774
- def __init__ (self , patch , name , N = 256 , shape = 'square' ):
1807
+ def __init__ (self , patch , name , N = 256 , shape = 'square' , origin = ( 0 , 0 ) ):
1775
1808
self .patch = patch
1776
- super ().__init__ (name , N , N , shape )
1809
+ super ().__init__ (name , N , N , shape , origin )
1777
1810
1778
1811
def _init (self ):
1779
1812
s = self .patch .shape
@@ -1809,9 +1842,13 @@ class BivarColormapFromImage(BivarColormap):
1809
1842
'outside' color
1810
1843
- If 'circleignore' a circular mask is applied, but the data is not clipped
1811
1844
1845
+ origin: (int, int)
1846
+ The relative origin of the colormap. Typically (0, 0), for colormaps
1847
+ that are linear on both axis, and (int(N*0.5), int(.5*M) for
1848
+ circular colormaps.
1812
1849
"""
1813
1850
1814
- def __init__ (self , lut , name = '' , shape = 'square' ):
1851
+ def __init__ (self , lut , name = '' , shape = 'square' , origin = ( 0 , 0 ) ):
1815
1852
# We can allow for a PIL.Image as unput in the following way, but importing
1816
1853
# matplotlib.image.pil_to_array() results in a circular import
1817
1854
# For now, this function only accepts numpy arrays.
@@ -1822,7 +1859,7 @@ def __init__(self, lut, name='', shape='square'):
1822
1859
raise ValueError ("The lut must be an array of shape (n, m, 3) or (n, m, 4)" ,
1823
1860
" or a PIL.image encoded as RGB or RGBA" )
1824
1861
self ._lut = lut
1825
- super ().__init__ (name , lut .shape [0 ], lut .shape [1 ], shape )
1862
+ super ().__init__ (name , lut .shape [0 ], lut .shape [1 ], shape , origin )
1826
1863
1827
1864
def _init (self ):
1828
1865
self ._isinit = True
0 commit comments