Thanks to visit codestin.com
Credit goes to github.com

Skip to content

List of lists of categorical data failing: Scatter ravel is performed before _process_unit_info() is called. #27035

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4657,14 +4657,14 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
# further processed by the rest of the function
linewidths = kwargs.pop('linewidth', None)
edgecolors = kwargs.pop('edgecolor', None)
# Process **kwargs to handle aliases, conflicts with explicit kwargs:
x, y = self._process_unit_info([("x", x), ("y", y)], kwargs)
# np.ma.ravel yields an ndarray, not a masked array,
# unless its argument is a masked array.
x = np.ma.ravel(x)
y = np.ma.ravel(y)
if x.size != y.size:
raise ValueError("x and y must be the same size")
# Process **kwargs to handle aliases, conflicts with explicit kwargs:
x, y = self._process_unit_info([("x", x), ("y", y)], kwargs)

if s is None:
s = (20 if mpl.rcParams['_internal.classic_mode'] else
Expand Down
34 changes: 33 additions & 1 deletion lib/matplotlib/tests/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,13 @@ def test_update_plot(self, plotter):
failing_test_cases = [("mixed", ['A', 3.14]),
("number integer", ['1', 1]),
("string integer", ['42', 42]),
("nested categorical", [["a", "b"], ["c", "d"]]),
("missing", ['12', np.nan])]

fids, fvalues = zip(*failing_test_cases)

plotters = [Axes.scatter, Axes.bar,
plotters = [pytest.param(Axes.scatter, marks=pytest.mark.xfail),
Axes.bar,
pytest.param(Axes.plot, marks=pytest.mark.xfail)]

@pytest.mark.parametrize("plotter", plotters)
Expand Down Expand Up @@ -321,3 +323,33 @@ def test_set_lim():
ax.plot(["a", "b", "c", "d"], [1, 2, 3, 4])
with warnings.catch_warnings():
ax.set_xlim("b", "c")


categorical_examples = [("nested categorical", [["a", "b"], ["c", "d"]]),
("nested with nan", [['0', np.nan], ["aa", "bb"]]),
("nested mixed", [[1, 'a'], ['b', np.nan]])]
Copy link
Member

Choose a reason for hiding this comment

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

By mixed, I meant what happens for [[1,2], ['a', 'b']] -> they all get cased to the same type b/c they get raveled into one list, so what happens then?

cids, cvalues = zip(*categorical_examples)


@pytest.mark.parametrize("xdata", cvalues, ids=cids)
@pytest.mark.parametrize("ydata", cvalues, ids=cids)
def test_nested_categorical(xdata, ydata):
ax = plt.figure().subplots()
ax.scatter(xdata, ydata)

xtexts = [xelement._text for xelement in ax.get_xticklabels()]

assert np.all(xtexts == np.ma.ravel(xdata))


@pytest.mark.parametrize("xdata", cvalues, ids=cids)
def test_nested_categorical_and_numerical(xdata):
ydata = [[0, 1], [2, 3]]
ax = plt.figure().subplots()
splot = ax.scatter(xdata, ydata)

xtexts = [xelement._text for xelement in ax.get_xticklabels()]
y_offset_processed = list(zip(*splot.get_offsets()))[1]

assert np.all(xtexts == np.ma.ravel(xdata))
assert np.all(np.ma.ravel(ydata) == y_offset_processed)