Skip to content

Commit

Permalink
Fix legend of colour-mapped scatter plots.
Browse files Browse the repository at this point in the history
This is a slight backport of ed6d92b,
namely ignoring the array update status when updating the scalar
mappable.

To not break the watchers though, the check must still be called to
update the status.

Fixes #19779.
  • Loading branch information
QuLogic committed Mar 29, 2021
1 parent 81c2a22 commit 7c1b2c0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
5 changes: 4 additions & 1 deletion lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,8 +906,11 @@ def update_scalarmappable(self):
"""
if not self._set_mappable_flags():
return
# We don't use the result of this call, but update the status for
# anyone watching.
self._check_update("array")
# Allow possibility to call 'self.set_array(None)'.
if self._check_update("array") and self._A is not None:
if self._A is not None:
# QuadMesh can map 2d arrays (but pcolormesh supplies 1d array)
if self._A.ndim > 1 and not isinstance(self, QuadMesh):
raise ValueError('Collections can only map rank 1 arrays')
Expand Down
18 changes: 12 additions & 6 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ def test_get_labels():
@check_figures_equal()
def test_label_loc_vertical(fig_test, fig_ref):
ax = fig_test.subplots()
sc = ax.scatter([1, 2], [1, 2], c=[1, 2])
sc = ax.scatter([1, 2], [1, 2], c=[1, 2], label='scatter')
ax.legend()
ax.set_ylabel('Y Label', loc='top')
ax.set_xlabel('X Label', loc='right')
cbar = fig_test.colorbar(sc)
cbar.set_label("Z Label", loc='top')

ax = fig_ref.subplots()
sc = ax.scatter([1, 2], [1, 2], c=[1, 2])
sc = ax.scatter([1, 2], [1, 2], c=[1, 2], label='scatter')
ax.legend()
ax.set_ylabel('Y Label', y=1, ha='right')
ax.set_xlabel('X Label', x=1, ha='right')
cbar = fig_ref.colorbar(sc)
Expand All @@ -69,14 +71,16 @@ def test_label_loc_vertical(fig_test, fig_ref):
@check_figures_equal()
def test_label_loc_horizontal(fig_test, fig_ref):
ax = fig_test.subplots()
sc = ax.scatter([1, 2], [1, 2], c=[1, 2])
sc = ax.scatter([1, 2], [1, 2], c=[1, 2], label='scatter')
ax.legend()
ax.set_ylabel('Y Label', loc='bottom')
ax.set_xlabel('X Label', loc='left')
cbar = fig_test.colorbar(sc, orientation='horizontal')
cbar.set_label("Z Label", loc='left')

ax = fig_ref.subplots()
sc = ax.scatter([1, 2], [1, 2], c=[1, 2])
sc = ax.scatter([1, 2], [1, 2], c=[1, 2], label='scatter')
ax.legend()
ax.set_ylabel('Y Label', y=0, ha='left')
ax.set_xlabel('X Label', x=0, ha='left')
cbar = fig_ref.colorbar(sc, orientation='horizontal')
Expand All @@ -88,14 +92,16 @@ def test_label_loc_rc(fig_test, fig_ref):
with matplotlib.rc_context({"xaxis.labellocation": "right",
"yaxis.labellocation": "top"}):
ax = fig_test.subplots()
sc = ax.scatter([1, 2], [1, 2], c=[1, 2])
sc = ax.scatter([1, 2], [1, 2], c=[1, 2], label='scatter')
ax.legend()
ax.set_ylabel('Y Label')
ax.set_xlabel('X Label')
cbar = fig_test.colorbar(sc, orientation='horizontal')
cbar.set_label("Z Label")

ax = fig_ref.subplots()
sc = ax.scatter([1, 2], [1, 2], c=[1, 2])
sc = ax.scatter([1, 2], [1, 2], c=[1, 2], label='scatter')
ax.legend()
ax.set_ylabel('Y Label', y=1, ha='right')
ax.set_xlabel('X Label', x=1, ha='right')
cbar = fig_ref.colorbar(sc, orientation='horizontal')
Expand Down

0 comments on commit 7c1b2c0

Please sign in to comment.