Skip to content

Commit 98d28e3

Browse files
authored
BUG: don't plot colorbar if c is column containing colors (#34344)
1 parent 74a77b3 commit 98d28e3

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

doc/source/whatsnew/v1.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ Plotting
969969
- Bug in :meth:`DataFrame.boxplot` and :meth:`DataFrame.plot.boxplot` lost color attributes of ``medianprops``, ``whiskerprops``, ``capprops`` and ``medianprops`` (:issue:`30346`)
970970
- Bug in :meth:`DataFrame.hist` where the order of ``column`` argument was ignored (:issue:`29235`)
971971
- Bug in :meth:`DataFrame.plot.scatter` that when adding multiple plots with different ``cmap``, colorbars alway use the first ``cmap`` (:issue:`33389`)
972-
972+
- Bug in :meth:`DataFrame.plot.scatter` was adding a colorbar to the plot even if the argument `c` was assigned to a column containing color names (:issue:`34316`)
973973

974974
Groupby/resample/rolling
975975
^^^^^^^^^^^^^^^^^^^^^^^^

pandas/plotting/_matplotlib/core.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
is_iterator,
1515
is_list_like,
1616
is_number,
17+
is_numeric_dtype,
1718
)
1819
from pandas.core.dtypes.generic import (
1920
ABCDataFrame,
@@ -952,9 +953,6 @@ def _make_plot(self):
952953

953954
c_is_column = is_hashable(c) and c in self.data.columns
954955

955-
# plot a colorbar only if a colormap is provided or necessary
956-
cb = self.kwds.pop("colorbar", self.colormap or c_is_column)
957-
958956
# pandas uses colormap, matplotlib uses cmap.
959957
cmap = self.colormap or "Greys"
960958
cmap = self.plt.cm.get_cmap(cmap)
@@ -970,6 +968,12 @@ def _make_plot(self):
970968
else:
971969
c_values = c
972970

971+
# plot colorbar if
972+
# 1. colormap is assigned, and
973+
# 2.`c` is a column containing only numeric values
974+
plot_colorbar = self.colormap or c_is_column
975+
cb = self.kwds.pop("colorbar", is_numeric_dtype(c_values) and plot_colorbar)
976+
973977
if self.legend and hasattr(self, "label"):
974978
label = self.label
975979
else:

pandas/tests/plotting/test_frame.py

+11
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,17 @@ def test_plot_scatter_with_c(self):
13061306
float_array = np.array([0.0, 1.0])
13071307
df.plot.scatter(x="A", y="B", c=float_array, cmap="spring")
13081308

1309+
@pytest.mark.parametrize("cmap", [None, "Greys"])
1310+
def test_scatter_with_c_column_name_with_colors(self, cmap):
1311+
# https://github.com/pandas-dev/pandas/issues/34316
1312+
df = pd.DataFrame(
1313+
[[5.1, 3.5], [4.9, 3.0], [7.0, 3.2], [6.4, 3.2], [5.9, 3.0]],
1314+
columns=["length", "width"],
1315+
)
1316+
df["species"] = ["r", "r", "g", "g", "b"]
1317+
ax = df.plot.scatter(x=0, y=1, c="species", cmap=cmap)
1318+
assert ax.collections[0].colorbar is None
1319+
13091320
def test_plot_scatter_with_s(self):
13101321
# this refers to GH 32904
13111322
df = DataFrame(np.random.random((10, 3)) * 100, columns=["a", "b", "c"],)

0 commit comments

Comments
 (0)