8000 Fix legend of colour-mapped scatter plots. · matplotlib/matplotlib@7c1b2c0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7c1b2c0

Browse files
committed
Fix legend of colour-mapped scatter plots.
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.
1 parent 81c2a22 commit 7c1b2c0

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

lib/matplotlib/collections.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,8 +906,11 @@ def update_scalarmappable(self):
906906
"""
907907
if not self._set_mappable_flags():
908908
return
909+
# We don't use the result of this call, but update the status for
910+
# anyone watching.
911+
self._check_update("array")
909912
# Allow possibility to call 'self.set_array(None)'.
910-
if self._check_update("array") and self._A is not None:
913+
if self._A is not None:
911914
# QuadMesh can map 2d arrays (but pcolormesh supplies 1d array)
912915
if self._A.ndim > 1 and not isinstance(self, QuadMesh):
913916
raise ValueError('Collections can only map rank 1 arrays')

lib/matplotlib/tests/test_axes.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,16 @@ def test_get_labels():
5252
@check_figures_equal()
5353
def test_label_loc_vertical(fig_test, fig_ref):
5454
ax = fig_test.subplots()
55-
sc = ax.scatter([1, 2], [1, 2], c=[1, 2])
55+
sc = ax.scatter([1, 2], [1, 2], c=[1, 2], label='scatter')
56+
ax.legend()
5657
ax.set_ylabel('Y Label', loc='top')
5758
ax.set_xlabel('X Label', loc='right')
5859
cbar = fig_test.colorbar(sc)
5960
cbar.set_label("Z Label", loc='top')
6061

6162
ax = fig_ref.subplots()
62-
sc = ax.scatter([1, 2], [1, 2], c=[1, 2])
63+
sc = ax.scatter([1, 2], [1, 2], c=[1, 2], label='scatter')
64+
ax.legend()
6365
ax.set_ylabel('Y Label', y=1, ha='right')
6466
ax.set_xlabel('X Label', x=1, ha='right')
6567
cbar = fig_ref.colorbar(sc)
@@ -69,14 +71,16 @@ def test_label_loc_vertical(fig_test, fig_ref):
6971
@check_figures_equal()
7072
def test_label_loc_horizontal(fig_test, fig_ref):
7173
ax = fig_test.subplots()
72-
sc = ax.scatter([1, 2], [1, 2], c=[1, 2])
74+
sc = ax.scatter([1, 2], [1, 2], c=[1, 2], label='scatter')
75+
ax.legend()
7376
ax.set_ylabel('Y Label', loc='bottom')
7477
ax.set_xlabel('X Label', loc='left')
7578
cbar = fig_test.colorbar(sc, orientation='horizontal')
7679
cbar.set_label("Z Label", loc='left')
7780

7881
ax = fig_ref.subplots()
79-
sc = ax.scatter([1, 2], [1, 2], c=[1, 2])
82+
sc = ax.scatter([1, 2], [1, 2], c=[1, 2], label='scatter')
83+
ax.legend()
8084
ax.set_ylabel('Y Label', y=0, ha='left')
8185
ax.set_xlabel('X Label', x=0, ha='left')
8286
cbar = fig_ref.colorbar(sc, orientation='horizontal')
@@ -88,14 +92,16 @@ def test_label_loc_rc(fig_test, fig_ref):
8892
with matplotlib.rc_context({"xaxis.labellocation": "right",
8993
"yaxis.labellocation": "top"}):
9094
ax = fig_test.subplots()
91-
sc = ax.scatter([1, 2], [1, 2], c=[1, 2])
95+
sc = ax.scatter([1, 2], [1, 2], c=[1, 2], label='scatter')
96+
ax.legend()
9297
ax.set_ylabel('Y Label')
9398
ax.set_xlabel('X Label')
9499
cbar = fig_test.colorbar(sc, orientation='horizontal')
95100
cbar.set_label("Z Label")
96101

97102
ax = fig_ref.subplots()
98-
sc = ax.scatter([1, 2], [1, 2], c=[1, 2])
103+
sc = ax.scatter([1, 2], [1, 2], c=[1, 2], label='scatter')
104+
ax.legend()
99105
ax.set_ylabel('Y Label', y=1, ha='right')
100106
ax.set_xlabel('X Label', x=1, ha='right')
101107
cbar = fig_ref.colorbar(sc, orientation='horizontal')

0 commit comments

Comments
 (0)
0