Skip to content

BUG: don't plot colorbar if c is column containing colors #34344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ Plotting
- Bug in :meth:`DataFrame.boxplot` and :meth:`DataFrame.plot.boxplot` lost color attributes of ``medianprops``, ``whiskerprops``, ``capprops`` and ``medianprops`` (:issue:`30346`)
- Bug in :meth:`DataFrame.hist` where the order of ``column`` argument was ignored (:issue:`29235`)
- Bug in :meth:`DataFrame.plot.scatter` that when adding multiple plots with different ``cmap``, colorbars alway use the first ``cmap`` (:issue:`33389`)

- 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`)

Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
13 changes: 10 additions & 3 deletions pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,9 +951,6 @@ def _make_plot(self):

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

# plot a colorbar only if a colormap is provided or necessary
cb = self.kwds.pop("colorbar", self.colormap or c_is_column)

# pandas uses colormap, matplotlib uses cmap.
cmap = self.colormap or "Greys"
cmap = self.plt.cm.get_cmap(cmap)
Expand All @@ -969,6 +966,16 @@ def _make_plot(self):
else:
c_values = c

# plot a colorbar only if a colormap is provided or necessary
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe remove this line and add a more descriptive comment for the next line (c_is_column_not_containing_colors)? this comment does not seem to add too much value to help others understand?

from matplotlib.colors import is_color_like

c_is_column_not_containing_colors = c_is_column and not all(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe currently we should only consider if c_values are numeric, instead of checking if not all are color-like?

because your other PR are still under development, and seems no concrete conclusion on how we will show categorical/color-like in colorbar/legend, so now only having numeric values on colorbar seems most reasonable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good point - thanks for your review, @charlesdong1991 !

np.vectorize(is_color_like)(c_values)
)
cb = self.kwds.pop(
"colorbar", self.colormap or c_is_column_not_containing_colors
)

if self.legend and hasattr(self, "label"):
label = self.label
else:
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/plotting/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,15 @@ def test_plot_scatter_with_c(self):
float_array = np.array([0.0, 1.0])
df.plot.scatter(x="A", y="B", c=float_array, cmap="spring")

def test_scatter_with_c_column_name_with_colors(self):
df = pd.DataFrame(
[[5.1, 3.5], [4.9, 3.0], [7.0, 3.2], [6.4, 3.2], [5.9, 3.0]],
columns=["length", "width"],
)
df["species"] = ["r", "r", "g", "g", "b"]
ax = df.plot.scatter(x=0, y=1, c="species",)
assert ax.collections[0].colorbar is None

def test_plot_scatter_with_s(self):
# this refers to GH 32904
df = DataFrame(np.random.random((10, 3)) * 100, columns=["a", "b", "c"],)
Expand Down