test_misc.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. """ Test cases for misc plot functions """
  2. import numpy as np
  3. import pytest
  4. import pandas.util._test_decorators as td
  5. from pandas import (
  6. DataFrame,
  7. Index,
  8. Series,
  9. Timestamp,
  10. interval_range,
  11. plotting,
  12. )
  13. import pandas._testing as tm
  14. from pandas.tests.plotting.common import (
  15. TestPlotBase,
  16. _check_plot_works,
  17. )
  18. @td.skip_if_mpl
  19. def test_import_error_message():
  20. # GH-19810
  21. df = DataFrame({"A": [1, 2]})
  22. with pytest.raises(ImportError, match="matplotlib is required for plotting"):
  23. df.plot()
  24. def test_get_accessor_args():
  25. func = plotting._core.PlotAccessor._get_call_args
  26. msg = "Called plot accessor for type list, expected Series or DataFrame"
  27. with pytest.raises(TypeError, match=msg):
  28. func(backend_name="", data=[], args=[], kwargs={})
  29. msg = "should not be called with positional arguments"
  30. with pytest.raises(TypeError, match=msg):
  31. func(backend_name="", data=Series(dtype=object), args=["line", None], kwargs={})
  32. x, y, kind, kwargs = func(
  33. backend_name="",
  34. data=DataFrame(),
  35. args=["x"],
  36. kwargs={"y": "y", "kind": "bar", "grid": False},
  37. )
  38. assert x == "x"
  39. assert y == "y"
  40. assert kind == "bar"
  41. assert kwargs == {"grid": False}
  42. x, y, kind, kwargs = func(
  43. backend_name="pandas.plotting._matplotlib",
  44. data=Series(dtype=object),
  45. args=[],
  46. kwargs={},
  47. )
  48. assert x is None
  49. assert y is None
  50. assert kind == "line"
  51. assert len(kwargs) == 24
  52. @td.skip_if_no_mpl
  53. class TestSeriesPlots(TestPlotBase):
  54. def test_autocorrelation_plot(self):
  55. from pandas.plotting import autocorrelation_plot
  56. ser = tm.makeTimeSeries(name="ts")
  57. # Ensure no UserWarning when making plot
  58. with tm.assert_produces_warning(None):
  59. _check_plot_works(autocorrelation_plot, series=ser)
  60. _check_plot_works(autocorrelation_plot, series=ser.values)
  61. ax = autocorrelation_plot(ser, label="Test")
  62. self._check_legend_labels(ax, labels=["Test"])
  63. @pytest.mark.parametrize("kwargs", [{}, {"lag": 5}])
  64. def test_lag_plot(self, kwargs):
  65. from pandas.plotting import lag_plot
  66. ser = tm.makeTimeSeries(name="ts")
  67. _check_plot_works(lag_plot, series=ser, **kwargs)
  68. def test_bootstrap_plot(self):
  69. from pandas.plotting import bootstrap_plot
  70. ser = tm.makeTimeSeries(name="ts")
  71. _check_plot_works(bootstrap_plot, series=ser, size=10)
  72. @td.skip_if_no_mpl
  73. class TestDataFramePlots(TestPlotBase):
  74. @td.skip_if_no_scipy
  75. @pytest.mark.parametrize("pass_axis", [False, True])
  76. def test_scatter_matrix_axis(self, pass_axis):
  77. scatter_matrix = plotting.scatter_matrix
  78. ax = None
  79. if pass_axis:
  80. _, ax = self.plt.subplots(3, 3)
  81. df = DataFrame(np.random.RandomState(42).randn(100, 3))
  82. # we are plotting multiples on a sub-plot
  83. with tm.assert_produces_warning(UserWarning, check_stacklevel=False):
  84. axes = _check_plot_works(
  85. scatter_matrix,
  86. frame=df,
  87. range_padding=0.1,
  88. ax=ax,
  89. )
  90. axes0_labels = axes[0][0].yaxis.get_majorticklabels()
  91. # GH 5662
  92. expected = ["-2", "0", "2"]
  93. self._check_text_labels(axes0_labels, expected)
  94. self._check_ticks_props(axes, xlabelsize=8, xrot=90, ylabelsize=8, yrot=0)
  95. df[0] = (df[0] - 2) / 3
  96. # we are plotting multiples on a sub-plot
  97. with tm.assert_produces_warning(UserWarning, check_stacklevel=False):
  98. axes = _check_plot_works(
  99. scatter_matrix,
  100. frame=df,
  101. range_padding=0.1,
  102. ax=ax,
  103. )
  104. axes0_labels = axes[0][0].yaxis.get_majorticklabels()
  105. expected = ["-1.0", "-0.5", "0.0"]
  106. self._check_text_labels(axes0_labels, expected)
  107. self._check_ticks_props(axes, xlabelsize=8, xrot=90, ylabelsize=8, yrot=0)
  108. @pytest.mark.slow
  109. def test_andrews_curves(self, iris):
  110. from matplotlib import cm
  111. from pandas.plotting import andrews_curves
  112. df = iris
  113. # Ensure no UserWarning when making plot
  114. with tm.assert_produces_warning(None):
  115. _check_plot_works(andrews_curves, frame=df, class_column="Name")
  116. rgba = ("#556270", "#4ECDC4", "#C7F464")
  117. ax = _check_plot_works(
  118. andrews_curves, frame=df, class_column="Name", color=rgba
  119. )
  120. self._check_colors(
  121. ax.get_lines()[:10], linecolors=rgba, mapping=df["Name"][:10]
  122. )
  123. cnames = ["dodgerblue", "aquamarine", "seagreen"]
  124. ax = _check_plot_works(
  125. andrews_curves, frame=df, class_column="Name", color=cnames
  126. )
  127. self._check_colors(
  128. ax.get_lines()[:10], linecolors=cnames, mapping=df["Name"][:10]
  129. )
  130. ax = _check_plot_works(
  131. andrews_curves, frame=df, class_column="Name", colormap=cm.jet
  132. )
  133. cmaps = [cm.jet(n) for n in np.linspace(0, 1, df["Name"].nunique())]
  134. self._check_colors(
  135. ax.get_lines()[:10], linecolors=cmaps, mapping=df["Name"][:10]
  136. )
  137. length = 10
  138. df = DataFrame(
  139. {
  140. "A": np.random.rand(length),
  141. "B": np.random.rand(length),
  142. "C": np.random.rand(length),
  143. "Name": ["A"] * length,
  144. }
  145. )
  146. _check_plot_works(andrews_curves, frame=df, class_column="Name")
  147. rgba = ("#556270", "#4ECDC4", "#C7F464")
  148. ax = _check_plot_works(
  149. andrews_curves, frame=df, class_column="Name", color=rgba
  150. )
  151. self._check_colors(
  152. ax.get_lines()[:10], linecolors=rgba, mapping=df["Name"][:10]
  153. )
  154. cnames = ["dodgerblue", "aquamarine", "seagreen"]
  155. ax = _check_plot_works(
  156. andrews_curves, frame=df, class_column="Name", color=cnames
  157. )
  158. self._check_colors(
  159. ax.get_lines()[:10], linecolors=cnames, mapping=df["Name"][:10]
  160. )
  161. ax = _check_plot_works(
  162. andrews_curves, frame=df, class_column="Name", colormap=cm.jet
  163. )
  164. cmaps = [cm.jet(n) for n in np.linspace(0, 1, df["Name"].nunique())]
  165. self._check_colors(
  166. ax.get_lines()[:10], linecolors=cmaps, mapping=df["Name"][:10]
  167. )
  168. colors = ["b", "g", "r"]
  169. df = DataFrame({"A": [1, 2, 3], "B": [1, 2, 3], "C": [1, 2, 3], "Name": colors})
  170. ax = andrews_curves(df, "Name", color=colors)
  171. handles, labels = ax.get_legend_handles_labels()
  172. self._check_colors(handles, linecolors=colors)
  173. @pytest.mark.slow
  174. def test_parallel_coordinates(self, iris):
  175. from matplotlib import cm
  176. from pandas.plotting import parallel_coordinates
  177. df = iris
  178. ax = _check_plot_works(parallel_coordinates, frame=df, class_column="Name")
  179. nlines = len(ax.get_lines())
  180. nxticks = len(ax.xaxis.get_ticklabels())
  181. rgba = ("#556270", "#4ECDC4", "#C7F464")
  182. ax = _check_plot_works(
  183. parallel_coordinates, frame=df, class_column="Name", color=rgba
  184. )
  185. self._check_colors(
  186. ax.get_lines()[:10], linecolors=rgba, mapping=df["Name"][:10]
  187. )
  188. cnames = ["dodgerblue", "aquamarine", "seagreen"]
  189. ax = _check_plot_works(
  190. parallel_coordinates, frame=df, class_column="Name", color=cnames
  191. )
  192. self._check_colors(
  193. ax.get_lines()[:10], linecolors=cnames, mapping=df["Name"][:10]
  194. )
  195. ax = _check_plot_works(
  196. parallel_coordinates, frame=df, class_column="Name", colormap=cm.jet
  197. )
  198. cmaps = [cm.jet(n) for n in np.linspace(0, 1, df["Name"].nunique())]
  199. self._check_colors(
  200. ax.get_lines()[:10], linecolors=cmaps, mapping=df["Name"][:10]
  201. )
  202. ax = _check_plot_works(
  203. parallel_coordinates, frame=df, class_column="Name", axvlines=False
  204. )
  205. assert len(ax.get_lines()) == (nlines - nxticks)
  206. colors = ["b", "g", "r"]
  207. df = DataFrame({"A": [1, 2, 3], "B": [1, 2, 3], "C": [1, 2, 3], "Name": colors})
  208. ax = parallel_coordinates(df, "Name", color=colors)
  209. handles, labels = ax.get_legend_handles_labels()
  210. self._check_colors(handles, linecolors=colors)
  211. # not sure if this is indicative of a problem
  212. @pytest.mark.filterwarnings("ignore:Attempting to set:UserWarning")
  213. def test_parallel_coordinates_with_sorted_labels(self):
  214. """For #15908"""
  215. from pandas.plotting import parallel_coordinates
  216. df = DataFrame(
  217. {
  218. "feat": list(range(30)),
  219. "class": [2 for _ in range(10)]
  220. + [3 for _ in range(10)]
  221. + [1 for _ in range(10)],
  222. }
  223. )
  224. ax = parallel_coordinates(df, "class", sort_labels=True)
  225. polylines, labels = ax.get_legend_handles_labels()
  226. color_label_tuples = zip(
  227. [polyline.get_color() for polyline in polylines], labels
  228. )
  229. ordered_color_label_tuples = sorted(color_label_tuples, key=lambda x: x[1])
  230. prev_next_tupels = zip(
  231. list(ordered_color_label_tuples[0:-1]), list(ordered_color_label_tuples[1:])
  232. )
  233. for prev, nxt in prev_next_tupels:
  234. # labels and colors are ordered strictly increasing
  235. assert prev[1] < nxt[1] and prev[0] < nxt[0]
  236. def test_radviz(self, iris):
  237. from matplotlib import cm
  238. from pandas.plotting import radviz
  239. df = iris
  240. # Ensure no UserWarning when making plot
  241. with tm.assert_produces_warning(None):
  242. _check_plot_works(radviz, frame=df, class_column="Name")
  243. rgba = ("#556270", "#4ECDC4", "#C7F464")
  244. ax = _check_plot_works(radviz, frame=df, class_column="Name", color=rgba)
  245. # skip Circle drawn as ticks
  246. patches = [p for p in ax.patches[:20] if p.get_label() != ""]
  247. self._check_colors(patches[:10], facecolors=rgba, mapping=df["Name"][:10])
  248. cnames = ["dodgerblue", "aquamarine", "seagreen"]
  249. _check_plot_works(radviz, frame=df, class_column="Name", color=cnames)
  250. patches = [p for p in ax.patches[:20] if p.get_label() != ""]
  251. self._check_colors(patches, facecolors=cnames, mapping=df["Name"][:10])
  252. _check_plot_works(radviz, frame=df, class_column="Name", colormap=cm.jet)
  253. cmaps = [cm.jet(n) for n in np.linspace(0, 1, df["Name"].nunique())]
  254. patches = [p for p in ax.patches[:20] if p.get_label() != ""]
  255. self._check_colors(patches, facecolors=cmaps, mapping=df["Name"][:10])
  256. 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]]
  257. df = DataFrame(
  258. {"A": [1, 2, 3], "B": [2, 1, 3], "C": [3, 2, 1], "Name": ["b", "g", "r"]}
  259. )
  260. ax = radviz(df, "Name", color=colors)
  261. handles, labels = ax.get_legend_handles_labels()
  262. self._check_colors(handles, facecolors=colors)
  263. def test_subplot_titles(self, iris):
  264. df = iris.drop("Name", axis=1).head()
  265. # Use the column names as the subplot titles
  266. title = list(df.columns)
  267. # Case len(title) == len(df)
  268. plot = df.plot(subplots=True, title=title)
  269. assert [p.get_title() for p in plot] == title
  270. # Case len(title) > len(df)
  271. msg = (
  272. "The length of `title` must equal the number of columns if "
  273. "using `title` of type `list` and `subplots=True`"
  274. )
  275. with pytest.raises(ValueError, match=msg):
  276. df.plot(subplots=True, title=title + ["kittens > puppies"])
  277. # Case len(title) < len(df)
  278. with pytest.raises(ValueError, match=msg):
  279. df.plot(subplots=True, title=title[:2])
  280. # Case subplots=False and title is of type list
  281. msg = (
  282. "Using `title` of type `list` is not supported unless "
  283. "`subplots=True` is passed"
  284. )
  285. with pytest.raises(ValueError, match=msg):
  286. df.plot(subplots=False, title=title)
  287. # Case df with 3 numeric columns but layout of (2,2)
  288. plot = df.drop("SepalWidth", axis=1).plot(
  289. subplots=True, layout=(2, 2), title=title[:-1]
  290. )
  291. title_list = [ax.get_title() for sublist in plot for ax in sublist]
  292. assert title_list == title[:3] + [""]
  293. def test_get_standard_colors_random_seed(self):
  294. # GH17525
  295. df = DataFrame(np.zeros((10, 10)))
  296. # Make sure that the np.random.seed isn't reset by get_standard_colors
  297. plotting.parallel_coordinates(df, 0)
  298. rand1 = np.random.random()
  299. plotting.parallel_coordinates(df, 0)
  300. rand2 = np.random.random()
  301. assert rand1 != rand2
  302. # Make sure it produces the same colors every time it's called
  303. from pandas.plotting._matplotlib.style import get_standard_colors
  304. color1 = get_standard_colors(1, color_type="random")
  305. color2 = get_standard_colors(1, color_type="random")
  306. assert color1 == color2
  307. def test_get_standard_colors_default_num_colors(self):
  308. from pandas.plotting._matplotlib.style import get_standard_colors
  309. # Make sure the default color_types returns the specified amount
  310. color1 = get_standard_colors(1, color_type="default")
  311. color2 = get_standard_colors(9, color_type="default")
  312. color3 = get_standard_colors(20, color_type="default")
  313. assert len(color1) == 1
  314. assert len(color2) == 9
  315. assert len(color3) == 20
  316. def test_plot_single_color(self):
  317. # Example from #20585. All 3 bars should have the same color
  318. df = DataFrame(
  319. {
  320. "account-start": ["2017-02-03", "2017-03-03", "2017-01-01"],
  321. "client": ["Alice Anders", "Bob Baker", "Charlie Chaplin"],
  322. "balance": [-1432.32, 10.43, 30000.00],
  323. "db-id": [1234, 2424, 251],
  324. "proxy-id": [525, 1525, 2542],
  325. "rank": [52, 525, 32],
  326. }
  327. )
  328. ax = df.client.value_counts().plot.bar()
  329. colors = [rect.get_facecolor() for rect in ax.get_children()[0:3]]
  330. assert all(color == colors[0] for color in colors)
  331. def test_get_standard_colors_no_appending(self):
  332. # GH20726
  333. # Make sure not to add more colors so that matplotlib can cycle
  334. # correctly.
  335. from matplotlib import cm
  336. from pandas.plotting._matplotlib.style import get_standard_colors
  337. color_before = cm.gnuplot(range(5))
  338. color_after = get_standard_colors(1, color=color_before)
  339. assert len(color_after) == len(color_before)
  340. df = DataFrame(np.random.randn(48, 4), columns=list("ABCD"))
  341. color_list = cm.gnuplot(np.linspace(0, 1, 16))
  342. p = df.A.plot.bar(figsize=(16, 7), color=color_list)
  343. assert p.patches[1].get_facecolor() == p.patches[17].get_facecolor()
  344. def test_dictionary_color(self):
  345. # issue-8193
  346. # Test plot color dictionary format
  347. data_files = ["a", "b"]
  348. expected = [(0.5, 0.24, 0.6), (0.3, 0.7, 0.7)]
  349. df1 = DataFrame(np.random.rand(2, 2), columns=data_files)
  350. dic_color = {"b": (0.3, 0.7, 0.7), "a": (0.5, 0.24, 0.6)}
  351. # Bar color test
  352. ax = df1.plot(kind="bar", color=dic_color)
  353. colors = [rect.get_facecolor()[0:-1] for rect in ax.get_children()[0:3:2]]
  354. assert all(color == expected[index] for index, color in enumerate(colors))
  355. # Line color test
  356. ax = df1.plot(kind="line", color=dic_color)
  357. colors = [rect.get_color() for rect in ax.get_lines()[0:2]]
  358. assert all(color == expected[index] for index, color in enumerate(colors))
  359. def test_bar_plot(self):
  360. # GH38947
  361. # Test bar plot with string and int index
  362. from matplotlib.text import Text
  363. expected = [Text(0, 0, "0"), Text(1, 0, "Total")]
  364. df = DataFrame(
  365. {
  366. "a": [1, 2],
  367. },
  368. index=Index([0, "Total"]),
  369. )
  370. plot_bar = df.plot.bar()
  371. assert all(
  372. (a.get_text() == b.get_text())
  373. for a, b in zip(plot_bar.get_xticklabels(), expected)
  374. )
  375. def test_barh_plot_labels_mixed_integer_string(self):
  376. # GH39126
  377. # Test barh plot with string and integer at the same column
  378. from matplotlib.text import Text
  379. df = DataFrame([{"word": 1, "value": 0}, {"word": "knowledg", "value": 2}])
  380. plot_barh = df.plot.barh(x="word", legend=None)
  381. expected_yticklabels = [Text(0, 0, "1"), Text(0, 1, "knowledg")]
  382. assert all(
  383. actual.get_text() == expected.get_text()
  384. for actual, expected in zip(
  385. plot_barh.get_yticklabels(), expected_yticklabels
  386. )
  387. )
  388. def test_has_externally_shared_axis_x_axis(self):
  389. # GH33819
  390. # Test _has_externally_shared_axis() works for x-axis
  391. func = plotting._matplotlib.tools._has_externally_shared_axis
  392. fig = self.plt.figure()
  393. plots = fig.subplots(2, 4)
  394. # Create *externally* shared axes for first and third columns
  395. plots[0][0] = fig.add_subplot(231, sharex=plots[1][0])
  396. plots[0][2] = fig.add_subplot(233, sharex=plots[1][2])
  397. # Create *internally* shared axes for second and third columns
  398. plots[0][1].twinx()
  399. plots[0][2].twinx()
  400. # First column is only externally shared
  401. # Second column is only internally shared
  402. # Third column is both
  403. # Fourth column is neither
  404. assert func(plots[0][0], "x")
  405. assert not func(plots[0][1], "x")
  406. assert func(plots[0][2], "x")
  407. assert not func(plots[0][3], "x")
  408. def test_has_externally_shared_axis_y_axis(self):
  409. # GH33819
  410. # Test _has_externally_shared_axis() works for y-axis
  411. func = plotting._matplotlib.tools._has_externally_shared_axis
  412. fig = self.plt.figure()
  413. plots = fig.subplots(4, 2)
  414. # Create *externally* shared axes for first and third rows
  415. plots[0][0] = fig.add_subplot(321, sharey=plots[0][1])
  416. plots[2][0] = fig.add_subplot(325, sharey=plots[2][1])
  417. # Create *internally* shared axes for second and third rows
  418. plots[1][0].twiny()
  419. plots[2][0].twiny()
  420. # First row is only externally shared
  421. # Second row is only internally shared
  422. # Third row is both
  423. # Fourth row is neither
  424. assert func(plots[0][0], "y")
  425. assert not func(plots[1][0], "y")
  426. assert func(plots[2][0], "y")
  427. assert not func(plots[3][0], "y")
  428. def test_has_externally_shared_axis_invalid_compare_axis(self):
  429. # GH33819
  430. # Test _has_externally_shared_axis() raises an exception when
  431. # passed an invalid value as compare_axis parameter
  432. func = plotting._matplotlib.tools._has_externally_shared_axis
  433. fig = self.plt.figure()
  434. plots = fig.subplots(4, 2)
  435. # Create arbitrary axes
  436. plots[0][0] = fig.add_subplot(321, sharey=plots[0][1])
  437. # Check that an invalid compare_axis value triggers the expected exception
  438. msg = "needs 'x' or 'y' as a second parameter"
  439. with pytest.raises(ValueError, match=msg):
  440. func(plots[0][0], "z")
  441. def test_externally_shared_axes(self):
  442. # Example from GH33819
  443. # Create data
  444. df = DataFrame({"a": np.random.randn(1000), "b": np.random.randn(1000)})
  445. # Create figure
  446. fig = self.plt.figure()
  447. plots = fig.subplots(2, 3)
  448. # Create *externally* shared axes
  449. plots[0][0] = fig.add_subplot(231, sharex=plots[1][0])
  450. # note: no plots[0][1] that's the twin only case
  451. plots[0][2] = fig.add_subplot(233, sharex=plots[1][2])
  452. # Create *internally* shared axes
  453. # note: no plots[0][0] that's the external only case
  454. twin_ax1 = plots[0][1].twinx()
  455. twin_ax2 = plots[0][2].twinx()
  456. # Plot data to primary axes
  457. df["a"].plot(ax=plots[0][0], title="External share only").set_xlabel(
  458. "this label should never be visible"
  459. )
  460. df["a"].plot(ax=plots[1][0])
  461. df["a"].plot(ax=plots[0][1], title="Internal share (twin) only").set_xlabel(
  462. "this label should always be visible"
  463. )
  464. df["a"].plot(ax=plots[1][1])
  465. df["a"].plot(ax=plots[0][2], title="Both").set_xlabel(
  466. "this label should never be visible"
  467. )
  468. df["a"].plot(ax=plots[1][2])
  469. # Plot data to twinned axes
  470. df["b"].plot(ax=twin_ax1, color="green")
  471. df["b"].plot(ax=twin_ax2, color="yellow")
  472. assert not plots[0][0].xaxis.get_label().get_visible()
  473. assert plots[0][1].xaxis.get_label().get_visible()
  474. assert not plots[0][2].xaxis.get_label().get_visible()
  475. def test_plot_bar_axis_units_timestamp_conversion(self):
  476. # GH 38736
  477. # Ensure string x-axis from the second plot will not be converted to datetime
  478. # due to axis data from first plot
  479. df = DataFrame(
  480. [1.0],
  481. index=[Timestamp("2022-02-22 22:22:22")],
  482. )
  483. _check_plot_works(df.plot)
  484. s = Series({"A": 1.0})
  485. _check_plot_works(s.plot.bar)
  486. def test_bar_plt_xaxis_intervalrange(self):
  487. # GH 38969
  488. # Ensure IntervalIndex x-axis produces a bar plot as expected
  489. from matplotlib.text import Text
  490. expected = [Text(0, 0, "([0, 1],)"), Text(1, 0, "([1, 2],)")]
  491. s = Series(
  492. [1, 2],
  493. index=[interval_range(0, 2, closed="both")],
  494. )
  495. _check_plot_works(s.plot.bar)
  496. assert all(
  497. (a.get_text() == b.get_text())
  498. for a, b in zip(s.plot.bar().get_xticklabels(), expected)
  499. )