Skip to content

Commit 4ff34ab

Browse files
committed
FIX: Warn when trying to set_facecolor on a array-mapped Collection
Colormapping array data takes precedence and an explicit facecolor is thus ignored. We now issue a warning because this should not pass silently. Closes matplotlib#27555.
1 parent 70d0bf7 commit 4ff34ab

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/matplotlib/collections.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,16 @@ def _get_default_facecolor(self):
778778
return mpl.rcParams['patch.facecolor']
779779

780780
def _set_facecolor(self, c):
781+
if self.get_array() is not None:
782+
# Note: we cannot use self._face_is_mapped here because it is
783+
# lazily calculated in update_scalarmappable(), but the equivalent
784+
# logic there is that colormapping from an array takes precedence
785+
# over an explicit facecolor.
786+
_api.warn_external(
787+
"Setting the facecolor has no effect because there is colormapped "
788+
"array data, which takes precedence. Use `set_array(None)` before "
789+
"setting the facecolor to override this.")
790+
781791
if c is None:
782792
c = self._get_default_facecolor()
783793

lib/matplotlib/tests/test_collections.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,14 @@ def test_add_collection():
333333
assert ax.dataLim.bounds == bounds
334334

335335

336+
def test_collection_facecolor():
337+
pc = plt.figure().subplots().scatter([0, 1, 2], [0, 1, 2], c=[0, 0.5, 1])
338+
with pytest.warns(match="Setting the facecolor has no effect"):
339+
pc.set_facecolor(['r', 'g', 'b'])
340+
# the returned facecolor is the colormapped one:
341+
pc.get_facecolor() == plt.get_cmap()([0, 0.5, 1])
342+
343+
336344
@mpl.style.context('mpl20')
337345
@check_figures_equal(extensions=['png'])
338346
def test_collection_log_datalim(fig_test, fig_ref):

0 commit comments

Comments
 (0)