Skip to content

BUG/PLT: Multiple plots with different cmap, colorbars legends use first cmap #33392

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 11 commits into from
Apr 10, 2020
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ Plotting
- :func:`.plot` for line/bar now accepts color by dictonary (:issue:`8193`).
-
- 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.plot.scatter` that when adding multiple plots with different ``cmap``, colorbars alway use the first ``cmap`` (:issue:`33389`)


Groupby/resample/rolling
Expand Down
6 changes: 5 additions & 1 deletion pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,11 @@ def _plot_colorbar(self, ax, **kwds):
# For a more detailed description of the issue
# see the following link:
# https://github.com/ipython/ipython/issues/11215
img = ax.collections[0]

# GH33389, if ax is used multiple times, we should always
# use the last one which contains the latest information
# about the ax
img = ax.collections[-1]
cbar = self.fig.colorbar(img, ax=ax, **kwds)

if _mpl_ge_3_0_0():
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/plotting/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,20 @@ def test_scatter_colors(self):
np.array([1, 1, 1, 1], dtype=np.float64),
)

def test_scatter_colorbar_different_cmap(self):
# GH 33389
import matplotlib.pyplot as plt

df = pd.DataFrame({"x": [1, 2, 3], "y": [1, 3, 2], "c": [1, 2, 3]})
df["x2"] = df["x"] + 1

fig, ax = plt.subplots()
df.plot("x", "y", c="c", kind="scatter", cmap="cividis", ax=ax)
df.plot("x2", "y", c="c", kind="scatter", cmap="magma", ax=ax)

assert ax.collections[0].cmap.name == "cividis"
assert ax.collections[1].cmap.name == "magma"

@pytest.mark.slow
def test_plot_bar(self):
df = DataFrame(
Expand Down