8000 Move methods to `Colorbar`. · matplotlib/matplotlib@60ebf94 · GitHub
[go: up one dir, main page]

Skip to content

Commit 60ebf94

Browse files
committed
Move methods to Colorbar.
Note that these methods now always return `ListedColormap`. Also: switch from `assert` to `raise ValueError`.
1 parent 42bdcb0 commit 60ebf94

File tree

3 files changed

+101
-198
lines changed

3 files changed

+101
-198
lines changed

lib/matplotlib/colors.py

Lines changed: 101 additions & 198 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,107 @@ def reversed(self, name=None):
614614
"""
615615
raise NotImplementedError()
616616

617+
def join(self, other, name=None, frac_self=None, N=None):
618+
"""
619+
Join colormap `self` to `other` and return the new colormap.
620+
621+
Parameters
622+
----------
623+
other : cmap
624+
The other colormap to be joined to this one.
625+
name : str, optional
626+
The name for the reversed colormap. If it's None the
627+
name will be ``self.name + '-' + other.name``.
628+
frac_self : float in the interval ``(0.0, 1.0)``, optional
629+
The fraction of the new colormap that should be occupied
630+
by self. By default, this is ``self.N / (self.N +
631+
other.N)``.
632+
N : int
633+
The number of entries in the color map. The default is ``None``,
634+
in which case the number of entries is the sum of the
635+
number of entries in the two colormaps to be joined.
636+
637+
Returns
638+
-------
639+
ListedColormap
640+
The joined colormap.
641+
642+
Examples
643+
--------
644+
import matplotlib.pyplat as plt
645+
cmap1 = plt.get_cmap('viridis', 128)
646+
cmap2 = plt.get_cmap('plasma_r', 64)
647+
648+
joined_cmap = cmap1.join(cmap2)
649+
650+
# Note that `joined_cmap` will be 2/3 `cmap1`, and 1/3 `cmap2`
651+
# because of the default behavior of frac_self
652+
# (i.e. proportional to N of each cmap).
653+
"""
654+
if N is None:
655+
N = self.N + other.N
656+
if frac_self is None:
657+
frac_self = self.N / (other.N + self.N)
658+
if name is None:
659+
name = '{}+{}'.format(self.name, other.name)
660+
if not (0 < frac_self and frac_self < 1):
661+
raise ValueError("frac_self must be in the interval (0.0, 1.0)")
662+
map0 = self(np.linspace(0, 1, int(N * frac_self)))
663+
map1 = other(np.linspace(0, 1, int(N * (1 - frac_self))))
664+
# N is set by len of the vstack'd array:
665+
return ListedColormap(np.vstack((map0, map1)), name, )
666+
667+
def truncate(self, minval=0.0, maxval=1.0, N=None):
668+
"""
669+
Truncate a colormap.
670+
671+
Parameters
672+
----------
673+
minval : float in the interval ``(0.0, 1.0)``, optional
674+
The lower fraction of the colormap you want to truncate
675+
(default 0.0).
676+
677+
maxval : float in the interval ``(0.0, 1.0)``, optional
678+
The upper limit of the colormap you want to keep. i.e. truncate
679+
the section above this value (default 1.0).
680+
681+
N : int
682+
The number of entries in the map. The default is *None*,
683+
in which case the same color-step density is preserved,
684+
i.e.: N = ceil(N * (maxval - minval))
685+
686+
Returns
687+
-------
688+
ListedColormap
689+
The truncated colormap.
690+
691+
Examples
692+
--------
693+
import matplotlib.pyplat as plt
694+
cmap = plt.get_cmap('viridis')
695+
696+
# This will return the `viridis` colormap with the bottom 20%,
697+
# and top 30% removed:
698+
cmap_trunc = cmap.truncate(0.2, 0.7)
699+
700+
"""
701+
if minval >= maxval:
702+
raise ValueError("minval must be less than maxval")
703+
if minval < 0 or minval >= 1 or maxval <= 0 or maxval > 1:
704+
raise ValueError(
705+
"minval and maxval must be in the interval (0.0, 1.0)"
706+
)
707+
if minval == 0 and maxval == 1:
708+
raise ValueError("This is not a truncation")
709+
# This was taken largely from
710+
# https://gist.github.com/denis-bz/8052855
711+
# Which, in turn was from @unutbu's SO answer:
712+
# http://stackoverflow.com/a/18926541/2121597
713+
if N is None:
714+
N = np.ceil(self.N * (maxval - minval))
715+
name = "trunc({},{:.2f},{:.2f})".format(self.name, minval, maxval)
716+
return ListedColormap(self(np.linspace(minval, maxval, N)), name)
717+
617718

618719
class LinearSegmentedColormap(Colormap):
619720
"""Colormap objects based on lookup tables using linear segments.
@@ -763,107 +864,6 @@ def func_r(x):
763864

764865
return LinearSegmentedColormap(name, data_r, self.N, self._gamma)
765866

766-
def join(self, other, name=None, frac_self=None, N=None):
767-
"""
768-
Join colormap `self` to `other` and return the new colormap.
769-
770-
Parameters
771-
----------
772-
other : cmap
773-
The other colormap to be joined to this one.
774-
name : str, optional
775-
The name for the reversed colormap. If it's None the
776-
name will be ``self.name + '-' + other.name``.
777-
frac_self : float in the interval ``(0.0, 1.0)``, optional
778-
The fraction of the new colormap that should be occupied
779-
by self. By default, this is ``self.N / (self.N +
780-
other.N)``.
781-
N : int
782-
The number of entries in the color map. The default is ``None``,
783-
in which case the number of entries is the sum of the
784-
number of entries in the two colormaps to be joined.
785-
786-
Returns
787-
-------
788-
LinearSegmentedColormap
789-
The joined colormap.
790-
791-
Examples
792-
--------
793-
import matplotlib.pyplat as plt
794-
cmap1 = plt.get_cmap('jet')
795-
cmap2 = plt.get_cmap('plasma_r')
796-
797-
joined_cmap = cmap1.join(cmap2)
798-
"""
799-
if N is None:
800-
N = self.N + other.N
801-
if frac_self is None:
802-
frac_self = self.N / (other.N + self.N)
803-
if name is None:
804-
name = '{}+{}'.format(self.name, other.name)
805-
assert 0 < frac_self and frac_self < 1, (
806-
"The parameter frac_self must be in the interval (0.0, 1.0)."
807-
)
808-
map0 = self(np.linspace(0, 1, int(N * frac_self)))
809-
map1 = other(np.linspace(0, 1, int(N * (1 - frac_self))))
810-
# N is set by len of the vstack'd array:
811-
return LinearSegmentedColormap.from_list(name, np.vstack((map0, map1)))
812-
813-
def truncate(self, minval=0.0, maxval=1.0, N=None):
814-
"""
815-
Truncate a colormap.
816-
817-
Parameters
818-
----------
819-
minval : float in the interval ``(0.0, 1.0)``, optional
820-
The lower fraction of the colormap you want to truncate
821-
(default 0.0).
822-
823-
maxval : float in the interval ``(0.0, 1.0)``, optional
824-
The upper limit of the colormap you want to keep. i.e. truncate
825-
the section above this value (default 1.0).
826-
827-
N : int
828-
The number of entries in the map. The default is *None*,
829-
in which case the same color-step density is preserved,
830-
i.e.: N = ceil(N * (maxval - minval))
831-
832-
Returns
833-
-------
834-
LinearSegmentedColormap
835-
The truncated colormap.
836-
837-
Examples
838-
--------
839-
import matplotlib.pyplat as plt
840-
cmap = plt.get_cmap('jet')
841-
842-
# This will return the `jet` colormap with the bottom 20%,
843-
# and top 30% removed:
844-
cmap_trunc = cmap.truncate(0.2, 0.7)
845-
846-
"""
847-
assert minval < maxval, "minval must be less than maxval"
848-
assert 0 <= minval and minval < 1, (
849-
"The parameter minval must be in the interval (0.0, 1.0).")
850-
assert 0 < maxval and maxval <= 1, (
851-
"The parameter maxval must be in the interval (0.0, 1.0).")
852-
assert minval != 0 or maxval != 1, (
853-
"This is not a truncation.")
854-
# This was taken largely from
855-
# https://gist.github.com/denis-bz/8052855
856-
# Which, in turn was from @unutbu's SO answer:
857-
# http://stackoverflow.com/a/18926541/2121597
858-
if N is None:
859-
N = np.ceil(self.N * (maxval - minval))
860-
name = "trunc({},{:.2f},{:.2f})".format(self.name, minval, maxval)
861-
return LinearSegmentedColormap.from_list(name,
862-
self(np.linspace(minval,
863-
maxval,
864-
N)),
865-
N)
866-
867867

868868
class ListedColormap(Colormap):
869869
"""Colormap object generated from a list of colors.
@@ -954,103 +954,6 @@ def reversed(self, name=None):
954954
colors_r = list(reversed(self.colors))
955955
return ListedColormap(colors_r, name=name, N=self.N)
956956

957-
def join(self, other, name=None, frac_self=None, N=None):
958-
"""
959-
Join colormap `self` to `other` and return the new colormap.
960-
961-
Parameters
962-
----------
963-
other : cmap
964-
The other colormap to be joined to this one.
965-
name : str, optional
966-
The name for the reversed colormap. If it's None the
967-
name will be ``self.name + '-' + other.name``.
968-
frac_self : float in the interval ``(0.0, 1.0)``, optional
969-
The fraction of the new colormap that should be occupied
970-
by self. By default, this is ``self.N / (self.N +
971-
other.N)``.
972-
N : int
973-
The number of entries in the color map. The default is ``None``,
974-
in which case the number of entries is the sum of the
975-
number of entries in the two colormaps to be joined.
976-
977-
Returns
978-
-------
979-
ListedColormap
980-
The joined colormap.
981-
982-
Examples
983-
--------
984-
import matplotlib.pyplat as plt
985-
cmap1 = plt.get_cmap('viridis')
986-
cmap2 = plt.get_cmap('plasma_r')
987-
988-
joined_cmap = cmap1.join(cmap2)
989-
"""
990-
if N is None:
991-
N = self.N + other.N
992-
if frac_self is None:
993-
frac_self = self.N / (other.N + self.N)
994-
if name is None:
995-
name = '{}+{}'.format(self.name, other.name)
996-
assert 0 < frac_self and frac_self < 1, (
997-
"The parameter frac_self must be in the interval (0.0, 1.0)."
998-
)
999-
map0 = self(np.linspace(0, 1, int(N * frac_self)))
1000-
map1 = other(np.linspace(0, 1, int(N * (1 - frac_self))))
1001-
# N is set by len of the vstack'd array:
1002-
return ListedColormap(np.vstack((map0, map1)), name, )
1003-
1004-
def truncate(self, minval=0.0, maxval=1.0, N=None):
1005-
"""
1006-
Truncate a colormap.
1007-
1008-
Parameters
1009-
----------
1010-
minval : float in the interval ``(0.0, 1.0)``, optional
1011-
The lower fraction of the colormap you want to truncate
1012-
(default 0.0).
1013-
1014-
maxval : float in the interval ``(0.0, 1.0)``, optional
1015-
The upper limit of the colormap you want to keep. i.e. truncate
1016-
the section above this value (default 1.0).
1017-
1018-
N : int
1019-
The number of entries in the map. The default is *None*,
1020-
in which case the same color-step density is preserved,
1021-
i.e.: N = ceil(N * (maxval - minval))
1022-
1023-
Returns
1024-
-------
1025-
ListedColormap
1026-
The truncated colormap.
1027-
1028-
Examples
1029-
--------
1030-
import matplotlib.pyplat as plt
1031-
cmap = plt.get_cmap('viridis')
1032-
1033-
# This will return the `viridis` colormap with the bottom 20%,
1034-
# and top 30% removed:
1035-
cmap_trunc = cmap.truncate(0.2, 0.7)
1036-
1037-
"""
1038-
assert minval < maxval, "minval must be less than maxval"
1039-
assert 0 <= minval and minval < 1, (
1040-
"The parameter minval must be in the interval (0.0, 1.0).")
1041-
assert 0 < maxval and maxval <= 1, (
1042-
"The parameter maxval must be in the interval (0.0, 1.0).")
1043-
assert minval != 0 or maxval != 1, (
1044-
"This is not a truncation.")
1045-
# This was taken largely from
1046-
# https://gist.github.com/denis-bz/8052855
1047-
# Which, in turn was from @unutbu's SO answer:
1048-
# http://stackoverflow.com/a/18926541/2121597
1049-
if N is None:
1050-
N = np.ceil(self.N * (maxval - minval))
1051-
name = "trunc({},{:.2f},{:.2f})".format(self.name, minval, maxval)
1052-
return ListedColormap(self(np.linspace(minval, maxval, N)), name)
1053-
1054957

1055958
class Normalize(object):
1056959
"""
Loading
Loading

0 commit comments

Comments
 (0)
0