Skip to content

Commit 6e86858

Browse files
Michael Vincent ManninoMichael Vincent Mannino
authored andcommitted
format
1 parent 609fe40 commit 6e86858

File tree

1 file changed

+31
-35
lines changed

1 file changed

+31
-35
lines changed

pandas/plotting/_matplotlib/core.py

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121

2222
import matplotlib as mpl
2323
import numpy as np
24-
from seaborn._base import (
25-
HueMapping,
26-
VectorPlotter,
27-
)
2824

2925
from pandas._libs import lib
3026
from pandas.errors import AbstractMethodError
@@ -1351,38 +1347,28 @@ def _make_plot(self, fig: Figure) -> None:
13511347
# if a list of non color strings is passed in as c, color points
13521348
# by uniqueness of the strings, such same strings get same color
13531349
create_colors = not self._are_valid_colors(c_values)
1354-
1355-
# Plot as normal
1356-
if not create_colors:
1357-
scatter = ax.scatter(
1358-
data[x].values,
1359-
data[y].values,
1360-
c=c_values,
1361-
label=label,
1362-
cmap=cmap,
1363-
norm=norm,
1364-
s=self.s,
1365-
**self.kwds,
1366-
)
1367-
# Have to custom color
1368-
else:
1369-
scatter = ax.scatter(
1370-
data[x].values,
1371-
data[y].values,
1372-
label=label,
1373-
cmap=cmap,
1374-
norm=norm,
1375-
s=self.s,
1376-
**self.kwds,
1350+
if create_colors:
1351+
color_mapping = self._get_color_mapping(c_values)
1352+
c_values = [color_mapping[s] for s in c_values]
1353+
1354+
# build legend for labeling custom colors
1355+
ax.legend(
1356+
handles=[
1357+
mpl.patches.Circle((0, 0), facecolor=c, label=s)
1358+
for s, c in color_mapping.items()
1359+
]
13771360
)
13781361

1379-
# set colors via Seaborn as it contains all the logic for handling color
1380-
# decision all nicely packaged
1381-
scatter.set_facecolor(
1382-
HueMapping(
1383-
VectorPlotter(data=data, variables={"x": x, "y": y, "hue": c})
1384-
)(c_values)
1385-
)
1362+
scatter = ax.scatter(
1363+
data[x].values,
1364+
data[y].values,
1365+
c=c_values,
1366+
label=label,
1367+
cmap=cmap,
1368+
norm=norm,
1369+
s=self.s,
1370+
**self.kwds,
1371+
)
13861372

13871373
if cb:
13881374
cbar_label = c if c_is_column else ""
@@ -1423,7 +1409,7 @@ def _get_c_values(self, color, color_by_categorical: bool, c_is_column: bool):
14231409
c_values = c
14241410
return c_values
14251411

1426-
def _are_valid_colors(self, c_values: np.ndarray):
1412+
def _are_valid_colors(self, c_values: Series):
14271413
# check if c_values contains strings and if these strings are valid mpl colors.
14281414
# no need to check numerics as these (and mpl colors) will be validated for us
14291415
# in .Axes.scatter._parse_scatter_color_args(...)
@@ -1437,6 +1423,16 @@ def _are_valid_colors(self, c_values: np.ndarray):
14371423
except (TypeError, ValueError) as _:
14381424
return False
14391425

1426+
def _get_color_mapping(self, c_values: Series) -> dict[str, str]:
1427+
unique = np.unique(c_values)
1428+
n_colors = len(unique)
1429+
1430+
# passing `None` here will default to :rc:`image.cmap`
1431+
cmap = mpl.colormaps.get_cmap(self.colormap)
1432+
colors = cmap(np.linspace(0, 1, n_colors)) # RGB tuples
1433+
1434+
return dict(zip(unique, colors))
1435+
14401436
def _get_norm_and_cmap(self, c_values, color_by_categorical: bool):
14411437
c = self.c
14421438
if self.colormap is not None:

0 commit comments

Comments
 (0)