Skip to content

Commit 4efe208

Browse files
authored
TST: Clean plotting/test_misc.py (#53864)
clean plotting test_misc
1 parent 281c6fc commit 4efe208

File tree

1 file changed

+60
-22
lines changed

1 file changed

+60
-22
lines changed

pandas/tests/plotting/test_misc.py

+60-22
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,21 @@ def test_scatter_matrix_axis(self, pass_axis):
117117
ax=ax,
118118
)
119119
axes0_labels = axes[0][0].yaxis.get_majorticklabels()
120-
121120
# GH 5662
122121
expected = ["-2", "0", "2"]
123122
_check_text_labels(axes0_labels, expected)
124123
_check_ticks_props(axes, xlabelsize=8, xrot=90, ylabelsize=8, yrot=0)
125124

125+
@td.skip_if_no_scipy
126+
@pytest.mark.parametrize("pass_axis", [False, True])
127+
def test_scatter_matrix_axis_smaller(self, pass_axis):
128+
scatter_matrix = plotting.scatter_matrix
129+
130+
ax = None
131+
if pass_axis:
132+
_, ax = mpl.pyplot.subplots(3, 3)
133+
134+
df = DataFrame(np.random.RandomState(42).randn(100, 3))
126135
df[0] = (df[0] - 2) / 3
127136

128137
# we are plotting multiples on a sub-plot
@@ -299,38 +308,47 @@ def test_parallel_coordinates_with_sorted_labels(self):
299308
# labels and colors are ordered strictly increasing
300309
assert prev[1] < nxt[1] and prev[0] < nxt[0]
301310

302-
def test_radviz(self, iris):
303-
from matplotlib import cm
304-
311+
def test_radviz_no_warning(self, iris):
305312
from pandas.plotting import radviz
306313

307314
df = iris
308315
# Ensure no UserWarning when making plot
309316
with tm.assert_produces_warning(None):
310317
_check_plot_works(radviz, frame=df, class_column="Name")
311318

312-
rgba = ("#556270", "#4ECDC4", "#C7F464")
313-
ax = _check_plot_works(radviz, frame=df, class_column="Name", color=rgba)
319+
@pytest.mark.parametrize(
320+
"color",
321+
[("#556270", "#4ECDC4", "#C7F464"), ["dodgerblue", "aquamarine", "seagreen"]],
322+
)
323+
def test_radviz_color(self, iris, color):
324+
from pandas.plotting import radviz
325+
326+
df = iris
327+
ax = _check_plot_works(radviz, frame=df, class_column="Name", color=color)
314328
# skip Circle drawn as ticks
315329
patches = [p for p in ax.patches[:20] if p.get_label() != ""]
316-
_check_colors(patches[:10], facecolors=rgba, mapping=df["Name"][:10])
330+
_check_colors(patches[:10], facecolors=color, mapping=df["Name"][:10])
317331

318-
cnames = ["dodgerblue", "aquamarine", "seagreen"]
319-
_check_plot_works(radviz, frame=df, class_column="Name", color=cnames)
320-
patches = [p for p in ax.patches[:20] if p.get_label() != ""]
321-
_check_colors(patches, facecolors=cnames, mapping=df["Name"][:10])
332+
def test_radviz_color_cmap(self, iris):
333+
from matplotlib import cm
334+
335+
from pandas.plotting import radviz
322336

323-
_check_plot_works(radviz, frame=df, class_column="Name", colormap=cm.jet)
337+
df = iris
338+
ax = _check_plot_works(radviz, frame=df, class_column="Name", colormap=cm.jet)
324339
cmaps = [cm.jet(n) for n in np.linspace(0, 1, df["Name"].nunique())]
325340
patches = [p for p in ax.patches[:20] if p.get_label() != ""]
326341
_check_colors(patches, facecolors=cmaps, mapping=df["Name"][:10])
327342

343+
def test_radviz_colors_handles(self):
344+
from pandas.plotting import radviz
345+
328346
colors = [[0.0, 0.0, 1.0, 1.0], [0.0, 0.5, 1.0, 1.0], [1.0, 0.0, 0.0, 1.0]]
329347
df = DataFrame(
330348
{"A": [1, 2, 3], "B": [2, 1, 3], "C": [3, 2, 1], "Name": ["b", "g", "r"]}
331349
)
332350
ax = radviz(df, "Name", color=colors)
333-
handles, labels = ax.get_legend_handles_labels()
351+
handles, _ = ax.get_legend_handles_labels()
334352
_check_colors(handles, facecolors=colors)
335353

336354
def test_subplot_titles(self, iris):
@@ -342,6 +360,10 @@ def test_subplot_titles(self, iris):
342360
plot = df.plot(subplots=True, title=title)
343361
assert [p.get_title() for p in plot] == title
344362

363+
def test_subplot_titles_too_much(self, iris):
364+
df = iris.drop("Name", axis=1).head()
365+
# Use the column names as the subplot titles
366+
title = list(df.columns)
345367
# Case len(title) > len(df)
346368
msg = (
347369
"The length of `title` must equal the number of columns if "
@@ -350,10 +372,22 @@ def test_subplot_titles(self, iris):
350372
with pytest.raises(ValueError, match=msg):
351373
df.plot(subplots=True, title=title + ["kittens > puppies"])
352374

375+
def test_subplot_titles_too_little(self, iris):
376+
df = iris.drop("Name", axis=1).head()
377+
# Use the column names as the subplot titles
378+
title = list(df.columns)
379+
msg = (
380+
"The length of `title` must equal the number of columns if "
381+
"using `title` of type `list` and `subplots=True`"
382+
)
353383
# Case len(title) < len(df)
354384
with pytest.raises(ValueError, match=msg):
355385
df.plot(subplots=True, title=title[:2])
356386

387+
def test_subplot_titles_subplots_false(self, iris):
388+
df = iris.drop("Name", axis=1).head()
389+
# Use the column names as the subplot titles
390+
title = list(df.columns)
357391
# Case subplots=False and title is of type list
358392
msg = (
359393
"Using `title` of type `list` is not supported unless "
@@ -362,6 +396,10 @@ def test_subplot_titles(self, iris):
362396
with pytest.raises(ValueError, match=msg):
363397
df.plot(subplots=False, title=title)
364398

399+
def test_subplot_titles_numeric_square_layout(self, iris):
400+
df = iris.drop("Name", axis=1).head()
401+
# Use the column names as the subplot titles
402+
title = list(df.columns)
365403
# Case df with 3 numeric columns but layout of (2,2)
366404
plot = df.drop("SepalWidth", axis=1).plot(
367405
subplots=True, layout=(2, 2), title=title[:-1]
@@ -380,6 +418,8 @@ def test_get_standard_colors_random_seed(self):
380418
rand2 = np.random.random()
381419
assert rand1 != rand2
382420

421+
def test_get_standard_colors_consistency(self):
422+
# GH17525
383423
# Make sure it produces the same colors every time it's called
384424
from pandas.plotting._matplotlib.style import get_standard_colors
385425

@@ -433,7 +473,8 @@ def test_get_standard_colors_no_appending(self):
433473
p = df.A.plot.bar(figsize=(16, 7), color=color_list)
434474
assert p.patches[1].get_facecolor() == p.patches[17].get_facecolor()
435475

436-
def test_dictionary_color(self):
476+
@pytest.mark.parametrize("kind", ["bar", "line"])
477+
def test_dictionary_color(self, kind):
437478
# issue-8193
438479
# Test plot color dictionary format
439480
data_files = ["a", "b"]
@@ -443,14 +484,11 @@ def test_dictionary_color(self):
443484
df1 = DataFrame(np.random.rand(2, 2), columns=data_files)
444485
dic_color = {"b": (0.3, 0.7, 0.7), "a": (0.5, 0.24, 0.6)}
445486

446-
# Bar color test
447-
ax = df1.plot(kind="bar", color=dic_color)
448-
colors = [rect.get_facecolor()[0:-1] for rect in ax.get_children()[0:3:2]]
449-
assert all(color == expected[index] for index, color in enumerate(colors))
450-
451-
# Line color test
452-
ax = df1.plot(kind="line", color=dic_color)
453-
colors = [rect.get_color() for rect in ax.get_lines()[0:2]]
487+
ax = df1.plot(kind=kind, color=dic_color)
488+
if kind == "bar":
489+
colors = [rect.get_facecolor()[0:-1] for rect in ax.get_children()[0:3:2]]
490+
else:
491+
colors = [rect.get_color() for rect in ax.get_lines()[0:2]]
454492
assert all(color == expected[index] for index, color in enumerate(colors))
455493

456494
def test_bar_plot(self):

0 commit comments

Comments
 (0)