Skip to content

Commit bdc6f66

Browse files
mzeitlin11luckyvs1
authored andcommitted
TST/REF: consolidate hist tests (pandas-dev#38618)
1 parent d5ff3f2 commit bdc6f66

File tree

2 files changed

+110
-110
lines changed

2 files changed

+110
-110
lines changed

pandas/tests/plotting/test_hist_method.py

+110
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,59 @@ def test_hist_with_legend_raises(self, by):
162162
with pytest.raises(ValueError, match="Cannot use both legend and label"):
163163
s.hist(legend=True, by=by, label="c")
164164

165+
def test_hist_kwargs(self):
166+
_, ax = self.plt.subplots()
167+
ax = self.ts.plot.hist(bins=5, ax=ax)
168+
assert len(ax.patches) == 5
169+
self._check_text_labels(ax.yaxis.get_label(), "Frequency")
170+
tm.close()
171+
172+
_, ax = self.plt.subplots()
173+
ax = self.ts.plot.hist(orientation="horizontal", ax=ax)
174+
self._check_text_labels(ax.xaxis.get_label(), "Frequency")
175+
tm.close()
176+
177+
_, ax = self.plt.subplots()
178+
ax = self.ts.plot.hist(align="left", stacked=True, ax=ax)
179+
tm.close()
180+
181+
@td.skip_if_no_scipy
182+
def test_hist_kde(self):
183+
184+
_, ax = self.plt.subplots()
185+
ax = self.ts.plot.hist(logy=True, ax=ax)
186+
self._check_ax_scales(ax, yaxis="log")
187+
xlabels = ax.get_xticklabels()
188+
# ticks are values, thus ticklabels are blank
189+
self._check_text_labels(xlabels, [""] * len(xlabels))
190+
ylabels = ax.get_yticklabels()
191+
self._check_text_labels(ylabels, [""] * len(ylabels))
192+
193+
_check_plot_works(self.ts.plot.kde)
194+
_check_plot_works(self.ts.plot.density)
195+
_, ax = self.plt.subplots()
196+
ax = self.ts.plot.kde(logy=True, ax=ax)
197+
self._check_ax_scales(ax, yaxis="log")
198+
xlabels = ax.get_xticklabels()
199+
self._check_text_labels(xlabels, [""] * len(xlabels))
200+
ylabels = ax.get_yticklabels()
201+
self._check_text_labels(ylabels, [""] * len(ylabels))
202+
203+
@td.skip_if_no_scipy
204+
def test_hist_kde_color(self):
205+
_, ax = self.plt.subplots()
206+
ax = self.ts.plot.hist(logy=True, bins=10, color="b", ax=ax)
207+
self._check_ax_scales(ax, yaxis="log")
208+
assert len(ax.patches) == 10
209+
self._check_colors(ax.patches, facecolors=["b"] * 10)
210+
211+
_, ax = self.plt.subplots()
212+
ax = self.ts.plot.kde(logy=True, color="r", ax=ax)
213+
self._check_ax_scales(ax, yaxis="log")
214+
lines = ax.get_lines()
215+
assert len(lines) == 1
216+
self._check_colors(lines, ["r"])
217+
165218

166219
@td.skip_if_no_mpl
167220
class TestDataFramePlots(TestPlotBase):
@@ -432,6 +485,63 @@ def test_hist_with_legend_raises(self, by, column):
432485
with pytest.raises(ValueError, match="Cannot use both legend and label"):
433486
df.hist(legend=True, by=by, column=column, label="d")
434487

488+
def test_hist_df_kwargs(self):
489+
df = DataFrame(np.random.randn(10, 2))
490+
_, ax = self.plt.subplots()
491+
ax = df.plot.hist(bins=5, ax=ax)
492+
assert len(ax.patches) == 10
493+
494+
def test_hist_df_with_nonnumerics(self):
495+
# GH 9853
496+
with tm.RNGContext(1):
497+
df = DataFrame(np.random.randn(10, 4), columns=["A", "B", "C", "D"])
498+
df["E"] = ["x", "y"] * 5
499+
_, ax = self.plt.subplots()
500+
ax = df.plot.hist(bins=5, ax=ax)
501+
assert len(ax.patches) == 20
502+
503+
_, ax = self.plt.subplots()
504+
ax = df.plot.hist(ax=ax) # bins=10
505+
assert len(ax.patches) == 40
506+
507+
def test_hist_secondary_legend(self):
508+
# GH 9610
509+
df = DataFrame(np.random.randn(30, 4), columns=list("abcd"))
510+
511+
# primary -> secondary
512+
_, ax = self.plt.subplots()
513+
ax = df["a"].plot.hist(legend=True, ax=ax)
514+
df["b"].plot.hist(ax=ax, legend=True, secondary_y=True)
515+
# both legends are dran on left ax
516+
# left and right axis must be visible
517+
self._check_legend_labels(ax, labels=["a", "b (right)"])
518+
assert ax.get_yaxis().get_visible()
519+
assert ax.right_ax.get_yaxis().get_visible()
520+
tm.close()
521+
522+
# secondary -> secondary
523+
_, ax = self.plt.subplots()
524+
ax = df["a"].plot.hist(legend=True, secondary_y=True, ax=ax)
525+
df["b"].plot.hist(ax=ax, legend=True, secondary_y=True)
526+
# both legends are draw on left ax
527+
# left axis must be invisible, right axis must be visible
528+
self._check_legend_labels(ax.left_ax, labels=["a (right)", "b (right)"])
529+
assert not ax.left_ax.get_yaxis().get_visible()
530+
assert ax.get_yaxis().get_visible()
531+
tm.close()
532+
533+
# secondary -> primary
534+
_, ax = self.plt.subplots()
535+
ax = df["a"].plot.hist(legend=True, secondary_y=True, ax=ax)
536+
# right axes is returned
537+
df["b"].plot.hist(ax=ax, legend=True)
538+
# both legends are draw on left ax
539+
# left and right axis must be visible
540+
self._check_legend_labels(ax.left_ax, labels=["a (right)", "b"])
541+
assert ax.left_ax.get_yaxis().get_visible()
542+
assert ax.get_yaxis().get_visible()
543+
tm.close()
544+
435545

436546
@td.skip_if_no_mpl
437547
class TestDataFrameGroupByPlots(TestPlotBase):

pandas/tests/plotting/test_series.py

-110
Original file line numberDiff line numberDiff line change
@@ -365,25 +365,6 @@ def test_pie_nan(self):
365365
result = [x.get_text() for x in ax.texts]
366366
assert result == expected
367367

368-
def test_hist_df_kwargs(self):
369-
df = DataFrame(np.random.randn(10, 2))
370-
_, ax = self.plt.subplots()
371-
ax = df.plot.hist(bins=5, ax=ax)
372-
assert len(ax.patches) == 10
373-
374-
def test_hist_df_with_nonnumerics(self):
375-
# GH 9853
376-
with tm.RNGContext(1):
377-
df = DataFrame(np.random.randn(10, 4), columns=["A", "B", "C", "D"])
378-
df["E"] = ["x", "y"] * 5
379-
_, ax = self.plt.subplots()
380-
ax = df.plot.hist(bins=5, ax=ax)
381-
assert len(ax.patches) == 20
382-
383-
_, ax = self.plt.subplots()
384-
ax = df.plot.hist(ax=ax) # bins=10
385-
assert len(ax.patches) == 40
386-
387368
def test_hist_legacy(self):
388369
_check_plot_works(self.ts.hist)
389370
_check_plot_works(self.ts.hist, grid=False)
@@ -468,44 +449,6 @@ def test_hist_no_overlap(self):
468449
axes = fig.axes
469450
assert len(axes) == 2
470451

471-
def test_hist_secondary_legend(self):
472-
# GH 9610
473-
df = DataFrame(np.random.randn(30, 4), columns=list("abcd"))
474-
475-
# primary -> secondary
476-
_, ax = self.plt.subplots()
477-
ax = df["a"].plot.hist(legend=True, ax=ax)
478-
df["b"].plot.hist(ax=ax, legend=True, secondary_y=True)
479-
# both legends are dran on left ax
480-
# left and right axis must be visible
481-
self._check_legend_labels(ax, labels=["a", "b (right)"])
482-
assert ax.get_yaxis().get_visible()
483-
assert ax.right_ax.get_yaxis().get_visible()
484-
tm.close()
485-
486-
# secondary -> secondary
487-
_, ax = self.plt.subplots()
488-
ax = df["a"].plot.hist(legend=True, secondary_y=True, ax=ax)
489-
df["b"].plot.hist(ax=ax, legend=True, secondary_y=True)
490-
# both legends are draw on left ax
491-
# left axis must be invisible, right axis must be visible
492-
self._check_legend_labels(ax.left_ax, labels=["a (right)", "b (right)"])
493-
assert not ax.left_ax.get_yaxis().get_visible()
494-
assert ax.get_yaxis().get_visible()
495-
tm.close()
496-
497-
# secondary -> primary
498-
_, ax = self.plt.subplots()
499-
ax = df["a"].plot.hist(legend=True, secondary_y=True, ax=ax)
500-
# right axes is returned
501-
df["b"].plot.hist(ax=ax, legend=True)
502-
# both legends are draw on left ax
503-
# left and right axis must be visible
504-
self._check_legend_labels(ax.left_ax, labels=["a (right)", "b"])
505-
assert ax.left_ax.get_yaxis().get_visible()
506-
assert ax.get_yaxis().get_visible()
507-
tm.close()
508-
509452
def test_df_series_secondary_legend(self):
510453
# GH 9779
511454
df = DataFrame(np.random.randn(30, 3), columns=list("abc"))
@@ -590,28 +533,6 @@ def test_plot_fails_with_dupe_color_and_style(self):
590533
_, ax = self.plt.subplots()
591534
x.plot(style="k--", color="k", ax=ax)
592535

593-
@td.skip_if_no_scipy
594-
def test_hist_kde(self):
595-
596-
_, ax = self.plt.subplots()
597-
ax = self.ts.plot.hist(logy=True, ax=ax)
598-
self._check_ax_scales(ax, yaxis="log")
599-
xlabels = ax.get_xticklabels()
600-
# ticks are values, thus ticklabels are blank
601-
self._check_text_labels(xlabels, [""] * len(xlabels))
602-
ylabels = ax.get_yticklabels()
603-
self._check_text_labels(ylabels, [""] * len(ylabels))
604-
605-
_check_plot_works(self.ts.plot.kde)
606-
_check_plot_works(self.ts.plot.density)
607-
_, ax = self.plt.subplots()
608-
ax = self.ts.plot.kde(logy=True, ax=ax)
609-
self._check_ax_scales(ax, yaxis="log")
610-
xlabels = ax.get_xticklabels()
611-
self._check_text_labels(xlabels, [""] * len(xlabels))
612-
ylabels = ax.get_yticklabels()
613-
self._check_text_labels(ylabels, [""] * len(ylabels))
614-
615536
@td.skip_if_no_scipy
616537
def test_kde_kwargs(self):
617538
sample_points = np.linspace(-100, 100, 20)
@@ -634,37 +555,6 @@ def test_kde_missing_vals(self):
634555
# gh-14821: check if the values have any missing values
635556
assert any(~np.isnan(axes.lines[0].get_xdata()))
636557

637-
def test_hist_kwargs(self):
638-
_, ax = self.plt.subplots()
639-
ax = self.ts.plot.hist(bins=5, ax=ax)
640-
assert len(ax.patches) == 5
641-
self._check_text_labels(ax.yaxis.get_label(), "Frequency")
642-
tm.close()
643-
644-
_, ax = self.plt.subplots()
645-
ax = self.ts.plot.hist(orientation="horizontal", ax=ax)
646-
self._check_text_labels(ax.xaxis.get_label(), "Frequency")
647-
tm.close()
648-
649-
_, ax = self.plt.subplots()
650-
ax = self.ts.plot.hist(align="left", stacked=True, ax=ax)
651-
tm.close()
652-
653-
@td.skip_if_no_scipy
654-
def test_hist_kde_color(self):
655-
_, ax = self.plt.subplots()
656-
ax = self.ts.plot.hist(logy=True, bins=10, color="b", ax=ax)
657-
self._check_ax_scales(ax, yaxis="log")
658-
assert len(ax.patches) == 10
659-
self._check_colors(ax.patches, facecolors=["b"] * 10)
660-
661-
_, ax = self.plt.subplots()
662-
ax = self.ts.plot.kde(logy=True, color="r", ax=ax)
663-
self._check_ax_scales(ax, yaxis="log")
664-
lines = ax.get_lines()
665-
assert len(lines) == 1
666-
self._check_colors(lines, ["r"])
667-
668558
def test_boxplot_series(self):
669559
_, ax = self.plt.subplots()
670560
ax = self.ts.plot.box(logy=True, ax=ax)

0 commit comments

Comments
 (0)