Skip to content

TST: Clean plotting/test_misc.py #53864

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 2 commits into from
Jun 26, 2023
Merged
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
82 changes: 60 additions & 22 deletions pandas/tests/plotting/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,21 @@ def test_scatter_matrix_axis(self, pass_axis):
ax=ax,
)
axes0_labels = axes[0][0].yaxis.get_majorticklabels()

# GH 5662
expected = ["-2", "0", "2"]
_check_text_labels(axes0_labels, expected)
_check_ticks_props(axes, xlabelsize=8, xrot=90, ylabelsize=8, yrot=0)

@td.skip_if_no_scipy
@pytest.mark.parametrize("pass_axis", [False, True])
def test_scatter_matrix_axis_smaller(self, pass_axis):
scatter_matrix = plotting.scatter_matrix

ax = None
if pass_axis:
_, ax = mpl.pyplot.subplots(3, 3)

df = DataFrame(np.random.RandomState(42).randn(100, 3))
df[0] = (df[0] - 2) / 3

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

def test_radviz(self, iris):
from matplotlib import cm

def test_radviz_no_warning(self, iris):
from pandas.plotting import radviz

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

rgba = ("#556270", "#4ECDC4", "#C7F464")
ax = _check_plot_works(radviz, frame=df, class_column="Name", color=rgba)
@pytest.mark.parametrize(
"color",
[("#556270", "#4ECDC4", "#C7F464"), ["dodgerblue", "aquamarine", "seagreen"]],
)
def test_radviz_color(self, iris, color):
from pandas.plotting import radviz

df = iris
ax = _check_plot_works(radviz, frame=df, class_column="Name", color=color)
# skip Circle drawn as ticks
patches = [p for p in ax.patches[:20] if p.get_label() != ""]
_check_colors(patches[:10], facecolors=rgba, mapping=df["Name"][:10])
_check_colors(patches[:10], facecolors=color, mapping=df["Name"][:10])

cnames = ["dodgerblue", "aquamarine", "seagreen"]
_check_plot_works(radviz, frame=df, class_column="Name", color=cnames)
patches = [p for p in ax.patches[:20] if p.get_label() != ""]
_check_colors(patches, facecolors=cnames, mapping=df["Name"][:10])
def test_radviz_color_cmap(self, iris):
from matplotlib import cm

from pandas.plotting import radviz

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

def test_radviz_colors_handles(self):
from pandas.plotting import radviz

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]]
df = DataFrame(
{"A": [1, 2, 3], "B": [2, 1, 3], "C": [3, 2, 1], "Name": ["b", "g", "r"]}
)
ax = radviz(df, "Name", color=colors)
handles, labels = ax.get_legend_handles_labels()
handles, _ = ax.get_legend_handles_labels()
_check_colors(handles, facecolors=colors)

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

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

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

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

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

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

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

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

# Bar color test
ax = df1.plot(kind="bar", color=dic_color)
colors = [rect.get_facecolor()[0:-1] for rect in ax.get_children()[0:3:2]]
assert all(color == expected[index] for index, color in enumerate(colors))

# Line color test
ax = df1.plot(kind="line", color=dic_color)
colors = [rect.get_color() for rect in ax.get_lines()[0:2]]
ax = df1.plot(kind=kind, color=dic_color)
if kind == "bar":
colors = [rect.get_facecolor()[0:-1] for rect in ax.get_children()[0:3:2]]
else:
colors = [rect.get_color() for rect in ax.get_lines()[0:2]]
assert all(color == expected[index] for index, color in enumerate(colors))

def test_bar_plot(self):
Expand Down