8000 Major reworking; mapping has priority. · matplotlib/matplotlib@a3e04d9 · 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 a3e04d9

Browse files
committed
Major reworking; mapping has priority.
1 parent a257f7c commit a3e04d9

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
@@ -6112,7 +6112,7 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
61126112
if shading is None:
61136113
shading = rcParams['pcolor.shading']
61146114
shading = shading.lower()
6115-
kwargs.setdefault('edgecolors', 'None')
6115+
kwargs.setdefault('edgecolors', 'none')
61166116

61176117
X, Y, C, shading = self._pcolorargs('pcolormesh', *args,
61186118
shading=shading, kwargs=kwargs)

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)
@@ -802,7 +802,9 @@ def _get_default_edgecolor(self):
802802
def _set_edgecolor(self, c):
803803
set_hatch_color = True
804804
if c is None:
805-
if mpl.rcParams['patch.force_edgecolor'] or self._edge_default:
805+
if (mpl.rcParams['patch.force_edgecolor']
806+
or self._edge_default
807+
or cbook._str_equal(self._original_facecolor, 'none')):
806808
c = self._get_default_edgecolor()
807809
else:
808810
c = 'none'
@@ -873,39 +875,20 @@ def _set_mappable_flags(self):
873875
"""
874876
edge0 = self._edge_is_mapped
875877
face0 = self._face_is_mapped
876-
if self._A is None:
877-
self._edge_is_mapped = False
878-
self._face_is_mapped = False
879-
else:
880-
# Typical mapping: faces, not edges.
881-
self._face_is_mapped = True
882-
self._edge_is_mapped = False
883-
884-
# Prepare color strings to check for special cases.
885-
fc = self._original_facecolor
886-
if fc is None:
887-
fc = self._get_default_facecolor()
888-
if not isinstance(fc, str):
889-
fc = 'array'
890-
ec = self._original_edgecolor
891-
if ec is None:
892-
if mpl.rcParams['patch.force_edgecolor'] or self._edge_default:
893-
ec = self._get_default_edgecolor()
894-
else:
895-
ec = 'none'
896-
if not isinstance(ec, str):
897-
ec = 'array'
898-
899-
# Handle special cases.
900-
if fc == 'none':
901-
self._face_is_mapped = False
902-
self._edge_is_mapped = True
903-
elif ec == 'face':
904-
self._edge_is_mapped = True
878+
self._edge_is_mapped = False
879+
self._face_is_mapped = False
880+
if self._A is not None:
881+
if not cbook._str_equal(self._original_facecolor, 'none'):
905882
self._face_is_mapped = True
883+
if cbook._str_equal(self._original_edgecolor, 'face'):
884+
self._edge_is_mapped = True
885+
else:
886+
9E81 if self._original_edgecolor is None:
887+
self._edge_is_mapped = True
906888

907889
mapped = self._face_is_mapped or self._edge_is_mapped
908-
changed = (self._edge_is_mapped != edge0
890+
changed = (edge0 is None or face0 is None
891+
or self._edge_is_mapped != edge0
909892
or self._face_is_mapped != face0)
910893
return mapped or changed
911894

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

lib/matplotlib/tests/test_collections.py

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -752,49 +752,70 @@ def test_legend_inverse_size_label_relationship():
752752
@pytest.mark.style('default')
753753
@pytest.mark.parametrize('pcfunc', [plt.pcolor, plt.pcolormesh])
754754
def test_color_logic(pcfunc):
755-
rgba_none = mcolors.to_rgba_array('none')
756755
z = np.arange(12).reshape(3, 4)
756+
# Explicitly set an edgecolor.
757757
pc = pcfunc(z, edgecolors='red', facecolors='none')
758+
pc.update_scalarmappable() # This is called in draw().
759+
# Define 2 reference "colors" here for multiple use.
758760
face_default = mcolors.to_rgba_array(pc._get_default_facecolor())
759-
assert_array_equal(pc.get_edgecolor(), [[1, 0, 0, 1]])
761+
mapped = pc.get_cmap()(pc.norm((z.ravel())))
762+
# Github issue #1302:
763+
assert mcolors.same_color(pc.get_edgecolor(), 'red')
760764
# Check setting attributes after initialization:
761765
pc = pcfunc(z)
762766
pc.set_facecolor('none')
763767
pc.set_edgecolor('red')
764-
assert_array_equal(pc.get_edgecolor(), [[1, 0, 0, 1]])
768+
pc.update_scalarmappable()
769+
assert mcolors.same_color(pc.get_edgecolor(), [[1, 0, 0, 1]])
765770
pc.set_alpha(0.5)
766-
assert_array_equal(pc.get_edgecolor(), [[1, 0, 0, 0.5]])
767-
pc.set_edgecolor(None) # reset to default
771+
pc.update_scalarmappable()
772+
assert mcolors.same_color(pc.get_edgecolor(), [[1, 0, 0, 0.5]])
768773
pc.set_alpha(None) # restore default alpha
769774
pc.update_scalarmappable()
770-
assert pc.get_edgecolor().shape == (12, 4) # color-mapped
771-
pc.set_facecolor(None)
775+
assert mcolors.same_color(pc.get_edgecolor(), [[1, 0, 0, 1]])
776+
# Reset edgecolor to default.
777+
pc.set_edgecolor(None)
778+
pc.update_scalarmappable()
779+
assert mcolors.same_color(pc.get_edgecolor(), mapped)
780+
pc.set_facecolor(None) # restore default for facecolor
772781
pc.update_scalarmappable()
773-
assert pc.get_facecolor().shape == (12, 4) # color-mapped
774-
assert_array_equal(pc.get_edgecolor(), rgba_none) # default: 'none'
782+
assert mcolors.same_color(pc.get_facecolor(), mapped)
783+
assert mcolors.same_color(pc.get_edgecolor(), 'none')
775784
# Turn off colormapping entirely:
776785
pc.set_array(None)
777786
pc.update_scalarmappable()
778-
assert_array_equal(pc.get_edgecolor(), rgba_none)
779-
assert pc.get_facecolor().shape == (1, 4) # no longer color-mapped
780-
assert_array_equal(pc.get_facecolor(), face_default)
787+
assert mcolors.same_color(pc.get_edgecolor(), 'none')
788+
assert mcolors.same_color(pc.get_facecolor(), face_default) # not mapped
781789
# Turn it back on by restoring the array (must be 1D!):
782790
pc.set_array(z.ravel())
783791
pc.update_scalarmappable()
784-
assert pc.get_facecolor().shape == (12, 4) # color-mapped
785-
assert_array_equal(pc.get_edgecolor(), rgba_none)
792+
assert mcolors.same_color(pc.get_facecolor(), mapped)
793+
assert mcolors.same_color(pc.get_edgecolor(), 'none')
786794
# Give color via tuple rather than string.
787795
pc = pcfunc(z, edgecolors=(1, 0, 0), facecolors=(0, 1, 0))
788-
assert_array_equal(pc.get_facecolor(), [[0, 1, 0, 1]])
789-
assert_array_equal(pc.get_edgecolor(), [[1, 0, 0, 1]])
790-
# Provide an RGB array.
796+
pc.update_scalarmappable()
797+
assert mcolors.same_color(pc.get_facecolor(), mapped)
798+
assert mcolors.same_color(pc.get_edgecolor(), [[1, 0, 0, 1]])
799+
# Provide an RGB array; mapping overrides it.
791800
pc = pcfunc(z, edgecolors=(1, 0, 0), facecolors=np.ones((12, 3)))
792-
assert_array_equal(pc.get_facecolor(), np.ones((12, 4)))
793-
assert_array_equal(pc.get_edgecolor(), [[1, 0, 0, 1]])
801+
pc.update_scalarmappable()
802+
assert mcolors.same_color(pc.get_facecolor(), mapped)
803+
assert mcolors.same_color(pc.get_edgecolor(), [[1, 0, 0, 1]])
804+
# Turn off the mapping.
805+
pc.set_array(None)
806+
pc.update_scalarmappable()
807+
assert mcolors.same_color(pc.get_facecolor(), np.ones((12, 3)))
808+
assert mcolors.same_color(pc.get_edgecolor(), [[1, 0, 0, 1]])
794809
# And an RGBA array.
795810
pc = pcfunc(z, edgecolors=(1, 0, 0), facecolors=np.ones((12, 4)))
796-
assert_array_equal(pc.get_facecolor(), np.ones((12, 4)))
797-
assert_array_equal(pc.get_edgecolor(), [[1, 0, 0, 1]])
811+
pc.update_scalarmappable()
812+
assert mcolors.same_color(pc.get_facecolor(), mapped)
813+
assert mcolors.same_color(pc.get_edgecolor(), [[1, 0, 0, 1]])
814+
# Turn off the mapping.
815+
pc.set_array(None)
816+
pc.update_scalarmappable()
817+
assert mcolors.same_color(pc.get_facecolor(), np.ones((12, 4)))
818+
assert mcolors.same_color(pc.get_edgecolor(), [[1, 0, 0, 1]])
798819

799820

800821
def test_LineCollection_args():

0 commit comments

Comments
 (0)
0