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

Skip to content

Commit de62491

Browse files
alvarosgalvarosg
alvarosg
authored andcommitted
Improved the auto-tick feature, and corrected some pep8 issues
1 parent 3749b0a commit de62491

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

lib/matplotlib/colors.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,13 @@ def _fun_normalizer(fun):
10431043
return (lambda x: (fun(x) - fun(0.)) / (fun(1.) - fun(0.)))
10441044

10451045
def ticks(self, N=13):
1046-
return self.inverse(np.linspace(0, 1, N))
1046+
ticks = self.inverse(np.linspace(0, 1, N))
1047+
finalticks = np.zeros(ticks.shape, dtype=np.bool)
1048+
finalticks[0] = True
1049+
finalticks[0] = True
1050+
ticks = FuncNorm._round_ticks(ticks, finalticks)
1051+
1052+
return ticks
10471053

10481054
def autoscale(self, A):
10491055
self.vmin = np.ma.min(A)
@@ -1057,6 +1063,19 @@ def autoscale_None(self, A):
10571063
if self.vmax is None:
10581064
self.vmax = np.ma.max(A)
10591065

1066+
@staticmethod
1067+
def _round_ticks(ticks, permanenttick):
1068+
ticks = ticks.copy()
1069+
for i in range(len(ticks)):
1070+
if i == 0 or i == len(ticks) - 1 or permanenttick[i]:
1071+
continue
1072+
d1 = ticks[i] - ticks[i - 1]
1073+
d2 = ticks[i + 1] - ticks[i]
1074+
d = min([d1, d2])
1075+
order = -np.floor(np.log10(d))
1076+
ticks[i] = float(np.round(ticks[i] * 10**order)) / 10**order
1077+
return ticks
1078+
10601079

10611080
class PiecewiseNorm(FuncNorm):
10621081
"""
@@ -1168,8 +1187,8 @@ def _build_f(self):
11681187
(x - rp_d[i]) / widths_d[i]) * widths_cm[i] + rp_cm[i])
11691188
masks.append(mask)
11701189
funcs.append(func)
1171-
maskmaker = (lambda x: [np.array([mi(x) == True]) if np.isscalar(
1172-
x) else mi(x) == True for mi in masks])
1190+
maskmaker = (lambda x: [np.array([mi(x)]) if np.isscalar(
1191+
x) else mi(x) for mi in masks])
11731192
f = (lambda x: np.piecewise(x, maskmaker(x), funcs))
11741193
return f
11751194

@@ -1195,8 +1214,8 @@ def _build_finv(self):
11951214
funcs.append(lambda x, i=i: self._finvlist[i](
11961215
(x - rp_cm[i]) / widths_cm[i]) * widths_d[i] + rp_d[i])
11971216

1198-
maskmaker = (lambda x: [np.array([mi(x) == True]) if np.isscalar(
1199-
x) else mi(x) == True for mi in masks])
1217+
maskmaker = (lambda x: [np.array([mi(x)]) if np.isscalar(
1218+
x) else mi(x) for mi in masks])
12001219
x = np.array([0.5, 1])
12011220
finv = (lambda x: np.piecewise(x, maskmaker(x), funcs))
12021221
return finv
@@ -1242,12 +1261,22 @@ def ticks(self, N=None):
12421261
N = nticks[i]
12431262
auxticks = np.linspace(rp_cm[i], rp_cm[i + 1], N + 2)
12441263
ticks = np.concatenate([ticks, auxticks[1:-1]])
1245-
return self.inverse(np.sort(ticks))
1264+
1265+
finalticks = np.zeros(ticks.shape, dtype=np.bool)
1266+
finalticks[0:len(rp_cm)] = True
1267+
1268+
inds = np.argsort(ticks)
1269+
ticks = ticks[inds]
1270+
finalticks = finalticks[inds]
1271+
1272+
ticks = PiecewiseNorm._round_ticks(self.inverse(ticks), finalticks)
1273+
1274+
return ticks
12461275

12471276

12481277
class MirrorPiecewiseNorm(PiecewiseNorm):
12491278
"""
1250-
Normalization allowing the definition of data ranges and colormap ranges,
1279+
Normalization allowing the definition of data ranges and colormap ranges,
12511280
with a non linear map between each.
12521281
>>> norm=PiecewiseNorm(fpos=(lambda x: x**0.5),
12531282
>>> fposinv=(lambda x: x**2),

0 commit comments

Comments
 (0)