8000 Major reworking; mapping has priority. · matplotlib/matplotlib@8fe1b2a · GitHub
[go: up one dir, main page]

Skip to content
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

Appearance settings

Commit 8fe1b2a

Browse files
committed
Major reworking; mapping has priority.
1 parent 7a26184 commit 8fe1b2a

File tree

3 files changed

+60
-55
lines changed

3 files changed

+60
-55
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6026,7 +6026,7 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
60266026
if shading is None:
60276027
shading = rcParams['pcolor.shading']
60286028
shading = shading.lower()
6029-
kwargs.setdefault('edgecolors', 'None')
6029+
kwargs.setdefault('edgecolors', 'none')
60306030

60316031
X, Y, C, shading = self._pcolorargs('pcolormesh', *args,
60326032
shading=shading)

lib/matplotlib/collections.py

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ def __init__(self,
171171
self._us_lw = [0]
172172
self._linewidths = [0]
173173
# Flags: do colors come from mapping an array?
174-
self._face_is_mapped = True
175-
self._edge_is_mapped = False
174+
self._face_is_mapped = None
175+
self._edge_is_mapped = None
176176
self._mapped_colors = None # Calculated in update_scalarmappable
177177
self._hatch_color = mcolors.to_rgba(mpl.rcParams['hatch.color'])
178178
self.set_facecolor(facecolors)
@@ -803,7 +803,9 @@ def _get_default_edgecolor(self):
803803
def _set_edgecolor(self, c):
804804
set_hatch_color = True
805805
if c is None:
806-
if mpl.rcParams['patch.force_edgecolor'] or self._edge_default:
806+
if (mpl.rcParams['patch.force_edgecolor']
807+
or self._edge_default
808+
or cbook._str_equal(self._original_facecolor, 'none')):
807809
c = self._get_default_edgecolor()
808810
else:
809811
c = 'none'
@@ -874,39 +876,20 @@ def _set_mappable_flags(self):
874876
"""
875877
edge0 = self._edge_is_mapped
876878
face0 = self._face_is_mapped
877-
if self._A is None:
878-
self._edge_is_mapped = False
879-
self._face_is_mapped = False
880-
else:
881-
# Typical mapping: faces, not edges.
882-
self._face_is_mapped = True
883-
self._edge_is_mapped = False
884-
885-
# Prepare color strings to check for special cases.
886-
fc = self._original_facecolor
887-
if fc is None:
888-
fc = self._get_default_facecolor()
889-
if not isinstance(fc, str):
890-
fc = 'array'
891-
ec = self._original_edgecolor
892-
if ec is None:
893-
if mpl.rcParams['patch.force_edgecolor'] or self._edge_default:
894-
ec = self._get_default_edgecolor()
895-
else:
896-
ec = 'none'
897-
if not isinstance(ec, str):
898-
ec = 'array'
899-
900-
# Handle special cases.
901-
if fc == 'none':
902-
self._face_is_mapped = False
903-
self._edge_is_mapped = True
904-
elif ec == 'face':
905-
self._edge_is_mapped = True
879+
self._edge_is_mapped = False
880+
self._face_is_mapped = False
881+
if self._A is not None:
882+
if not cbook._str_equal(self._original_facecolor, 'none'):
906883
self._face_is_mapped = True
884+
if cbook._str_equal(self._original_edgecolor, 'face'):
885+
self._edge_is_mapped = True
886+
else:
887+
if self._original_edgecolor is None:
888+
self._edge_is_mapped = True
907889

908890
mapped = self._face_is_mapped or self._edge_is_mapped
909-
changed = (self._edge_is_mapped != edge0
891+
changed = (edge0 is None or face0 is None
892+
or self._edge_is_mapped != edge0
910893
or self._face_is_mapped != face0)
911894
return mapped or changed
912895

@@ -1466,6 +1449,7 @@ def __init__(self, segments, # Can be None.
14661449
"arguments is deprecated, and they will become keyword-only "
14671450
"arguments %(removal)s."
14681451
)
1452+
kwargs.setdefault('facecolors', 'none')
14691453
super().__init__(
14701454
zorder=zorder,
14711455
**kwargs)

lib/matplotlib/tests/test_collections.py

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -754,49 +754,70 @@ def test_legend_inverse_size_label_relationship():
754754
@pytest.mark.style('default')
755755
@pytest.mark.parametrize('pcfunc', [plt.pcolor, plt.pcolormesh])
756756
def test_color_logic(pcfunc):
757-
rgba_none = mcolors.to_rgba_array('none')
758757
z = np.arange(12).reshape(3, 4)
758+
# Explicitly set an edgecolor.
759759
pc = pcfunc(z, edgecolors='red', facecolors='none')
760+
pc.update_scalarmappable() # This is called in draw().
761+
# Define 2 reference "colors" here for multiple use.
760762
face_default = mcolors.to_rgba_array(pc._get_default_facecolor())
761-
assert_array_equal(pc.get_edgecolor(), [[1, 0, 0, 1]])
763+
mapped = pc.get_cmap()(pc.norm((z.ravel())))
764+
# Github issue #1302:
765+
assert mcolors.same_color(pc.get_edgecolor(), 'red')
762766
# Check setting attributes after initialization:
763767
pc = pcfunc(z)
764768
pc.set_facecolor('none')
765769
pc.set_edgecolor('red')
766-
assert_array_equal(pc.get_edgecolor(), [[1, 0, 0, 1]])
770+
pc.update_scalarmappable()
771+
assert mcolors.same_color(pc.get_edgecolor(), [[1, 0, 0, 1]])
767772
pc.set_alpha(0.5)
768-
assert_array_equal(pc.get_edgecolor(), [[1, 0, 0, 0.5]])
769-
pc.set_edgecolor(None) # reset to default
773+
pc.update_scalarmappable()
774+
assert mcolors.same_color(pc.get_edgecolor(), [[1, 0, 0, 0.5]])
770775
pc.set_alpha(None) # restore default alpha
771776
pc.update_scalarmappable()
772-
assert pc.get_edgecolor().shape == (12, 4) # color-mapped
773-
pc.set_facecolor(None)
777+
assert mcolors.same_color(pc.get_edgecolor(), [[1, 0, 0, 1]])
778+
# Reset edgecolor to default.
779+
pc.set_edgecolor(None)
780+
pc.update_scalarmappable()
781+
assert mcolors.same_color(pc.get_edgecolor(), mapped)
782+
pc.set_facecolor(None) # restore default for facecolor
774783
pc.update_scalarmappable()
775-
assert pc.get_facecolor().shape == (12, 4) # color-mapped
776-
assert_array_equal(pc.get_edgecolor(), rgba_none) # default: 'none'
784+
assert mcolors.same_color(pc.get_facecolor(), mapped)
785+
assert mcolors.same_color(pc.get_edgecolor(), 'none')
777786
# Turn off colormapping entirely:
778787
pc.set_array(None)
779788
pc.update_scalarmappable()
780-
assert_array_equal(pc.get_edgecolor(), rgba_none)
781-
assert pc.get_facecolor().shape == (1, 4) # no longer color-mapped
782-
assert_array_equal(pc.get_facecolor(), face_default)
789+
assert mcolors.same_color(pc.get_edgecolor(), 'none')
790+
assert mcolors.same_color(pc.get_facecolor(), face_default) # not mapped
783791
# Turn it back on by restoring the array (must be 1D!):
784792
pc.set_array(z.ravel())
785793
pc.update_scalarmappable()
786-
assert pc.get_facecolor().shape == (12, 4) # color-mapped
787-
assert_array_equal(pc.get_edgecolor(), rgba_none)
794+
assert mcolors.same_color(pc.get_facecolor(), mapped)
795+
assert mcolors.same_color(pc.get_edgecolor(), 'none')
788796
# Give color via tuple rather than string.
789797
pc = pcfunc(z, edgecolors=(1, 0, 0), facecolors=(0, 1, 0))
790-
assert_array_equal(pc.get_facecolor(), [[0, 1, 0, 1]])
791-
assert_array_equal(pc.get_edgecolor(), [[1, 0, 0, 1]])
792-
# Provide an RGB array.
798+
pc.update_scalarmappable()
799+
assert mcolors.same_color(pc.get_facecolor(), mapped)
800+
assert mcolors.same_color(pc.get_edgecolor(), [[1, 0, 0, 1]])
801+
# Provide an RGB array; mapping overrides it.
793802
pc = pcfunc(z, edgecolors=(1, 0, 0), facecolors=np.ones((12, 3)))
794-
assert_array_equal(pc.get_facecolor(), np.ones((12, 4)))
795-
assert_array_equal(pc.get_edgecolor(), [[1, 0, 0, 1]])
803+
pc.update_scalarmappable()
804+
assert mcolors.same_color(pc.get_facecolor(), mapped)
805+
assert mcolors.same_color(pc.get_edgecolor(), [[1, 0, 0, 1]])
806+
# Turn off the mapping.
807+
pc.set_array(None)
808+
pc.update_scalarmappable()
809+
assert mcolors.same_color(pc.get_facecolor(), np.ones((12, 3)))
810+
assert mcolors.same_color(pc.get_edgecolor(), [[1, 0, 0, 1]])
796811
# And an RGBA array.
797812
pc = pcfunc(z, edgecolors=(1, 0, 0), facecolors=np.ones((12, 4)))
798-
assert_array_equal(pc.get_facecolor(), np.ones((12, 4)))
799-
assert_array_equal(pc.get_edgecolor(), [[1, 0, 0, 1]])
813+
pc.update_scalarmappable()
814+
assert mcolors.same_color(pc.get_facecolor(), mapped)
815+
assert mcolors.same_color(pc.get_edgecolor(), [[1, 0, 0, 1]])
816+
# Turn off the mapping.
817+
pc.set_array(None)
818+
pc.update_scalarmappable()
819+
assert mcolors.same_color(pc.get_facecolor(), np.ones((12, 4)))
820+
assert mcolors.same_color(pc.get_edgecolor(), [[1, 0, 0, 1]])
800821

801822

802823
def test_LineCollection_args():

0 commit comments

Comments
 (0)
0