Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2177d8c

Browse files
committed
FIX: fix colorbars with no scales
1 parentc56e3c5 commit2177d8c

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

‎lib/matplotlib/colorbar.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,13 @@ def _inverse_boundaries(self, x):
996996
b=self._boundaries
997997
returnnp.interp(x,np.linspace(0,b[-1],len(b)),b)
998998

999+
def_forward_from_norm(self,x):
1000+
y=self.norm(x)
1001+
returny
1002+
1003+
def_inverse_from_norm(self,x):
1004+
returnself.norm.inverse(x)
1005+
9991006
def_reset_locator_formatter_scale(self):
10001007
"""
10011008
Reset the locator et al to defaults. Any user-hardcoded changes
@@ -1006,24 +1013,30 @@ def _reset_locator_formatter_scale(self):
10061013
self.locator=None
10071014
self.minorlocator=None
10081015
self.formatter=None
1009-
if ((self.spacing=='uniform')and
1010-
((self.boundariesisnotNone)or
1011-
isinstance(self.norm,colors.BoundaryNorm))):
1012-
funcs= (self._forward_boundaries,self._inverse_boundaries)
1013-
self.ax.set_xscale('function',functions=funcs)
1014-
self.ax.set_yscale('function',functions=funcs)
1015-
self.__scale='function'
1016+
if (self.boundariesisnotNoneor
1017+
isinstance(self.norm,colors.BoundaryNorm)):
1018+
ifself.spacing=='uniform':
1019+
funcs= (self._forward_boundaries,self._inverse_boundaries)
1020+
self.ax.set_xscale('function',functions=funcs)
1021+
self.ax.set_yscale('function',functions=funcs)
1022+
self.__scale='function'
1023+
elifself.spacing=='proportional':
1024+
self.__scale='linear'
1025+
self.ax.set_xscale('linear')
1026+
self.ax.set_yscale('linear')
10161027
elifhasattr(self.norm,'_scale')and (self.norm._scaleisnotNone):
10171028
self.ax.set_xscale(self.norm._scale)
10181029
self.ax.set_yscale(self.norm._scale)
10191030
self.__scale=self.norm._scale.name
1020-
else:
1031+
eliftype(self.norm)iscolors.Normalize:
1032+
self.__scale='linear'
10211033
self.ax.set_xscale('linear')
10221034
self.ax.set_yscale('linear')
1023-
iftype(self.norm)iscolors.Normalize:
1024-
self.__scale='linear'
1025-
else:
1026-
self.__scale='manual'
1035+
else:
1036+
funcs= (self._forward_from_norm,self._inverse_from_norm)
1037+
self.ax.set_xscale('function',functions=funcs)
1038+
self.ax.set_yscale('function',functions=funcs)
1039+
self.__scale='function'
10271040

10281041
def_locate(self,x):
10291042
"""

‎lib/matplotlib/colors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,16 @@ def __call__(self, value, clip=None):
13341334
result=np.atleast_1d(result)[0]
13351335
returnresult
13361336

1337+
definverse(self,value):
1338+
ifnotself.scaled():
1339+
raiseValueError("Not invertible until both vmin and vmax are set")
1340+
(vmin,),_=self.process_value(self.vmin)
1341+
(vmax,),_=self.process_value(self.vmax)
1342+
(vcenter,),_=self.process_value(self.vcenter)
1343+
1344+
result=np.interp(value, [0,0.5,1.], [vmin,vcenter,vmax])
1345+
returnresult
1346+
13371347

13381348
classCenteredNorm(Normalize):
13391349
def__init__(self,vcenter=0,halfrange=None,clip=False):

‎lib/matplotlib/tests/test_colorbar.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,3 +771,17 @@ def test_inset_colorbar_layout():
771771
np.testing.assert_allclose(cb.ax.get_position().bounds,
772772
[0.87,0.342,0.0237,0.315],atol=0.01)
773773
assertcb.ax.outer_axinax.child_axes
774+
775+
776+
@image_comparison(['colorbar_twoslope.png'],remove_text=True,
777+
style='mpl20')
778+
deftest_twoslope_colorbar():
779+
# Note that the first tick = 20, and should be in the middle
780+
# of the colorbar (white)
781+
fig,ax=plt.subplots()
782+
783+
norm=mcolors.TwoSlopeNorm(20,0,100)
784+
pc=ax.pcolormesh(np.arange(1,11),np.arange(1,11),
785+
np.arange(100).reshape(10,10),
786+
norm=norm,cmap='RdBu_r')
787+
fig.colorbar(pc)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp