@@ -742,6 +742,99 @@ def func_r(x):
742
742
743
743
return LinearSegmentedColormap (name , data_r , self .N , self ._gamma )
744
744
745
+ def join (self , other , name = None , frac_self = None , N = None ):
746
+ """
747
+ Join colormap `self` to `other` and return the new colormap.
748
+
749
+ Parameters
750
+ ----------
751
+ other : cmap
752
+ The other colormap to be joined to this one.
753
+ name : str, optional
754
+ The name for the reversed colormap. If it's None the
755
+ name will be ``self.name + '-' + other.name``.
756
+ frac_self : float in the interval ``(0.0, 1.0)``, optional
757
+ The fraction of the new colormap that should be occupied
758
+ by self. By default, this is ``self.N / (self.N +
759
+ other.N)``.
760
+
761
+ Returns
762
+ -------
763
+ LinearSegmentedColormap
764
+ The joined colormap.
765
+
766
+ Examples
767
+ --------
768
+ import matplotlib.pyplat as plt
769
+ cmap1 = plt.get_cmap('jet')
770
+ cmap2 = plt.get_cmap('plasma_r')
771
+
772
+ joined_cmap = cmap1.join(cmap2)
773
+ """
774
+ if N is None :
775
+ N = self .N + other .N
776
+ if frac_self is None :
777
+ frac_self = self .N / (other .N + self .N )
778
+ if name is None :
779
+ name = '{}+{}' .format (self .name , other .name )
780
+ assert 0 < frac_self and frac_self < 1 , (
781
+ "The parameter ``frac_self`` must be in the interval ``(0.0, 1.0)``."
782
+ )
783
+ map0 = self (np .linspace (0 , 1 , int (N * frac_self )))
784
+ map1 = other (np .linspace (0 , 1 , int (N * (1 - frac_self ))))
785
+ # N is set by len of the vstack'd array:
786
+ return LinearSegmentedColormap .from_list (name , np .vstack ((map0 , map1 )))
787
+
788
+ def truncate (self , minval = 0.0 , maxval = 1.0 , N = None ):
789
+ """
790
+ Truncate a colormap.
791
+
792
+ Parameters
793
+ ----------
794
+ minval : float in the interval ``(0.0, 1.0)``, optional
795
+ The lower fraction of the colormap you want to truncate
796
+ (default 0.0).
797
+
798
+ maxval : float in the interval ``(0.0, 1.0)``, optional
799
+ The upper limit of the colormap you want to keep. i.e. truncate
800
+ the section above this value (default 1.0).
801
+
802
+ N : int
803
+ The number of entries in the map. The default is *None*,
804
+ in which case the same color-step density is preserved,
805
+ i.e.: N = ceil(N * (maxval - minval))
806
+
807
+ Returns
808
+ -------
809
+ LinearSegmentedColormap
810
+ The truncated colormap.
811
+
812
+ Examples
813
+ --------
814
+ import matplotlib.pyplat as plt
815
+ cmap = plt.get_cmap('jet')
816
+
817
+ # This will return the `jet` colormap with the bottom 20%,
818
+ # and top 30% removed:
819
+ cmap_trunc = cmap.truncate(0.2, 0.7)
820
+
821
+ """
822
+ assert minval < maxval , "``minval`` must be less than ``maxval``"
823
+ assert 0 <= minval and minval < 1 , (
824
+ "The parameter ``minval`` must be in the interval ``(0.0, 1.0)``." )
825
+ assert 0 < maxval and maxval <= 1 , (
826
+ "The parameter ``maxval`` must be in the interval ``(0.0, 1.0)``." )
827
+ assert minval != 0 or maxval != 1 , (
828
+ "This is not a truncation." )
829
+ # This was taken largely from
830
+ # https://gist.github.com/denis-bz/8052855
831
+ # Which, in turn was from @unutbu's SO answer:
832
+ # http://stackoverflow.com/a/18926541/2121597
833
+ if N is None :
834
+ N = np .ceil (self .N * (maxval - minval ))
835
+ name = "trunc({},{:.2f},{:.2f})" .format (self .name , minval , maxval )
836
+ return LinearSegmentedColormap .from_list (name , self (np .linspace (minval , maxval , N )), N )
837
+
745
838
746
839
class ListedColormap (Colormap ):
747
840
"""Colormap object generated from a list of colors.
@@ -835,6 +928,99 @@ def reversed(self, name=None):
835
928
colors_r = list (reversed (self .colors ))
836
929
return ListedColormap (colors_r , name = name , N = self .N )
837
930
931
+ def join (self , other , name = None , frac_self = None , N = None ):
932
+ """
933
+ Join colormap `self` to `other` and return the new colormap.
934
+
935
+ Parameters
936
+ ----------
937
+ other : cmap
938
+ The other colormap to be joined to this one.
939
+ name : str, optional
940
+ The name for the reversed colormap. If it's None the
941
+ name will be ``self.name + '-' + other.name``.
942
+ frac_self : float in the interval ``(0.0, 1.0)``, optional
943
+ The fraction of the new colormap that should be occupied
944
+ by self. By default, this is ``self.N / (self.N +
945
+ other.N)``.
946
+
947
+ Returns
948
+ -------
949
+ ListedColormap
950
+ The joined colormap.
951
+
952
+ Examples
953
+ --------
954
+ import matplotlib.pyplat as plt
955
+ cmap1 = plt.get_cmap('viridis')
956
+ cmap2 = plt.get_cmap('plasma_r')
957
+
958
+ joined_cmap = cmap1.join(cmap2)
959
+ """
960
+ if N is None :
961
+ N = self .N + other .N
962
+ if frac_self is None :
963
+ frac_self = self .N / (other .N + self .N )
964
+ if name is None :
965
+ name = '{}+{}' .format (self .name , other .name )
966
+ assert 0 < frac_self and frac_self < 1 , (
967
+ "The parameter ``frac_self`` must be in the interval ``(0.0, 1.0)``."
968
+ )
969
+ map0 = self (np .linspace (0 , 1 , int (N * frac_self )))
970
+ map1 = other (np .linspace (0 , 1 , int (N * (1 - frac_self ))))
971
+ # N is set by len of the vstack'd array:
972
+ return ListedColormap (np .vstack ((map0 , map1 )), name , )
973
+
974
+ def truncate (self , minval = 0.0 , maxval = 1.0 , N = None ):
975
+ """
976
+ Truncate a colormap.
977
+
978
+ Parameters
979
+ ----------
980
+ minval : float in the interval ``(0.0, 1.0)``, optional
981
+ The lower fraction of the colormap you want to truncate
982
+ (default 0.0).
983
+
984
+ maxval : float in the interval ``(0.0, 1.0)``, optional
985
+ The upper limit of the colormap you want to keep. i.e. truncate
986
+ the section above this value (default 1.0).
987
+
988
+ N : int
989
+ The number of entries in the map. The default is *None*,
990
+ in which case the same color-step density is preserved,
991
+ i.e.: N = ceil(N * (maxval - minval))
992
+
993
+ Returns
994
+ -------
995
+ ListedColormap
996
+ The truncated colormap.
997
+
998
+ Examples
999
+ --------
1000
+ import matplotlib.pyplat as plt
1001
+ cmap = plt.get_cmap('viridis')
1002
+
1003
+ # This will return the `viridis` colormap with the bottom 20%,
1004
+ # and top 30% removed:
1005
+ cmap_trunc = cmap.truncate(0.2, 0.7)
1006
+
1007
+ """
1008
+ assert minval < maxval , "``minval`` must be less than ``maxval``"
1009
+ assert 0 <= minval and minval < 1 , (
1010
+ "The parameter ``minval`` must be in the interval ``(0.0, 1.0)``." )
1011
+ assert 0 < maxval and maxval <= 1 , (
1012
+ "The parameter ``maxval`` must be in the interval ``(0.0, 1.0)``." )
1013
+ assert minval != 0 or maxval != 1 , (
1014
+ "This is not a truncation." )
1015
+ # This was taken largely from
1016
+ # https://gist.github.com/denis-bz/8052855
1017
+ # Which, in turn was from @unutbu's SO answer:
1018
+ # http://stackoverflow.com/a/18926541/2121597
1019
+ if N is None :
1020
+ N = np .ceil (self .N * (maxval - minval ))
1021
+ name = "trunc({},{:.2f},{:.2f})" .format (self .name , minval , maxval )
1022
+ return ListedColormap (self (np .linspace (minval , maxval , N )), name )
1023
+
838
1024
839
1025
class Normalize (object ):
840
1026
"""
@@ -1040,7 +1226,7 @@ class SymLogNorm(Normalize):
1040
1226
*linthresh* allows the user to specify the size of this range
1041
1227
(-*linthresh*, *linthresh*).
1042
1228
"""
1043
- def __init__ (self , linthresh , linscale = 1.0 ,
1229
+ def __init__ (self , linthresh , linscale = 1.0 ,
1044
1230
vmin = None , vmax = None , clip = False ):
1045
1231
"""
1046
1232
*linthresh*:
0 commit comments