test_frame.py 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223
  1. """ Test cases for DataFrame.plot """
  2. from datetime import (
  3. date,
  4. datetime,
  5. )
  6. import gc
  7. import itertools
  8. import re
  9. import string
  10. import warnings
  11. import weakref
  12. import numpy as np
  13. import pytest
  14. import pandas.util._test_decorators as td
  15. from pandas.core.dtypes.api import is_list_like
  16. import pandas as pd
  17. from pandas import (
  18. DataFrame,
  19. MultiIndex,
  20. PeriodIndex,
  21. Series,
  22. bdate_range,
  23. date_range,
  24. plotting,
  25. )
  26. import pandas._testing as tm
  27. from pandas.tests.plotting.common import (
  28. TestPlotBase,
  29. _check_plot_works,
  30. )
  31. from pandas.io.formats.printing import pprint_thing
  32. @td.skip_if_no_mpl
  33. class TestDataFramePlots(TestPlotBase):
  34. @pytest.mark.xfail(reason="Api changed in 3.6.0")
  35. @pytest.mark.slow
  36. def test_plot(self):
  37. df = tm.makeTimeDataFrame()
  38. _check_plot_works(df.plot, grid=False)
  39. # _check_plot_works adds an ax so use default_axes=True to avoid warning
  40. axes = _check_plot_works(df.plot, default_axes=True, subplots=True)
  41. self._check_axes_shape(axes, axes_num=4, layout=(4, 1))
  42. axes = _check_plot_works(
  43. df.plot,
  44. default_axes=True,
  45. subplots=True,
  46. layout=(-1, 2),
  47. )
  48. self._check_axes_shape(axes, axes_num=4, layout=(2, 2))
  49. axes = _check_plot_works(
  50. df.plot,
  51. default_axes=True,
  52. subplots=True,
  53. use_index=False,
  54. )
  55. self._check_ticks_props(axes, xrot=0)
  56. self._check_axes_shape(axes, axes_num=4, layout=(4, 1))
  57. df = DataFrame({"x": [1, 2], "y": [3, 4]})
  58. msg = "'Line2D' object has no property 'blarg'"
  59. with pytest.raises(AttributeError, match=msg):
  60. df.plot.line(blarg=True)
  61. df = DataFrame(np.random.rand(10, 3), index=list(string.ascii_letters[:10]))
  62. ax = _check_plot_works(df.plot, use_index=True)
  63. self._check_ticks_props(ax, xrot=0)
  64. _check_plot_works(df.plot, yticks=[1, 5, 10])
  65. _check_plot_works(df.plot, xticks=[1, 5, 10])
  66. _check_plot_works(df.plot, ylim=(-100, 100), xlim=(-100, 100))
  67. _check_plot_works(df.plot, default_axes=True, subplots=True, title="blah")
  68. # We have to redo it here because _check_plot_works does two plots,
  69. # once without an ax kwarg and once with an ax kwarg and the new sharex
  70. # behaviour does not remove the visibility of the latter axis (as ax is
  71. # present). see: https://github.com/pandas-dev/pandas/issues/9737
  72. axes = df.plot(subplots=True, title="blah")
  73. self._check_axes_shape(axes, axes_num=3, layout=(3, 1))
  74. # axes[0].figure.savefig("test.png")
  75. for ax in axes[:2]:
  76. self._check_visible(ax.xaxis) # xaxis must be visible for grid
  77. self._check_visible(ax.get_xticklabels(), visible=False)
  78. self._check_visible(ax.get_xticklabels(minor=True), visible=False)
  79. self._check_visible([ax.xaxis.get_label()], visible=False)
  80. for ax in [axes[2]]:
  81. self._check_visible(ax.xaxis)
  82. self._check_visible(ax.get_xticklabels())
  83. self._check_visible([ax.xaxis.get_label()])
  84. self._check_ticks_props(ax, xrot=0)
  85. _check_plot_works(df.plot, title="blah")
  86. tuples = zip(string.ascii_letters[:10], range(10))
  87. df = DataFrame(np.random.rand(10, 3), index=MultiIndex.from_tuples(tuples))
  88. ax = _check_plot_works(df.plot, use_index=True)
  89. self._check_ticks_props(ax, xrot=0)
  90. # unicode
  91. index = MultiIndex.from_tuples(
  92. [
  93. ("\u03b1", 0),
  94. ("\u03b1", 1),
  95. ("\u03b2", 2),
  96. ("\u03b2", 3),
  97. ("\u03b3", 4),
  98. ("\u03b3", 5),
  99. ("\u03b4", 6),
  100. ("\u03b4", 7),
  101. ],
  102. names=["i0", "i1"],
  103. )
  104. columns = MultiIndex.from_tuples(
  105. [("bar", "\u0394"), ("bar", "\u0395")], names=["c0", "c1"]
  106. )
  107. df = DataFrame(np.random.randint(0, 10, (8, 2)), columns=columns, index=index)
  108. _check_plot_works(df.plot, title="\u03A3")
  109. # GH 6951
  110. # Test with single column
  111. df = DataFrame({"x": np.random.rand(10)})
  112. axes = _check_plot_works(df.plot.bar, subplots=True)
  113. self._check_axes_shape(axes, axes_num=1, layout=(1, 1))
  114. axes = _check_plot_works(df.plot.bar, subplots=True, layout=(-1, 1))
  115. self._check_axes_shape(axes, axes_num=1, layout=(1, 1))
  116. # When ax is supplied and required number of axes is 1,
  117. # passed ax should be used:
  118. fig, ax = self.plt.subplots()
  119. axes = df.plot.bar(subplots=True, ax=ax)
  120. assert len(axes) == 1
  121. result = ax.axes
  122. assert result is axes[0]
  123. def test_nullable_int_plot(self):
  124. # GH 32073
  125. dates = ["2008", "2009", None, "2011", "2012"]
  126. df = DataFrame(
  127. {
  128. "A": [1, 2, 3, 4, 5],
  129. "B": [1, 2, 3, 4, 5],
  130. "C": np.array([7, 5, np.nan, 3, 2], dtype=object),
  131. "D": pd.to_datetime(dates, format="%Y").view("i8"),
  132. "E": pd.to_datetime(dates, format="%Y", utc=True).view("i8"),
  133. }
  134. )
  135. _check_plot_works(df.plot, x="A", y="B")
  136. _check_plot_works(df[["A", "B"]].plot, x="A", y="B")
  137. _check_plot_works(df[["C", "A"]].plot, x="C", y="A") # nullable value on x-axis
  138. _check_plot_works(df[["A", "C"]].plot, x="A", y="C")
  139. _check_plot_works(df[["B", "C"]].plot, x="B", y="C")
  140. _check_plot_works(df[["A", "D"]].plot, x="A", y="D")
  141. _check_plot_works(df[["A", "E"]].plot, x="A", y="E")
  142. @pytest.mark.slow
  143. def test_integer_array_plot(self):
  144. # GH 25587
  145. arr = pd.array([1, 2, 3, 4], dtype="UInt32")
  146. s = Series(arr)
  147. _check_plot_works(s.plot.line)
  148. _check_plot_works(s.plot.bar)
  149. _check_plot_works(s.plot.hist)
  150. _check_plot_works(s.plot.pie)
  151. df = DataFrame({"x": arr, "y": arr})
  152. _check_plot_works(df.plot.line)
  153. _check_plot_works(df.plot.bar)
  154. _check_plot_works(df.plot.hist)
  155. _check_plot_works(df.plot.pie, y="y")
  156. _check_plot_works(df.plot.scatter, x="x", y="y")
  157. _check_plot_works(df.plot.hexbin, x="x", y="y")
  158. def test_nonnumeric_exclude(self):
  159. df = DataFrame({"A": ["x", "y", "z"], "B": [1, 2, 3]})
  160. ax = df.plot()
  161. assert len(ax.get_lines()) == 1 # B was plotted
  162. def test_implicit_label(self):
  163. df = DataFrame(np.random.randn(10, 3), columns=["a", "b", "c"])
  164. ax = df.plot(x="a", y="b")
  165. self._check_text_labels(ax.xaxis.get_label(), "a")
  166. def test_donot_overwrite_index_name(self):
  167. # GH 8494
  168. df = DataFrame(np.random.randn(2, 2), columns=["a", "b"])
  169. df.index.name = "NAME"
  170. df.plot(y="b", label="LABEL")
  171. assert df.index.name == "NAME"
  172. def test_plot_xy(self):
  173. # columns.inferred_type == 'string'
  174. df = tm.makeTimeDataFrame()
  175. self._check_data(df.plot(x=0, y=1), df.set_index("A")["B"].plot())
  176. self._check_data(df.plot(x=0), df.set_index("A").plot())
  177. self._check_data(df.plot(y=0), df.B.plot())
  178. self._check_data(df.plot(x="A", y="B"), df.set_index("A").B.plot())
  179. self._check_data(df.plot(x="A"), df.set_index("A").plot())
  180. self._check_data(df.plot(y="B"), df.B.plot())
  181. # columns.inferred_type == 'integer'
  182. df.columns = np.arange(1, len(df.columns) + 1)
  183. self._check_data(df.plot(x=1, y=2), df.set_index(1)[2].plot())
  184. self._check_data(df.plot(x=1), df.set_index(1).plot())
  185. self._check_data(df.plot(y=1), df[1].plot())
  186. # figsize and title
  187. ax = df.plot(x=1, y=2, title="Test", figsize=(16, 8))
  188. self._check_text_labels(ax.title, "Test")
  189. self._check_axes_shape(ax, axes_num=1, layout=(1, 1), figsize=(16.0, 8.0))
  190. # columns.inferred_type == 'mixed'
  191. # TODO add MultiIndex test
  192. @pytest.mark.parametrize(
  193. "input_log, expected_log", [(True, "log"), ("sym", "symlog")]
  194. )
  195. def test_logscales(self, input_log, expected_log):
  196. df = DataFrame({"a": np.arange(100)}, index=np.arange(100))
  197. ax = df.plot(logy=input_log)
  198. self._check_ax_scales(ax, yaxis=expected_log)
  199. assert ax.get_yscale() == expected_log
  200. ax = df.plot(logx=input_log)
  201. self._check_ax_scales(ax, xaxis=expected_log)
  202. assert ax.get_xscale() == expected_log
  203. ax = df.plot(loglog=input_log)
  204. self._check_ax_scales(ax, xaxis=expected_log, yaxis=expected_log)
  205. assert ax.get_xscale() == expected_log
  206. assert ax.get_yscale() == expected_log
  207. @pytest.mark.parametrize("input_param", ["logx", "logy", "loglog"])
  208. def test_invalid_logscale(self, input_param):
  209. # GH: 24867
  210. df = DataFrame({"a": np.arange(100)}, index=np.arange(100))
  211. msg = "Boolean, None and 'sym' are valid options, 'sm' is given."
  212. with pytest.raises(ValueError, match=msg):
  213. df.plot(**{input_param: "sm"})
  214. def test_xcompat(self):
  215. df = tm.makeTimeDataFrame()
  216. ax = df.plot(x_compat=True)
  217. lines = ax.get_lines()
  218. assert not isinstance(lines[0].get_xdata(), PeriodIndex)
  219. self._check_ticks_props(ax, xrot=30)
  220. tm.close()
  221. plotting.plot_params["xaxis.compat"] = True
  222. ax = df.plot()
  223. lines = ax.get_lines()
  224. assert not isinstance(lines[0].get_xdata(), PeriodIndex)
  225. self._check_ticks_props(ax, xrot=30)
  226. tm.close()
  227. plotting.plot_params["x_compat"] = False
  228. ax = df.plot()
  229. lines = ax.get_lines()
  230. assert not isinstance(lines[0].get_xdata(), PeriodIndex)
  231. assert isinstance(PeriodIndex(lines[0].get_xdata()), PeriodIndex)
  232. tm.close()
  233. # useful if you're plotting a bunch together
  234. with plotting.plot_params.use("x_compat", True):
  235. ax = df.plot()
  236. lines = ax.get_lines()
  237. assert not isinstance(lines[0].get_xdata(), PeriodIndex)
  238. self._check_ticks_props(ax, xrot=30)
  239. tm.close()
  240. ax = df.plot()
  241. lines = ax.get_lines()
  242. assert not isinstance(lines[0].get_xdata(), PeriodIndex)
  243. assert isinstance(PeriodIndex(lines[0].get_xdata()), PeriodIndex)
  244. self._check_ticks_props(ax, xrot=0)
  245. def test_period_compat(self):
  246. # GH 9012
  247. # period-array conversions
  248. df = DataFrame(
  249. np.random.rand(21, 2),
  250. index=bdate_range(datetime(2000, 1, 1), datetime(2000, 1, 31)),
  251. columns=["a", "b"],
  252. )
  253. df.plot()
  254. self.plt.axhline(y=0)
  255. tm.close()
  256. def test_unsorted_index(self):
  257. df = DataFrame(
  258. {"y": np.arange(100)}, index=np.arange(99, -1, -1), dtype=np.int64
  259. )
  260. ax = df.plot()
  261. lines = ax.get_lines()[0]
  262. rs = lines.get_xydata()
  263. rs = Series(rs[:, 1], rs[:, 0], dtype=np.int64, name="y")
  264. tm.assert_series_equal(rs, df.y, check_index_type=False)
  265. tm.close()
  266. df.index = pd.Index(np.arange(99, -1, -1), dtype=np.float64)
  267. ax = df.plot()
  268. lines = ax.get_lines()[0]
  269. rs = lines.get_xydata()
  270. rs = Series(rs[:, 1], rs[:, 0], dtype=np.int64, name="y")
  271. tm.assert_series_equal(rs, df.y)
  272. def test_unsorted_index_lims(self):
  273. df = DataFrame({"y": [0.0, 1.0, 2.0, 3.0]}, index=[1.0, 0.0, 3.0, 2.0])
  274. ax = df.plot()
  275. xmin, xmax = ax.get_xlim()
  276. lines = ax.get_lines()
  277. assert xmin <= np.nanmin(lines[0].get_data()[0])
  278. assert xmax >= np.nanmax(lines[0].get_data()[0])
  279. df = DataFrame(
  280. {"y": [0.0, 1.0, np.nan, 3.0, 4.0, 5.0, 6.0]},
  281. index=[1.0, 0.0, 3.0, 2.0, np.nan, 3.0, 2.0],
  282. )
  283. ax = df.plot()
  284. xmin, xmax = ax.get_xlim()
  285. lines = ax.get_lines()
  286. assert xmin <= np.nanmin(lines[0].get_data()[0])
  287. assert xmax >= np.nanmax(lines[0].get_data()[0])
  288. df = DataFrame({"y": [0.0, 1.0, 2.0, 3.0], "z": [91.0, 90.0, 93.0, 92.0]})
  289. ax = df.plot(x="z", y="y")
  290. xmin, xmax = ax.get_xlim()
  291. lines = ax.get_lines()
  292. assert xmin <= np.nanmin(lines[0].get_data()[0])
  293. assert xmax >= np.nanmax(lines[0].get_data()[0])
  294. def test_negative_log(self):
  295. df = -DataFrame(
  296. np.random.rand(6, 4),
  297. index=list(string.ascii_letters[:6]),
  298. columns=["x", "y", "z", "four"],
  299. )
  300. msg = "Log-y scales are not supported in area plot"
  301. with pytest.raises(ValueError, match=msg):
  302. df.plot.area(logy=True)
  303. with pytest.raises(ValueError, match=msg):
  304. df.plot.area(loglog=True)
  305. def _compare_stacked_y_cood(self, normal_lines, stacked_lines):
  306. base = np.zeros(len(normal_lines[0].get_data()[1]))
  307. for nl, sl in zip(normal_lines, stacked_lines):
  308. base += nl.get_data()[1] # get y coordinates
  309. sy = sl.get_data()[1]
  310. tm.assert_numpy_array_equal(base, sy)
  311. @pytest.mark.parametrize("kind", ["line", "area"])
  312. def test_line_area_stacked(self, kind):
  313. np_random = np.random.RandomState(42)
  314. df = DataFrame(np_random.rand(6, 4), columns=["w", "x", "y", "z"])
  315. neg_df = -df
  316. # each column has either positive or negative value
  317. sep_df = DataFrame(
  318. {
  319. "w": np_random.rand(6),
  320. "x": np_random.rand(6),
  321. "y": -np_random.rand(6),
  322. "z": -np_random.rand(6),
  323. }
  324. )
  325. # each column has positive-negative mixed value
  326. mixed_df = DataFrame(
  327. np_random.randn(6, 4),
  328. index=list(string.ascii_letters[:6]),
  329. columns=["w", "x", "y", "z"],
  330. )
  331. ax1 = _check_plot_works(df.plot, kind=kind, stacked=False)
  332. ax2 = _check_plot_works(df.plot, kind=kind, stacked=True)
  333. self._compare_stacked_y_cood(ax1.lines, ax2.lines)
  334. ax1 = _check_plot_works(neg_df.plot, kind=kind, stacked=False)
  335. ax2 = _check_plot_works(neg_df.plot, kind=kind, stacked=True)
  336. self._compare_stacked_y_cood(ax1.lines, ax2.lines)
  337. ax1 = _check_plot_works(sep_df.plot, kind=kind, stacked=False)
  338. ax2 = _check_plot_works(sep_df.plot, kind=kind, stacked=True)
  339. self._compare_stacked_y_cood(ax1.lines[:2], ax2.lines[:2])
  340. self._compare_stacked_y_cood(ax1.lines[2:], ax2.lines[2:])
  341. _check_plot_works(mixed_df.plot, stacked=False)
  342. msg = (
  343. "When stacked is True, each column must be either all positive or "
  344. "all negative. Column 'w' contains both positive and negative "
  345. "values"
  346. )
  347. with pytest.raises(ValueError, match=msg):
  348. mixed_df.plot(stacked=True)
  349. # Use an index with strictly positive values, preventing
  350. # matplotlib from warning about ignoring xlim
  351. df2 = df.set_index(df.index + 1)
  352. _check_plot_works(df2.plot, kind=kind, logx=True, stacked=True)
  353. def test_line_area_nan_df(self):
  354. values1 = [1, 2, np.nan, 3]
  355. values2 = [3, np.nan, 2, 1]
  356. df = DataFrame({"a": values1, "b": values2})
  357. tdf = DataFrame({"a": values1, "b": values2}, index=tm.makeDateIndex(k=4))
  358. for d in [df, tdf]:
  359. ax = _check_plot_works(d.plot)
  360. masked1 = ax.lines[0].get_ydata()
  361. masked2 = ax.lines[1].get_ydata()
  362. # remove nan for comparison purpose
  363. exp = np.array([1, 2, 3], dtype=np.float64)
  364. tm.assert_numpy_array_equal(np.delete(masked1.data, 2), exp)
  365. exp = np.array([3, 2, 1], dtype=np.float64)
  366. tm.assert_numpy_array_equal(np.delete(masked2.data, 1), exp)
  367. tm.assert_numpy_array_equal(
  368. masked1.mask, np.array([False, False, True, False])
  369. )
  370. tm.assert_numpy_array_equal(
  371. masked2.mask, np.array([False, True, False, False])
  372. )
  373. expected1 = np.array([1, 2, 0, 3], dtype=np.float64)
  374. expected2 = np.array([3, 0, 2, 1], dtype=np.float64)
  375. ax = _check_plot_works(d.plot, stacked=True)
  376. tm.assert_numpy_array_equal(ax.lines[0].get_ydata(), expected1)
  377. tm.assert_numpy_array_equal(ax.lines[1].get_ydata(), expected1 + expected2)
  378. ax = _check_plot_works(d.plot.area)
  379. tm.assert_numpy_array_equal(ax.lines[0].get_ydata(), expected1)
  380. tm.assert_numpy_array_equal(ax.lines[1].get_ydata(), expected1 + expected2)
  381. ax = _check_plot_works(d.plot.area, stacked=False)
  382. tm.assert_numpy_array_equal(ax.lines[0].get_ydata(), expected1)
  383. tm.assert_numpy_array_equal(ax.lines[1].get_ydata(), expected2)
  384. def test_line_lim(self):
  385. df = DataFrame(np.random.rand(6, 3), columns=["x", "y", "z"])
  386. ax = df.plot()
  387. xmin, xmax = ax.get_xlim()
  388. lines = ax.get_lines()
  389. assert xmin <= lines[0].get_data()[0][0]
  390. assert xmax >= lines[0].get_data()[0][-1]
  391. ax = df.plot(secondary_y=True)
  392. xmin, xmax = ax.get_xlim()
  393. lines = ax.get_lines()
  394. assert xmin <= lines[0].get_data()[0][0]
  395. assert xmax >= lines[0].get_data()[0][-1]
  396. axes = df.plot(secondary_y=True, subplots=True)
  397. self._check_axes_shape(axes, axes_num=3, layout=(3, 1))
  398. for ax in axes:
  399. assert hasattr(ax, "left_ax")
  400. assert not hasattr(ax, "right_ax")
  401. xmin, xmax = ax.get_xlim()
  402. lines = ax.get_lines()
  403. assert xmin <= lines[0].get_data()[0][0]
  404. assert xmax >= lines[0].get_data()[0][-1]
  405. @pytest.mark.xfail(
  406. strict=False,
  407. reason="2020-12-01 this has been failing periodically on the "
  408. "ymin==0 assertion for a week or so.",
  409. )
  410. @pytest.mark.parametrize("stacked", [True, False])
  411. def test_area_lim(self, stacked):
  412. df = DataFrame(np.random.rand(6, 4), columns=["x", "y", "z", "four"])
  413. neg_df = -df
  414. ax = _check_plot_works(df.plot.area, stacked=stacked)
  415. xmin, xmax = ax.get_xlim()
  416. ymin, ymax = ax.get_ylim()
  417. lines = ax.get_lines()
  418. assert xmin <= lines[0].get_data()[0][0]
  419. assert xmax >= lines[0].get_data()[0][-1]
  420. assert ymin == 0
  421. ax = _check_plot_works(neg_df.plot.area, stacked=stacked)
  422. ymin, ymax = ax.get_ylim()
  423. assert ymax == 0
  424. def test_area_sharey_dont_overwrite(self):
  425. # GH37942
  426. df = DataFrame(np.random.rand(4, 2), columns=["x", "y"])
  427. fig, (ax1, ax2) = self.plt.subplots(1, 2, sharey=True)
  428. df.plot(ax=ax1, kind="area")
  429. df.plot(ax=ax2, kind="area")
  430. assert self.get_y_axis(ax1).joined(ax1, ax2)
  431. assert self.get_y_axis(ax2).joined(ax1, ax2)
  432. def test_bar_linewidth(self):
  433. df = DataFrame(np.random.randn(5, 5))
  434. # regular
  435. ax = df.plot.bar(linewidth=2)
  436. for r in ax.patches:
  437. assert r.get_linewidth() == 2
  438. # stacked
  439. ax = df.plot.bar(stacked=True, linewidth=2)
  440. for r in ax.patches:
  441. assert r.get_linewidth() == 2
  442. # subplots
  443. axes = df.plot.bar(linewidth=2, subplots=True)
  444. self._check_axes_shape(axes, axes_num=5, layout=(5, 1))
  445. for ax in axes:
  446. for r in ax.patches:
  447. assert r.get_linewidth() == 2
  448. def test_bar_barwidth(self):
  449. df = DataFrame(np.random.randn(5, 5))
  450. width = 0.9
  451. # regular
  452. ax = df.plot.bar(width=width)
  453. for r in ax.patches:
  454. assert r.get_width() == width / len(df.columns)
  455. # stacked
  456. ax = df.plot.bar(stacked=True, width=width)
  457. for r in ax.patches:
  458. assert r.get_width() == width
  459. # horizontal regular
  460. ax = df.plot.barh(width=width)
  461. for r in ax.patches:
  462. assert r.get_height() == width / len(df.columns)
  463. # horizontal stacked
  464. ax = df.plot.barh(stacked=True, width=width)
  465. for r in ax.patches:
  466. assert r.get_height() == width
  467. # subplots
  468. axes = df.plot.bar(width=width, subplots=True)
  469. for ax in axes:
  470. for r in ax.patches:
  471. assert r.get_width() == width
  472. # horizontal subplots
  473. axes = df.plot.barh(width=width, subplots=True)
  474. for ax in axes:
  475. for r in ax.patches:
  476. assert r.get_height() == width
  477. def test_bar_bottom_left(self):
  478. df = DataFrame(np.random.rand(5, 5))
  479. ax = df.plot.bar(stacked=False, bottom=1)
  480. result = [p.get_y() for p in ax.patches]
  481. assert result == [1] * 25
  482. ax = df.plot.bar(stacked=True, bottom=[-1, -2, -3, -4, -5])
  483. result = [p.get_y() for p in ax.patches[:5]]
  484. assert result == [-1, -2, -3, -4, -5]
  485. ax = df.plot.barh(stacked=False, left=np.array([1, 1, 1, 1, 1]))
  486. result = [p.get_x() for p in ax.patches]
  487. assert result == [1] * 25
  488. ax = df.plot.barh(stacked=True, left=[1, 2, 3, 4, 5])
  489. result = [p.get_x() for p in ax.patches[:5]]
  490. assert result == [1, 2, 3, 4, 5]
  491. axes = df.plot.bar(subplots=True, bottom=-1)
  492. for ax in axes:
  493. result = [p.get_y() for p in ax.patches]
  494. assert result == [-1] * 5
  495. axes = df.plot.barh(subplots=True, left=np.array([1, 1, 1, 1, 1]))
  496. for ax in axes:
  497. result = [p.get_x() for p in ax.patches]
  498. assert result == [1] * 5
  499. def test_bar_nan(self):
  500. df = DataFrame({"A": [10, np.nan, 20], "B": [5, 10, 20], "C": [1, 2, 3]})
  501. ax = df.plot.bar()
  502. expected = [10, 0, 20, 5, 10, 20, 1, 2, 3]
  503. result = [p.get_height() for p in ax.patches]
  504. assert result == expected
  505. ax = df.plot.bar(stacked=True)
  506. result = [p.get_height() for p in ax.patches]
  507. assert result == expected
  508. result = [p.get_y() for p in ax.patches]
  509. expected = [0.0, 0.0, 0.0, 10.0, 0.0, 20.0, 15.0, 10.0, 40.0]
  510. assert result == expected
  511. def test_bar_categorical(self):
  512. # GH 13019
  513. df1 = DataFrame(
  514. np.random.randn(6, 5),
  515. index=pd.Index(list("ABCDEF")),
  516. columns=pd.Index(list("abcde")),
  517. )
  518. # categorical index must behave the same
  519. df2 = DataFrame(
  520. np.random.randn(6, 5),
  521. index=pd.CategoricalIndex(list("ABCDEF")),
  522. columns=pd.CategoricalIndex(list("abcde")),
  523. )
  524. for df in [df1, df2]:
  525. ax = df.plot.bar()
  526. ticks = ax.xaxis.get_ticklocs()
  527. tm.assert_numpy_array_equal(ticks, np.array([0, 1, 2, 3, 4, 5]))
  528. assert ax.get_xlim() == (-0.5, 5.5)
  529. # check left-edge of bars
  530. assert ax.patches[0].get_x() == -0.25
  531. assert ax.patches[-1].get_x() == 5.15
  532. ax = df.plot.bar(stacked=True)
  533. tm.assert_numpy_array_equal(ticks, np.array([0, 1, 2, 3, 4, 5]))
  534. assert ax.get_xlim() == (-0.5, 5.5)
  535. assert ax.patches[0].get_x() == -0.25
  536. assert ax.patches[-1].get_x() == 4.75
  537. def test_plot_scatter(self):
  538. df = DataFrame(
  539. np.random.randn(6, 4),
  540. index=list(string.ascii_letters[:6]),
  541. columns=["x", "y", "z", "four"],
  542. )
  543. _check_plot_works(df.plot.scatter, x="x", y="y")
  544. _check_plot_works(df.plot.scatter, x=1, y=2)
  545. msg = re.escape("scatter() missing 1 required positional argument: 'y'")
  546. with pytest.raises(TypeError, match=msg):
  547. df.plot.scatter(x="x")
  548. msg = re.escape("scatter() missing 1 required positional argument: 'x'")
  549. with pytest.raises(TypeError, match=msg):
  550. df.plot.scatter(y="y")
  551. # GH 6951
  552. axes = df.plot(x="x", y="y", kind="scatter", subplots=True)
  553. self._check_axes_shape(axes, axes_num=1, layout=(1, 1))
  554. def test_raise_error_on_datetime_time_data(self):
  555. # GH 8113, datetime.time type is not supported by matplotlib in scatter
  556. df = DataFrame(np.random.randn(10), columns=["a"])
  557. df["dtime"] = date_range(start="2014-01-01", freq="h", periods=10).time
  558. msg = "must be a string or a (real )?number, not 'datetime.time'"
  559. with pytest.raises(TypeError, match=msg):
  560. df.plot(kind="scatter", x="dtime", y="a")
  561. def test_scatterplot_datetime_data(self):
  562. # GH 30391
  563. dates = date_range(start=date(2019, 1, 1), periods=12, freq="W")
  564. vals = np.random.normal(0, 1, len(dates))
  565. df = DataFrame({"dates": dates, "vals": vals})
  566. _check_plot_works(df.plot.scatter, x="dates", y="vals")
  567. _check_plot_works(df.plot.scatter, x=0, y=1)
  568. def test_scatterplot_object_data(self):
  569. # GH 18755
  570. df = DataFrame({"a": ["A", "B", "C"], "b": [2, 3, 4]})
  571. _check_plot_works(df.plot.scatter, x="a", y="b")
  572. _check_plot_works(df.plot.scatter, x=0, y=1)
  573. df = DataFrame({"a": ["A", "B", "C"], "b": ["a", "b", "c"]})
  574. _check_plot_works(df.plot.scatter, x="a", y="b")
  575. _check_plot_works(df.plot.scatter, x=0, y=1)
  576. @pytest.mark.parametrize("ordered", [True, False])
  577. @pytest.mark.parametrize(
  578. "categories",
  579. (["setosa", "versicolor", "virginica"], ["versicolor", "virginica", "setosa"]),
  580. )
  581. def test_scatterplot_color_by_categorical(self, ordered, categories):
  582. df = DataFrame(
  583. [[5.1, 3.5], [4.9, 3.0], [7.0, 3.2], [6.4, 3.2], [5.9, 3.0]],
  584. columns=["length", "width"],
  585. )
  586. df["species"] = pd.Categorical(
  587. ["setosa", "setosa", "virginica", "virginica", "versicolor"],
  588. ordered=ordered,
  589. categories=categories,
  590. )
  591. ax = df.plot.scatter(x=0, y=1, c="species")
  592. (colorbar_collection,) = ax.collections
  593. colorbar = colorbar_collection.colorbar
  594. expected_ticks = np.array([0.5, 1.5, 2.5])
  595. result_ticks = colorbar.get_ticks()
  596. tm.assert_numpy_array_equal(result_ticks, expected_ticks)
  597. expected_boundaries = np.array([0.0, 1.0, 2.0, 3.0])
  598. result_boundaries = colorbar._boundaries
  599. tm.assert_numpy_array_equal(result_boundaries, expected_boundaries)
  600. expected_yticklabels = categories
  601. result_yticklabels = [i.get_text() for i in colorbar.ax.get_ymajorticklabels()]
  602. assert all(i == j for i, j in zip(result_yticklabels, expected_yticklabels))
  603. @pytest.mark.parametrize("x, y", [("x", "y"), ("y", "x"), ("y", "y")])
  604. def test_plot_scatter_with_categorical_data(self, x, y):
  605. # after fixing GH 18755, should be able to plot categorical data
  606. df = DataFrame({"x": [1, 2, 3, 4], "y": pd.Categorical(["a", "b", "a", "c"])})
  607. _check_plot_works(df.plot.scatter, x=x, y=y)
  608. def test_plot_scatter_with_c(self):
  609. df = DataFrame(
  610. np.random.randint(low=0, high=100, size=(6, 4)),
  611. index=list(string.ascii_letters[:6]),
  612. columns=["x", "y", "z", "four"],
  613. )
  614. axes = [df.plot.scatter(x="x", y="y", c="z"), df.plot.scatter(x=0, y=1, c=2)]
  615. for ax in axes:
  616. # default to Greys
  617. assert ax.collections[0].cmap.name == "Greys"
  618. assert ax.collections[0].colorbar.ax.get_ylabel() == "z"
  619. cm = "cubehelix"
  620. ax = df.plot.scatter(x="x", y="y", c="z", colormap=cm)
  621. assert ax.collections[0].cmap.name == cm
  622. # verify turning off colorbar works
  623. ax = df.plot.scatter(x="x", y="y", c="z", colorbar=False)
  624. assert ax.collections[0].colorbar is None
  625. # verify that we can still plot a solid color
  626. ax = df.plot.scatter(x=0, y=1, c="red")
  627. assert ax.collections[0].colorbar is None
  628. self._check_colors(ax.collections, facecolors=["r"])
  629. # Ensure that we can pass an np.array straight through to matplotlib,
  630. # this functionality was accidentally removed previously.
  631. # See https://github.com/pandas-dev/pandas/issues/8852 for bug report
  632. #
  633. # Exercise colormap path and non-colormap path as they are independent
  634. #
  635. df = DataFrame({"A": [1, 2], "B": [3, 4]})
  636. red_rgba = [1.0, 0.0, 0.0, 1.0]
  637. green_rgba = [0.0, 1.0, 0.0, 1.0]
  638. rgba_array = np.array([red_rgba, green_rgba])
  639. ax = df.plot.scatter(x="A", y="B", c=rgba_array)
  640. # expect the face colors of the points in the non-colormap path to be
  641. # identical to the values we supplied, normally we'd be on shaky ground
  642. # comparing floats for equality but here we expect them to be
  643. # identical.
  644. tm.assert_numpy_array_equal(ax.collections[0].get_facecolor(), rgba_array)
  645. # we don't test the colors of the faces in this next plot because they
  646. # are dependent on the spring colormap, which may change its colors
  647. # later.
  648. float_array = np.array([0.0, 1.0])
  649. df.plot.scatter(x="A", y="B", c=float_array, cmap="spring")
  650. def test_plot_scatter_with_s(self):
  651. # this refers to GH 32904
  652. df = DataFrame(np.random.random((10, 3)) * 100, columns=["a", "b", "c"])
  653. ax = df.plot.scatter(x="a", y="b", s="c")
  654. tm.assert_numpy_array_equal(df["c"].values, right=ax.collections[0].get_sizes())
  655. def test_plot_scatter_with_norm(self):
  656. # added while fixing GH 45809
  657. import matplotlib as mpl
  658. df = DataFrame(np.random.random((10, 3)) * 100, columns=["a", "b", "c"])
  659. norm = mpl.colors.LogNorm()
  660. ax = df.plot.scatter(x="a", y="b", c="c", norm=norm)
  661. assert ax.collections[0].norm is norm
  662. def test_plot_scatter_without_norm(self):
  663. # added while fixing GH 45809
  664. import matplotlib as mpl
  665. df = DataFrame(np.random.random((10, 3)) * 100, columns=["a", "b", "c"])
  666. ax = df.plot.scatter(x="a", y="b", c="c")
  667. plot_norm = ax.collections[0].norm
  668. color_min_max = (df.c.min(), df.c.max())
  669. default_norm = mpl.colors.Normalize(*color_min_max)
  670. for value in df.c:
  671. assert plot_norm(value) == default_norm(value)
  672. @pytest.mark.slow
  673. def test_plot_bar(self):
  674. df = DataFrame(
  675. np.random.randn(6, 4),
  676. index=list(string.ascii_letters[:6]),
  677. columns=["one", "two", "three", "four"],
  678. )
  679. _check_plot_works(df.plot.bar)
  680. _check_plot_works(df.plot.bar, legend=False)
  681. _check_plot_works(df.plot.bar, default_axes=True, subplots=True)
  682. _check_plot_works(df.plot.bar, stacked=True)
  683. df = DataFrame(
  684. np.random.randn(10, 15),
  685. index=list(string.ascii_letters[:10]),
  686. columns=range(15),
  687. )
  688. _check_plot_works(df.plot.bar)
  689. df = DataFrame({"a": [0, 1], "b": [1, 0]})
  690. ax = _check_plot_works(df.plot.bar)
  691. self._check_ticks_props(ax, xrot=90)
  692. ax = df.plot.bar(rot=35, fontsize=10)
  693. self._check_ticks_props(ax, xrot=35, xlabelsize=10, ylabelsize=10)
  694. ax = _check_plot_works(df.plot.barh)
  695. self._check_ticks_props(ax, yrot=0)
  696. ax = df.plot.barh(rot=55, fontsize=11)
  697. self._check_ticks_props(ax, yrot=55, ylabelsize=11, xlabelsize=11)
  698. def test_boxplot(self, hist_df):
  699. df = hist_df
  700. series = df["height"]
  701. numeric_cols = df._get_numeric_data().columns
  702. labels = [pprint_thing(c) for c in numeric_cols]
  703. ax = _check_plot_works(df.plot.box)
  704. self._check_text_labels(ax.get_xticklabels(), labels)
  705. tm.assert_numpy_array_equal(
  706. ax.xaxis.get_ticklocs(), np.arange(1, len(numeric_cols) + 1)
  707. )
  708. assert len(ax.lines) == 7 * len(numeric_cols)
  709. tm.close()
  710. axes = series.plot.box(rot=40)
  711. self._check_ticks_props(axes, xrot=40, yrot=0)
  712. tm.close()
  713. ax = _check_plot_works(series.plot.box)
  714. positions = np.array([1, 6, 7])
  715. ax = df.plot.box(positions=positions)
  716. numeric_cols = df._get_numeric_data().columns
  717. labels = [pprint_thing(c) for c in numeric_cols]
  718. self._check_text_labels(ax.get_xticklabels(), labels)
  719. tm.assert_numpy_array_equal(ax.xaxis.get_ticklocs(), positions)
  720. assert len(ax.lines) == 7 * len(numeric_cols)
  721. def test_boxplot_vertical(self, hist_df):
  722. df = hist_df
  723. numeric_cols = df._get_numeric_data().columns
  724. labels = [pprint_thing(c) for c in numeric_cols]
  725. # if horizontal, yticklabels are rotated
  726. ax = df.plot.box(rot=50, fontsize=8, vert=False)
  727. self._check_ticks_props(ax, xrot=0, yrot=50, ylabelsize=8)
  728. self._check_text_labels(ax.get_yticklabels(), labels)
  729. assert len(ax.lines) == 7 * len(numeric_cols)
  730. axes = _check_plot_works(
  731. df.plot.box,
  732. default_axes=True,
  733. subplots=True,
  734. vert=False,
  735. logx=True,
  736. )
  737. self._check_axes_shape(axes, axes_num=3, layout=(1, 3))
  738. self._check_ax_scales(axes, xaxis="log")
  739. for ax, label in zip(axes, labels):
  740. self._check_text_labels(ax.get_yticklabels(), [label])
  741. assert len(ax.lines) == 7
  742. positions = np.array([3, 2, 8])
  743. ax = df.plot.box(positions=positions, vert=False)
  744. self._check_text_labels(ax.get_yticklabels(), labels)
  745. tm.assert_numpy_array_equal(ax.yaxis.get_ticklocs(), positions)
  746. assert len(ax.lines) == 7 * len(numeric_cols)
  747. def test_boxplot_return_type(self):
  748. df = DataFrame(
  749. np.random.randn(6, 4),
  750. index=list(string.ascii_letters[:6]),
  751. columns=["one", "two", "three", "four"],
  752. )
  753. msg = "return_type must be {None, 'axes', 'dict', 'both'}"
  754. with pytest.raises(ValueError, match=msg):
  755. df.plot.box(return_type="not_a_type")
  756. result = df.plot.box(return_type="dict")
  757. self._check_box_return_type(result, "dict")
  758. result = df.plot.box(return_type="axes")
  759. self._check_box_return_type(result, "axes")
  760. result = df.plot.box() # default axes
  761. self._check_box_return_type(result, "axes")
  762. result = df.plot.box(return_type="both")
  763. self._check_box_return_type(result, "both")
  764. @td.skip_if_no_scipy
  765. def test_kde_df(self):
  766. df = DataFrame(np.random.randn(100, 4))
  767. ax = _check_plot_works(df.plot, kind="kde")
  768. expected = [pprint_thing(c) for c in df.columns]
  769. self._check_legend_labels(ax, labels=expected)
  770. self._check_ticks_props(ax, xrot=0)
  771. ax = df.plot(kind="kde", rot=20, fontsize=5)
  772. self._check_ticks_props(ax, xrot=20, xlabelsize=5, ylabelsize=5)
  773. axes = _check_plot_works(
  774. df.plot,
  775. default_axes=True,
  776. kind="kde",
  777. subplots=True,
  778. )
  779. self._check_axes_shape(axes, axes_num=4, layout=(4, 1))
  780. axes = df.plot(kind="kde", logy=True, subplots=True)
  781. self._check_ax_scales(axes, yaxis="log")
  782. @td.skip_if_no_scipy
  783. def test_kde_missing_vals(self):
  784. df = DataFrame(np.random.uniform(size=(100, 4)))
  785. df.loc[0, 0] = np.nan
  786. _check_plot_works(df.plot, kind="kde")
  787. def test_hist_df(self):
  788. from matplotlib.patches import Rectangle
  789. df = DataFrame(np.random.randn(100, 4))
  790. series = df[0]
  791. ax = _check_plot_works(df.plot.hist)
  792. expected = [pprint_thing(c) for c in df.columns]
  793. self._check_legend_labels(ax, labels=expected)
  794. axes = _check_plot_works(
  795. df.plot.hist,
  796. default_axes=True,
  797. subplots=True,
  798. logy=True,
  799. )
  800. self._check_axes_shape(axes, axes_num=4, layout=(4, 1))
  801. self._check_ax_scales(axes, yaxis="log")
  802. axes = series.plot.hist(rot=40)
  803. self._check_ticks_props(axes, xrot=40, yrot=0)
  804. tm.close()
  805. ax = series.plot.hist(cumulative=True, bins=4, density=True)
  806. # height of last bin (index 5) must be 1.0
  807. rects = [x for x in ax.get_children() if isinstance(x, Rectangle)]
  808. tm.assert_almost_equal(rects[-1].get_height(), 1.0)
  809. tm.close()
  810. ax = series.plot.hist(cumulative=True, bins=4)
  811. rects = [x for x in ax.get_children() if isinstance(x, Rectangle)]
  812. tm.assert_almost_equal(rects[-2].get_height(), 100.0)
  813. tm.close()
  814. # if horizontal, yticklabels are rotated
  815. axes = df.plot.hist(rot=50, fontsize=8, orientation="horizontal")
  816. self._check_ticks_props(axes, xrot=0, yrot=50, ylabelsize=8)
  817. @pytest.mark.parametrize(
  818. "weights", [0.1 * np.ones(shape=(100,)), 0.1 * np.ones(shape=(100, 2))]
  819. )
  820. def test_hist_weights(self, weights):
  821. # GH 33173
  822. np.random.seed(0)
  823. df = DataFrame(dict(zip(["A", "B"], np.random.randn(2, 100))))
  824. ax1 = _check_plot_works(df.plot, kind="hist", weights=weights)
  825. ax2 = _check_plot_works(df.plot, kind="hist")
  826. patch_height_with_weights = [patch.get_height() for patch in ax1.patches]
  827. # original heights with no weights, and we manually multiply with example
  828. # weights, so after multiplication, they should be almost same
  829. expected_patch_height = [0.1 * patch.get_height() for patch in ax2.patches]
  830. tm.assert_almost_equal(patch_height_with_weights, expected_patch_height)
  831. def _check_box_coord(
  832. self,
  833. patches,
  834. expected_y=None,
  835. expected_h=None,
  836. expected_x=None,
  837. expected_w=None,
  838. ):
  839. result_y = np.array([p.get_y() for p in patches])
  840. result_height = np.array([p.get_height() for p in patches])
  841. result_x = np.array([p.get_x() for p in patches])
  842. result_width = np.array([p.get_width() for p in patches])
  843. # dtype is depending on above values, no need to check
  844. if expected_y is not None:
  845. tm.assert_numpy_array_equal(result_y, expected_y, check_dtype=False)
  846. if expected_h is not None:
  847. tm.assert_numpy_array_equal(result_height, expected_h, check_dtype=False)
  848. if expected_x is not None:
  849. tm.assert_numpy_array_equal(result_x, expected_x, check_dtype=False)
  850. if expected_w is not None:
  851. tm.assert_numpy_array_equal(result_width, expected_w, check_dtype=False)
  852. def test_hist_df_coord(self):
  853. normal_df = DataFrame(
  854. {
  855. "A": np.repeat(np.array([1, 2, 3, 4, 5]), np.array([10, 9, 8, 7, 6])),
  856. "B": np.repeat(np.array([1, 2, 3, 4, 5]), np.array([8, 8, 8, 8, 8])),
  857. "C": np.repeat(np.array([1, 2, 3, 4, 5]), np.array([6, 7, 8, 9, 10])),
  858. },
  859. columns=["A", "B", "C"],
  860. )
  861. nan_df = DataFrame(
  862. {
  863. "A": np.repeat(
  864. np.array([np.nan, 1, 2, 3, 4, 5]), np.array([3, 10, 9, 8, 7, 6])
  865. ),
  866. "B": np.repeat(
  867. np.array([1, np.nan, 2, 3, 4, 5]), np.array([8, 3, 8, 8, 8, 8])
  868. ),
  869. "C": np.repeat(
  870. np.array([1, 2, 3, np.nan, 4, 5]), np.array([6, 7, 8, 3, 9, 10])
  871. ),
  872. },
  873. columns=["A", "B", "C"],
  874. )
  875. for df in [normal_df, nan_df]:
  876. ax = df.plot.hist(bins=5)
  877. self._check_box_coord(
  878. ax.patches[:5],
  879. expected_y=np.array([0, 0, 0, 0, 0]),
  880. expected_h=np.array([10, 9, 8, 7, 6]),
  881. )
  882. self._check_box_coord(
  883. ax.patches[5:10],
  884. expected_y=np.array([0, 0, 0, 0, 0]),
  885. expected_h=np.array([8, 8, 8, 8, 8]),
  886. )
  887. self._check_box_coord(
  888. ax.patches[10:],
  889. expected_y=np.array([0, 0, 0, 0, 0]),
  890. expected_h=np.array([6, 7, 8, 9, 10]),
  891. )
  892. ax = df.plot.hist(bins=5, stacked=True)
  893. self._check_box_coord(
  894. ax.patches[:5],
  895. expected_y=np.array([0, 0, 0, 0, 0]),
  896. expected_h=np.array([10, 9, 8, 7, 6]),
  897. )
  898. self._check_box_coord(
  899. ax.patches[5:10],
  900. expected_y=np.array([10, 9, 8, 7, 6]),
  901. expected_h=np.array([8, 8, 8, 8, 8]),
  902. )
  903. self._check_box_coord(
  904. ax.patches[10:],
  905. expected_y=np.array([18, 17, 16, 15, 14]),
  906. expected_h=np.array([6, 7, 8, 9, 10]),
  907. )
  908. axes = df.plot.hist(bins=5, stacked=True, subplots=True)
  909. self._check_box_coord(
  910. axes[0].patches,
  911. expected_y=np.array([0, 0, 0, 0, 0]),
  912. expected_h=np.array([10, 9, 8, 7, 6]),
  913. )
  914. self._check_box_coord(
  915. axes[1].patches,
  916. expected_y=np.array([0, 0, 0, 0, 0]),
  917. expected_h=np.array([8, 8, 8, 8, 8]),
  918. )
  919. self._check_box_coord(
  920. axes[2].patches,
  921. expected_y=np.array([0, 0, 0, 0, 0]),
  922. expected_h=np.array([6, 7, 8, 9, 10]),
  923. )
  924. # horizontal
  925. ax = df.plot.hist(bins=5, orientation="horizontal")
  926. self._check_box_coord(
  927. ax.patches[:5],
  928. expected_x=np.array([0, 0, 0, 0, 0]),
  929. expected_w=np.array([10, 9, 8, 7, 6]),
  930. )
  931. self._check_box_coord(
  932. ax.patches[5:10],
  933. expected_x=np.array([0, 0, 0, 0, 0]),
  934. expected_w=np.array([8, 8, 8, 8, 8]),
  935. )
  936. self._check_box_coord(
  937. ax.patches[10:],
  938. expected_x=np.array([0, 0, 0, 0, 0]),
  939. expected_w=np.array([6, 7, 8, 9, 10]),
  940. )
  941. ax = df.plot.hist(bins=5, stacked=True, orientation="horizontal")
  942. self._check_box_coord(
  943. ax.patches[:5],
  944. expected_x=np.array([0, 0, 0, 0, 0]),
  945. expected_w=np.array([10, 9, 8, 7, 6]),
  946. )
  947. self._check_box_coord(
  948. ax.patches[5:10],
  949. expected_x=np.array([10, 9, 8, 7, 6]),
  950. expected_w=np.array([8, 8, 8, 8, 8]),
  951. )
  952. self._check_box_coord(
  953. ax.patches[10:],
  954. expected_x=np.array([18, 17, 16, 15, 14]),
  955. expected_w=np.array([6, 7, 8, 9, 10]),
  956. )
  957. axes = df.plot.hist(
  958. bins=5, stacked=True, subplots=True, orientation="horizontal"
  959. )
  960. self._check_box_coord(
  961. axes[0].patches,
  962. expected_x=np.array([0, 0, 0, 0, 0]),
  963. expected_w=np.array([10, 9, 8, 7, 6]),
  964. )
  965. self._check_box_coord(
  966. axes[1].patches,
  967. expected_x=np.array([0, 0, 0, 0, 0]),
  968. expected_w=np.array([8, 8, 8, 8, 8]),
  969. )
  970. self._check_box_coord(
  971. axes[2].patches,
  972. expected_x=np.array([0, 0, 0, 0, 0]),
  973. expected_w=np.array([6, 7, 8, 9, 10]),
  974. )
  975. def test_plot_int_columns(self):
  976. df = DataFrame(np.random.randn(100, 4)).cumsum()
  977. _check_plot_works(df.plot, legend=True)
  978. def test_style_by_column(self):
  979. import matplotlib.pyplot as plt
  980. fig = plt.gcf()
  981. df = DataFrame(np.random.randn(100, 3))
  982. for markers in [
  983. {0: "^", 1: "+", 2: "o"},
  984. {0: "^", 1: "+"},
  985. ["^", "+", "o"],
  986. ["^", "+"],
  987. ]:
  988. fig.clf()
  989. fig.add_subplot(111)
  990. ax = df.plot(style=markers)
  991. for idx, line in enumerate(ax.get_lines()[: len(markers)]):
  992. assert line.get_marker() == markers[idx]
  993. def test_line_label_none(self):
  994. s = Series([1, 2])
  995. ax = s.plot()
  996. assert ax.get_legend() is None
  997. ax = s.plot(legend=True)
  998. assert ax.get_legend().get_texts()[0].get_text() == ""
  999. @pytest.mark.parametrize(
  1000. "props, expected",
  1001. [
  1002. ("boxprops", "boxes"),
  1003. ("whiskerprops", "whiskers"),
  1004. ("capprops", "caps"),
  1005. ("medianprops", "medians"),
  1006. ],
  1007. )
  1008. def test_specified_props_kwd_plot_box(self, props, expected):
  1009. # GH 30346
  1010. df = DataFrame({k: np.random.random(100) for k in "ABC"})
  1011. kwd = {props: {"color": "C1"}}
  1012. result = df.plot.box(return_type="dict", **kwd)
  1013. assert result[expected][0].get_color() == "C1"
  1014. def test_unordered_ts(self):
  1015. df = DataFrame(
  1016. np.array([3.0, 2.0, 1.0]),
  1017. index=[date(2012, 10, 1), date(2012, 9, 1), date(2012, 8, 1)],
  1018. columns=["test"],
  1019. )
  1020. ax = df.plot()
  1021. xticks = ax.lines[0].get_xdata()
  1022. assert xticks[0] < xticks[1]
  1023. ydata = ax.lines[0].get_ydata()
  1024. tm.assert_numpy_array_equal(ydata, np.array([1.0, 2.0, 3.0]))
  1025. @td.skip_if_no_scipy
  1026. def test_kind_both_ways(self):
  1027. df = DataFrame({"x": [1, 2, 3]})
  1028. for kind in plotting.PlotAccessor._common_kinds:
  1029. df.plot(kind=kind)
  1030. getattr(df.plot, kind)()
  1031. for kind in ["scatter", "hexbin"]:
  1032. df.plot("x", "x", kind=kind)
  1033. getattr(df.plot, kind)("x", "x")
  1034. def test_all_invalid_plot_data(self):
  1035. df = DataFrame(list("abcd"))
  1036. for kind in plotting.PlotAccessor._common_kinds:
  1037. msg = "no numeric data to plot"
  1038. with pytest.raises(TypeError, match=msg):
  1039. df.plot(kind=kind)
  1040. def test_partially_invalid_plot_data(self):
  1041. df = DataFrame(np.random.RandomState(42).randn(10, 2), dtype=object)
  1042. df[np.random.rand(df.shape[0]) > 0.5] = "a"
  1043. for kind in plotting.PlotAccessor._common_kinds:
  1044. msg = "no numeric data to plot"
  1045. with pytest.raises(TypeError, match=msg):
  1046. df.plot(kind=kind)
  1047. # area plot doesn't support positive/negative mixed data
  1048. df = DataFrame(np.random.RandomState(42).rand(10, 2), dtype=object)
  1049. df[np.random.rand(df.shape[0]) > 0.5] = "a"
  1050. with pytest.raises(TypeError, match="no numeric data to plot"):
  1051. df.plot(kind="area")
  1052. def test_invalid_kind(self):
  1053. df = DataFrame(np.random.randn(10, 2))
  1054. msg = "invalid_plot_kind is not a valid plot kind"
  1055. with pytest.raises(ValueError, match=msg):
  1056. df.plot(kind="invalid_plot_kind")
  1057. @pytest.mark.parametrize(
  1058. "x,y,lbl",
  1059. [
  1060. (["B", "C"], "A", "a"),
  1061. (["A"], ["B", "C"], ["b", "c"]),
  1062. ],
  1063. )
  1064. def test_invalid_xy_args(self, x, y, lbl):
  1065. # GH 18671, 19699 allows y to be list-like but not x
  1066. df = DataFrame({"A": [1, 2], "B": [3, 4], "C": [5, 6]})
  1067. with pytest.raises(ValueError, match="x must be a label or position"):
  1068. df.plot(x=x, y=y, label=lbl)
  1069. def test_bad_label(self):
  1070. df = DataFrame({"A": [1, 2], "B": [3, 4], "C": [5, 6]})
  1071. msg = "label should be list-like and same length as y"
  1072. with pytest.raises(ValueError, match=msg):
  1073. df.plot(x="A", y=["B", "C"], label="bad_label")
  1074. @pytest.mark.parametrize("x,y", [("A", "B"), (["A"], "B")])
  1075. def test_invalid_xy_args_dup_cols(self, x, y):
  1076. # GH 18671, 19699 allows y to be list-like but not x
  1077. df = DataFrame([[1, 3, 5], [2, 4, 6]], columns=list("AAB"))
  1078. with pytest.raises(ValueError, match="x must be a label or position"):
  1079. df.plot(x=x, y=y)
  1080. @pytest.mark.parametrize(
  1081. "x,y,lbl,colors",
  1082. [
  1083. ("A", ["B"], ["b"], ["red"]),
  1084. ("A", ["B", "C"], ["b", "c"], ["red", "blue"]),
  1085. (0, [1, 2], ["bokeh", "cython"], ["green", "yellow"]),
  1086. ],
  1087. )
  1088. def test_y_listlike(self, x, y, lbl, colors):
  1089. # GH 19699: tests list-like y and verifies lbls & colors
  1090. df = DataFrame({"A": [1, 2], "B": [3, 4], "C": [5, 6]})
  1091. _check_plot_works(df.plot, x="A", y=y, label=lbl)
  1092. ax = df.plot(x=x, y=y, label=lbl, color=colors)
  1093. assert len(ax.lines) == len(y)
  1094. self._check_colors(ax.get_lines(), linecolors=colors)
  1095. @pytest.mark.parametrize("x,y,colnames", [(0, 1, ["A", "B"]), (1, 0, [0, 1])])
  1096. def test_xy_args_integer(self, x, y, colnames):
  1097. # GH 20056: tests integer args for xy and checks col names
  1098. df = DataFrame({"A": [1, 2], "B": [3, 4]})
  1099. df.columns = colnames
  1100. _check_plot_works(df.plot, x=x, y=y)
  1101. def test_hexbin_basic(self):
  1102. df = DataFrame(
  1103. {
  1104. "A": np.random.uniform(size=20),
  1105. "B": np.random.uniform(size=20),
  1106. "C": np.arange(20) + np.random.uniform(size=20),
  1107. }
  1108. )
  1109. ax = df.plot.hexbin(x="A", y="B", gridsize=10)
  1110. # TODO: need better way to test. This just does existence.
  1111. assert len(ax.collections) == 1
  1112. # GH 6951
  1113. axes = df.plot.hexbin(x="A", y="B", subplots=True)
  1114. # hexbin should have 2 axes in the figure, 1 for plotting and another
  1115. # is colorbar
  1116. assert len(axes[0].figure.axes) == 2
  1117. # return value is single axes
  1118. self._check_axes_shape(axes, axes_num=1, layout=(1, 1))
  1119. def test_hexbin_with_c(self):
  1120. df = DataFrame(
  1121. {
  1122. "A": np.random.uniform(size=20),
  1123. "B": np.random.uniform(size=20),
  1124. "C": np.arange(20) + np.random.uniform(size=20),
  1125. }
  1126. )
  1127. ax = df.plot.hexbin(x="A", y="B", C="C")
  1128. assert len(ax.collections) == 1
  1129. ax = df.plot.hexbin(x="A", y="B", C="C", reduce_C_function=np.std)
  1130. assert len(ax.collections) == 1
  1131. @pytest.mark.parametrize(
  1132. "kwargs, expected",
  1133. [
  1134. ({}, "BuGn"), # default cmap
  1135. ({"colormap": "cubehelix"}, "cubehelix"),
  1136. ({"cmap": "YlGn"}, "YlGn"),
  1137. ],
  1138. )
  1139. def test_hexbin_cmap(self, kwargs, expected):
  1140. df = DataFrame(
  1141. {
  1142. "A": np.random.uniform(size=20),
  1143. "B": np.random.uniform(size=20),
  1144. "C": np.arange(20) + np.random.uniform(size=20),
  1145. }
  1146. )
  1147. ax = df.plot.hexbin(x="A", y="B", **kwargs)
  1148. assert ax.collections[0].cmap.name == expected
  1149. def test_pie_df(self):
  1150. df = DataFrame(
  1151. np.random.rand(5, 3),
  1152. columns=["X", "Y", "Z"],
  1153. index=["a", "b", "c", "d", "e"],
  1154. )
  1155. msg = "pie requires either y column or 'subplots=True'"
  1156. with pytest.raises(ValueError, match=msg):
  1157. df.plot.pie()
  1158. ax = _check_plot_works(df.plot.pie, y="Y")
  1159. self._check_text_labels(ax.texts, df.index)
  1160. ax = _check_plot_works(df.plot.pie, y=2)
  1161. self._check_text_labels(ax.texts, df.index)
  1162. axes = _check_plot_works(
  1163. df.plot.pie,
  1164. default_axes=True,
  1165. subplots=True,
  1166. )
  1167. assert len(axes) == len(df.columns)
  1168. for ax in axes:
  1169. self._check_text_labels(ax.texts, df.index)
  1170. for ax, ylabel in zip(axes, df.columns):
  1171. assert ax.get_ylabel() == ylabel
  1172. labels = ["A", "B", "C", "D", "E"]
  1173. color_args = ["r", "g", "b", "c", "m"]
  1174. axes = _check_plot_works(
  1175. df.plot.pie,
  1176. default_axes=True,
  1177. subplots=True,
  1178. labels=labels,
  1179. colors=color_args,
  1180. )
  1181. assert len(axes) == len(df.columns)
  1182. for ax in axes:
  1183. self._check_text_labels(ax.texts, labels)
  1184. self._check_colors(ax.patches, facecolors=color_args)
  1185. def test_pie_df_nan(self):
  1186. import matplotlib as mpl
  1187. df = DataFrame(np.random.rand(4, 4))
  1188. for i in range(4):
  1189. df.iloc[i, i] = np.nan
  1190. fig, axes = self.plt.subplots(ncols=4)
  1191. # GH 37668
  1192. kwargs = {}
  1193. if mpl.__version__ >= "3.3":
  1194. kwargs = {"normalize": True}
  1195. with tm.assert_produces_warning(None):
  1196. df.plot.pie(subplots=True, ax=axes, legend=True, **kwargs)
  1197. base_expected = ["0", "1", "2", "3"]
  1198. for i, ax in enumerate(axes):
  1199. expected = list(base_expected) # force copy
  1200. expected[i] = ""
  1201. result = [x.get_text() for x in ax.texts]
  1202. assert result == expected
  1203. # legend labels
  1204. # NaN's not included in legend with subplots
  1205. # see https://github.com/pandas-dev/pandas/issues/8390
  1206. result_labels = [x.get_text() for x in ax.get_legend().get_texts()]
  1207. expected_labels = base_expected[:i] + base_expected[i + 1 :]
  1208. assert result_labels == expected_labels
  1209. @pytest.mark.slow
  1210. def test_errorbar_plot(self):
  1211. d = {"x": np.arange(12), "y": np.arange(12, 0, -1)}
  1212. df = DataFrame(d)
  1213. d_err = {"x": np.ones(12) * 0.2, "y": np.ones(12) * 0.4}
  1214. df_err = DataFrame(d_err)
  1215. # check line plots
  1216. ax = _check_plot_works(df.plot, yerr=df_err, logy=True)
  1217. self._check_has_errorbars(ax, xerr=0, yerr=2)
  1218. ax = _check_plot_works(df.plot, yerr=df_err, logx=True, logy=True)
  1219. self._check_has_errorbars(ax, xerr=0, yerr=2)
  1220. ax = _check_plot_works(df.plot, yerr=df_err, loglog=True)
  1221. self._check_has_errorbars(ax, xerr=0, yerr=2)
  1222. ax = _check_plot_works(
  1223. (df + 1).plot, yerr=df_err, xerr=df_err, kind="bar", log=True
  1224. )
  1225. self._check_has_errorbars(ax, xerr=2, yerr=2)
  1226. # yerr is raw error values
  1227. ax = _check_plot_works(df["y"].plot, yerr=np.ones(12) * 0.4)
  1228. self._check_has_errorbars(ax, xerr=0, yerr=1)
  1229. ax = _check_plot_works(df.plot, yerr=np.ones((2, 12)) * 0.4)
  1230. self._check_has_errorbars(ax, xerr=0, yerr=2)
  1231. # yerr is column name
  1232. for yerr in ["yerr", "誤差"]:
  1233. s_df = df.copy()
  1234. s_df[yerr] = np.ones(12) * 0.2
  1235. ax = _check_plot_works(s_df.plot, yerr=yerr)
  1236. self._check_has_errorbars(ax, xerr=0, yerr=2)
  1237. ax = _check_plot_works(s_df.plot, y="y", x="x", yerr=yerr)
  1238. self._check_has_errorbars(ax, xerr=0, yerr=1)
  1239. with tm.external_error_raised(ValueError):
  1240. df.plot(yerr=np.random.randn(11))
  1241. df_err = DataFrame({"x": ["zzz"] * 12, "y": ["zzz"] * 12})
  1242. with tm.external_error_raised(TypeError):
  1243. df.plot(yerr=df_err)
  1244. @pytest.mark.slow
  1245. @pytest.mark.parametrize("kind", ["line", "bar", "barh"])
  1246. def test_errorbar_plot_different_kinds(self, kind):
  1247. d = {"x": np.arange(12), "y": np.arange(12, 0, -1)}
  1248. df = DataFrame(d)
  1249. d_err = {"x": np.ones(12) * 0.2, "y": np.ones(12) * 0.4}
  1250. df_err = DataFrame(d_err)
  1251. ax = _check_plot_works(df.plot, yerr=df_err["x"], kind=kind)
  1252. self._check_has_errorbars(ax, xerr=0, yerr=2)
  1253. ax = _check_plot_works(df.plot, yerr=d_err, kind=kind)
  1254. self._check_has_errorbars(ax, xerr=0, yerr=2)
  1255. ax = _check_plot_works(df.plot, yerr=df_err, xerr=df_err, kind=kind)
  1256. self._check_has_errorbars(ax, xerr=2, yerr=2)
  1257. ax = _check_plot_works(df.plot, yerr=df_err["x"], xerr=df_err["x"], kind=kind)
  1258. self._check_has_errorbars(ax, xerr=2, yerr=2)
  1259. ax = _check_plot_works(df.plot, xerr=0.2, yerr=0.2, kind=kind)
  1260. self._check_has_errorbars(ax, xerr=2, yerr=2)
  1261. axes = _check_plot_works(
  1262. df.plot,
  1263. default_axes=True,
  1264. yerr=df_err,
  1265. xerr=df_err,
  1266. subplots=True,
  1267. kind=kind,
  1268. )
  1269. self._check_has_errorbars(axes, xerr=1, yerr=1)
  1270. @pytest.mark.xfail(reason="Iterator is consumed", raises=ValueError)
  1271. def test_errorbar_plot_iterator(self):
  1272. with warnings.catch_warnings():
  1273. d = {"x": np.arange(12), "y": np.arange(12, 0, -1)}
  1274. df = DataFrame(d)
  1275. # yerr is iterator
  1276. ax = _check_plot_works(df.plot, yerr=itertools.repeat(0.1, len(df)))
  1277. self._check_has_errorbars(ax, xerr=0, yerr=2)
  1278. def test_errorbar_with_integer_column_names(self):
  1279. # test with integer column names
  1280. df = DataFrame(np.abs(np.random.randn(10, 2)))
  1281. df_err = DataFrame(np.abs(np.random.randn(10, 2)))
  1282. ax = _check_plot_works(df.plot, yerr=df_err)
  1283. self._check_has_errorbars(ax, xerr=0, yerr=2)
  1284. ax = _check_plot_works(df.plot, y=0, yerr=1)
  1285. self._check_has_errorbars(ax, xerr=0, yerr=1)
  1286. @pytest.mark.slow
  1287. def test_errorbar_with_partial_columns(self):
  1288. df = DataFrame(np.abs(np.random.randn(10, 3)))
  1289. df_err = DataFrame(np.abs(np.random.randn(10, 2)), columns=[0, 2])
  1290. kinds = ["line", "bar"]
  1291. for kind in kinds:
  1292. ax = _check_plot_works(df.plot, yerr=df_err, kind=kind)
  1293. self._check_has_errorbars(ax, xerr=0, yerr=2)
  1294. ix = date_range("1/1/2000", periods=10, freq="M")
  1295. df.set_index(ix, inplace=True)
  1296. df_err.set_index(ix, inplace=True)
  1297. ax = _check_plot_works(df.plot, yerr=df_err, kind="line")
  1298. self._check_has_errorbars(ax, xerr=0, yerr=2)
  1299. d = {"x": np.arange(12), "y": np.arange(12, 0, -1)}
  1300. df = DataFrame(d)
  1301. d_err = {"x": np.ones(12) * 0.2, "z": np.ones(12) * 0.4}
  1302. df_err = DataFrame(d_err)
  1303. for err in [d_err, df_err]:
  1304. ax = _check_plot_works(df.plot, yerr=err)
  1305. self._check_has_errorbars(ax, xerr=0, yerr=1)
  1306. @pytest.mark.parametrize("kind", ["line", "bar", "barh"])
  1307. def test_errorbar_timeseries(self, kind):
  1308. d = {"x": np.arange(12), "y": np.arange(12, 0, -1)}
  1309. d_err = {"x": np.ones(12) * 0.2, "y": np.ones(12) * 0.4}
  1310. # check time-series plots
  1311. ix = date_range("1/1/2000", "1/1/2001", freq="M")
  1312. tdf = DataFrame(d, index=ix)
  1313. tdf_err = DataFrame(d_err, index=ix)
  1314. ax = _check_plot_works(tdf.plot, yerr=tdf_err, kind=kind)
  1315. self._check_has_errorbars(ax, xerr=0, yerr=2)
  1316. ax = _check_plot_works(tdf.plot, yerr=d_err, kind=kind)
  1317. self._check_has_errorbars(ax, xerr=0, yerr=2)
  1318. ax = _check_plot_works(tdf.plot, y="y", yerr=tdf_err["x"], kind=kind)
  1319. self._check_has_errorbars(ax, xerr=0, yerr=1)
  1320. ax = _check_plot_works(tdf.plot, y="y", yerr="x", kind=kind)
  1321. self._check_has_errorbars(ax, xerr=0, yerr=1)
  1322. ax = _check_plot_works(tdf.plot, yerr=tdf_err, kind=kind)
  1323. self._check_has_errorbars(ax, xerr=0, yerr=2)
  1324. axes = _check_plot_works(
  1325. tdf.plot,
  1326. default_axes=True,
  1327. kind=kind,
  1328. yerr=tdf_err,
  1329. subplots=True,
  1330. )
  1331. self._check_has_errorbars(axes, xerr=0, yerr=1)
  1332. def test_errorbar_asymmetrical(self):
  1333. np.random.seed(0)
  1334. err = np.random.rand(3, 2, 5)
  1335. # each column is [0, 1, 2, 3, 4], [3, 4, 5, 6, 7]...
  1336. df = DataFrame(np.arange(15).reshape(3, 5)).T
  1337. ax = df.plot(yerr=err, xerr=err / 2)
  1338. yerr_0_0 = ax.collections[1].get_paths()[0].vertices[:, 1]
  1339. expected_0_0 = err[0, :, 0] * np.array([-1, 1])
  1340. tm.assert_almost_equal(yerr_0_0, expected_0_0)
  1341. msg = re.escape(
  1342. "Asymmetrical error bars should be provided with the shape (3, 2, 5)"
  1343. )
  1344. with pytest.raises(ValueError, match=msg):
  1345. df.plot(yerr=err.T)
  1346. tm.close()
  1347. def test_table(self):
  1348. df = DataFrame(np.random.rand(10, 3), index=list(string.ascii_letters[:10]))
  1349. _check_plot_works(df.plot, table=True)
  1350. _check_plot_works(df.plot, table=df)
  1351. # GH 35945 UserWarning
  1352. with tm.assert_produces_warning(None):
  1353. ax = df.plot()
  1354. assert len(ax.tables) == 0
  1355. plotting.table(ax, df.T)
  1356. assert len(ax.tables) == 1
  1357. def test_errorbar_scatter(self):
  1358. df = DataFrame(
  1359. np.abs(np.random.randn(5, 2)), index=range(5), columns=["x", "y"]
  1360. )
  1361. df_err = DataFrame(
  1362. np.abs(np.random.randn(5, 2)) / 5, index=range(5), columns=["x", "y"]
  1363. )
  1364. ax = _check_plot_works(df.plot.scatter, x="x", y="y")
  1365. self._check_has_errorbars(ax, xerr=0, yerr=0)
  1366. ax = _check_plot_works(df.plot.scatter, x="x", y="y", xerr=df_err)
  1367. self._check_has_errorbars(ax, xerr=1, yerr=0)
  1368. ax = _check_plot_works(df.plot.scatter, x="x", y="y", yerr=df_err)
  1369. self._check_has_errorbars(ax, xerr=0, yerr=1)
  1370. ax = _check_plot_works(df.plot.scatter, x="x", y="y", xerr=df_err, yerr=df_err)
  1371. self._check_has_errorbars(ax, xerr=1, yerr=1)
  1372. def _check_errorbar_color(containers, expected, has_err="has_xerr"):
  1373. lines = []
  1374. errs = [c.lines for c in ax.containers if getattr(c, has_err, False)][0]
  1375. for el in errs:
  1376. if is_list_like(el):
  1377. lines.extend(el)
  1378. else:
  1379. lines.append(el)
  1380. err_lines = [x for x in lines if x in ax.collections]
  1381. self._check_colors(
  1382. err_lines, linecolors=np.array([expected] * len(err_lines))
  1383. )
  1384. # GH 8081
  1385. df = DataFrame(
  1386. np.abs(np.random.randn(10, 5)), columns=["a", "b", "c", "d", "e"]
  1387. )
  1388. ax = df.plot.scatter(x="a", y="b", xerr="d", yerr="e", c="red")
  1389. self._check_has_errorbars(ax, xerr=1, yerr=1)
  1390. _check_errorbar_color(ax.containers, "red", has_err="has_xerr")
  1391. _check_errorbar_color(ax.containers, "red", has_err="has_yerr")
  1392. ax = df.plot.scatter(x="a", y="b", yerr="e", color="green")
  1393. self._check_has_errorbars(ax, xerr=0, yerr=1)
  1394. _check_errorbar_color(ax.containers, "green", has_err="has_yerr")
  1395. def test_scatter_unknown_colormap(self):
  1396. # GH#48726
  1397. df = DataFrame({"a": [1, 2, 3], "b": 4})
  1398. with pytest.raises((ValueError, KeyError), match="'unknown' is not a"):
  1399. df.plot(x="a", y="b", colormap="unknown", kind="scatter")
  1400. def test_sharex_and_ax(self):
  1401. # https://github.com/pandas-dev/pandas/issues/9737 using gridspec,
  1402. # the axis in fig.get_axis() are sorted differently than pandas
  1403. # expected them, so make sure that only the right ones are removed
  1404. import matplotlib.pyplot as plt
  1405. plt.close("all")
  1406. gs, axes = _generate_4_axes_via_gridspec()
  1407. df = DataFrame(
  1408. {
  1409. "a": [1, 2, 3, 4, 5, 6],
  1410. "b": [1, 2, 3, 4, 5, 6],
  1411. "c": [1, 2, 3, 4, 5, 6],
  1412. "d": [1, 2, 3, 4, 5, 6],
  1413. }
  1414. )
  1415. def _check(axes):
  1416. for ax in axes:
  1417. assert len(ax.lines) == 1
  1418. self._check_visible(ax.get_yticklabels(), visible=True)
  1419. for ax in [axes[0], axes[2]]:
  1420. self._check_visible(ax.get_xticklabels(), visible=False)
  1421. self._check_visible(ax.get_xticklabels(minor=True), visible=False)
  1422. for ax in [axes[1], axes[3]]:
  1423. self._check_visible(ax.get_xticklabels(), visible=True)
  1424. self._check_visible(ax.get_xticklabels(minor=True), visible=True)
  1425. for ax in axes:
  1426. df.plot(x="a", y="b", title="title", ax=ax, sharex=True)
  1427. gs.tight_layout(plt.gcf())
  1428. _check(axes)
  1429. tm.close()
  1430. gs, axes = _generate_4_axes_via_gridspec()
  1431. with tm.assert_produces_warning(UserWarning):
  1432. axes = df.plot(subplots=True, ax=axes, sharex=True)
  1433. _check(axes)
  1434. tm.close()
  1435. gs, axes = _generate_4_axes_via_gridspec()
  1436. # without sharex, no labels should be touched!
  1437. for ax in axes:
  1438. df.plot(x="a", y="b", title="title", ax=ax)
  1439. gs.tight_layout(plt.gcf())
  1440. for ax in axes:
  1441. assert len(ax.lines) == 1
  1442. self._check_visible(ax.get_yticklabels(), visible=True)
  1443. self._check_visible(ax.get_xticklabels(), visible=True)
  1444. self._check_visible(ax.get_xticklabels(minor=True), visible=True)
  1445. tm.close()
  1446. def test_sharey_and_ax(self):
  1447. # https://github.com/pandas-dev/pandas/issues/9737 using gridspec,
  1448. # the axis in fig.get_axis() are sorted differently than pandas
  1449. # expected them, so make sure that only the right ones are removed
  1450. import matplotlib.pyplot as plt
  1451. gs, axes = _generate_4_axes_via_gridspec()
  1452. df = DataFrame(
  1453. {
  1454. "a": [1, 2, 3, 4, 5, 6],
  1455. "b": [1, 2, 3, 4, 5, 6],
  1456. "c": [1, 2, 3, 4, 5, 6],
  1457. "d": [1, 2, 3, 4, 5, 6],
  1458. }
  1459. )
  1460. def _check(axes):
  1461. for ax in axes:
  1462. assert len(ax.lines) == 1
  1463. self._check_visible(ax.get_xticklabels(), visible=True)
  1464. self._check_visible(ax.get_xticklabels(minor=True), visible=True)
  1465. for ax in [axes[0], axes[1]]:
  1466. self._check_visible(ax.get_yticklabels(), visible=True)
  1467. for ax in [axes[2], axes[3]]:
  1468. self._check_visible(ax.get_yticklabels(), visible=False)
  1469. for ax in axes:
  1470. df.plot(x="a", y="b", title="title", ax=ax, sharey=True)
  1471. gs.tight_layout(plt.gcf())
  1472. _check(axes)
  1473. tm.close()
  1474. gs, axes = _generate_4_axes_via_gridspec()
  1475. with tm.assert_produces_warning(UserWarning):
  1476. axes = df.plot(subplots=True, ax=axes, sharey=True)
  1477. gs.tight_layout(plt.gcf())
  1478. _check(axes)
  1479. tm.close()
  1480. gs, axes = _generate_4_axes_via_gridspec()
  1481. # without sharex, no labels should be touched!
  1482. for ax in axes:
  1483. df.plot(x="a", y="b", title="title", ax=ax)
  1484. gs.tight_layout(plt.gcf())
  1485. for ax in axes:
  1486. assert len(ax.lines) == 1
  1487. self._check_visible(ax.get_yticklabels(), visible=True)
  1488. self._check_visible(ax.get_xticklabels(), visible=True)
  1489. self._check_visible(ax.get_xticklabels(minor=True), visible=True)
  1490. @td.skip_if_no_scipy
  1491. def test_memory_leak(self):
  1492. """Check that every plot type gets properly collected."""
  1493. results = {}
  1494. for kind in plotting.PlotAccessor._all_kinds:
  1495. args = {}
  1496. if kind in ["hexbin", "scatter", "pie"]:
  1497. df = DataFrame(
  1498. {
  1499. "A": np.random.uniform(size=20),
  1500. "B": np.random.uniform(size=20),
  1501. "C": np.arange(20) + np.random.uniform(size=20),
  1502. }
  1503. )
  1504. args = {"x": "A", "y": "B"}
  1505. elif kind == "area":
  1506. df = tm.makeTimeDataFrame().abs()
  1507. else:
  1508. df = tm.makeTimeDataFrame()
  1509. # Use a weakref so we can see if the object gets collected without
  1510. # also preventing it from being collected
  1511. results[kind] = weakref.proxy(df.plot(kind=kind, **args))
  1512. # have matplotlib delete all the figures
  1513. tm.close()
  1514. # force a garbage collection
  1515. gc.collect()
  1516. msg = "weakly-referenced object no longer exists"
  1517. for result_value in results.values():
  1518. # check that every plot was collected
  1519. with pytest.raises(ReferenceError, match=msg):
  1520. # need to actually access something to get an error
  1521. result_value.lines
  1522. def test_df_gridspec_patterns(self):
  1523. # GH 10819
  1524. from matplotlib import gridspec
  1525. import matplotlib.pyplot as plt
  1526. ts = Series(np.random.randn(10), index=date_range("1/1/2000", periods=10))
  1527. df = DataFrame(np.random.randn(10, 2), index=ts.index, columns=list("AB"))
  1528. def _get_vertical_grid():
  1529. gs = gridspec.GridSpec(3, 1)
  1530. fig = plt.figure()
  1531. ax1 = fig.add_subplot(gs[:2, :])
  1532. ax2 = fig.add_subplot(gs[2, :])
  1533. return ax1, ax2
  1534. def _get_horizontal_grid():
  1535. gs = gridspec.GridSpec(1, 3)
  1536. fig = plt.figure()
  1537. ax1 = fig.add_subplot(gs[:, :2])
  1538. ax2 = fig.add_subplot(gs[:, 2])
  1539. return ax1, ax2
  1540. for ax1, ax2 in [_get_vertical_grid(), _get_horizontal_grid()]:
  1541. ax1 = ts.plot(ax=ax1)
  1542. assert len(ax1.lines) == 1
  1543. ax2 = df.plot(ax=ax2)
  1544. assert len(ax2.lines) == 2
  1545. for ax in [ax1, ax2]:
  1546. self._check_visible(ax.get_yticklabels(), visible=True)
  1547. self._check_visible(ax.get_xticklabels(), visible=True)
  1548. self._check_visible(ax.get_xticklabels(minor=True), visible=True)
  1549. tm.close()
  1550. # subplots=True
  1551. for ax1, ax2 in [_get_vertical_grid(), _get_horizontal_grid()]:
  1552. axes = df.plot(subplots=True, ax=[ax1, ax2])
  1553. assert len(ax1.lines) == 1
  1554. assert len(ax2.lines) == 1
  1555. for ax in axes:
  1556. self._check_visible(ax.get_yticklabels(), visible=True)
  1557. self._check_visible(ax.get_xticklabels(), visible=True)
  1558. self._check_visible(ax.get_xticklabels(minor=True), visible=True)
  1559. tm.close()
  1560. # vertical / subplots / sharex=True / sharey=True
  1561. ax1, ax2 = _get_vertical_grid()
  1562. with tm.assert_produces_warning(UserWarning):
  1563. axes = df.plot(subplots=True, ax=[ax1, ax2], sharex=True, sharey=True)
  1564. assert len(axes[0].lines) == 1
  1565. assert len(axes[1].lines) == 1
  1566. for ax in [ax1, ax2]:
  1567. # yaxis are visible because there is only one column
  1568. self._check_visible(ax.get_yticklabels(), visible=True)
  1569. # xaxis of axes0 (top) are hidden
  1570. self._check_visible(axes[0].get_xticklabels(), visible=False)
  1571. self._check_visible(axes[0].get_xticklabels(minor=True), visible=False)
  1572. self._check_visible(axes[1].get_xticklabels(), visible=True)
  1573. self._check_visible(axes[1].get_xticklabels(minor=True), visible=True)
  1574. tm.close()
  1575. # horizontal / subplots / sharex=True / sharey=True
  1576. ax1, ax2 = _get_horizontal_grid()
  1577. with tm.assert_produces_warning(UserWarning):
  1578. axes = df.plot(subplots=True, ax=[ax1, ax2], sharex=True, sharey=True)
  1579. assert len(axes[0].lines) == 1
  1580. assert len(axes[1].lines) == 1
  1581. self._check_visible(axes[0].get_yticklabels(), visible=True)
  1582. # yaxis of axes1 (right) are hidden
  1583. self._check_visible(axes[1].get_yticklabels(), visible=False)
  1584. for ax in [ax1, ax2]:
  1585. # xaxis are visible because there is only one column
  1586. self._check_visible(ax.get_xticklabels(), visible=True)
  1587. self._check_visible(ax.get_xticklabels(minor=True), visible=True)
  1588. tm.close()
  1589. # boxed
  1590. def _get_boxed_grid():
  1591. gs = gridspec.GridSpec(3, 3)
  1592. fig = plt.figure()
  1593. ax1 = fig.add_subplot(gs[:2, :2])
  1594. ax2 = fig.add_subplot(gs[:2, 2])
  1595. ax3 = fig.add_subplot(gs[2, :2])
  1596. ax4 = fig.add_subplot(gs[2, 2])
  1597. return ax1, ax2, ax3, ax4
  1598. axes = _get_boxed_grid()
  1599. df = DataFrame(np.random.randn(10, 4), index=ts.index, columns=list("ABCD"))
  1600. axes = df.plot(subplots=True, ax=axes)
  1601. for ax in axes:
  1602. assert len(ax.lines) == 1
  1603. # axis are visible because these are not shared
  1604. self._check_visible(ax.get_yticklabels(), visible=True)
  1605. self._check_visible(ax.get_xticklabels(), visible=True)
  1606. self._check_visible(ax.get_xticklabels(minor=True), visible=True)
  1607. tm.close()
  1608. # subplots / sharex=True / sharey=True
  1609. axes = _get_boxed_grid()
  1610. with tm.assert_produces_warning(UserWarning):
  1611. axes = df.plot(subplots=True, ax=axes, sharex=True, sharey=True)
  1612. for ax in axes:
  1613. assert len(ax.lines) == 1
  1614. for ax in [axes[0], axes[2]]: # left column
  1615. self._check_visible(ax.get_yticklabels(), visible=True)
  1616. for ax in [axes[1], axes[3]]: # right column
  1617. self._check_visible(ax.get_yticklabels(), visible=False)
  1618. for ax in [axes[0], axes[1]]: # top row
  1619. self._check_visible(ax.get_xticklabels(), visible=False)
  1620. self._check_visible(ax.get_xticklabels(minor=True), visible=False)
  1621. for ax in [axes[2], axes[3]]: # bottom row
  1622. self._check_visible(ax.get_xticklabels(), visible=True)
  1623. self._check_visible(ax.get_xticklabels(minor=True), visible=True)
  1624. tm.close()
  1625. def test_df_grid_settings(self):
  1626. # Make sure plot defaults to rcParams['axes.grid'] setting, GH 9792
  1627. self._check_grid_settings(
  1628. DataFrame({"a": [1, 2, 3], "b": [2, 3, 4]}),
  1629. plotting.PlotAccessor._dataframe_kinds,
  1630. kws={"x": "a", "y": "b"},
  1631. )
  1632. def test_plain_axes(self):
  1633. # supplied ax itself is a SubplotAxes, but figure contains also
  1634. # a plain Axes object (GH11556)
  1635. fig, ax = self.plt.subplots()
  1636. fig.add_axes([0.2, 0.2, 0.2, 0.2])
  1637. Series(np.random.rand(10)).plot(ax=ax)
  1638. # supplied ax itself is a plain Axes, but because the cmap keyword
  1639. # a new ax is created for the colorbar -> also multiples axes (GH11520)
  1640. df = DataFrame({"a": np.random.randn(8), "b": np.random.randn(8)})
  1641. fig = self.plt.figure()
  1642. ax = fig.add_axes((0, 0, 1, 1))
  1643. df.plot(kind="scatter", ax=ax, x="a", y="b", c="a", cmap="hsv")
  1644. # other examples
  1645. fig, ax = self.plt.subplots()
  1646. from mpl_toolkits.axes_grid1 import make_axes_locatable
  1647. divider = make_axes_locatable(ax)
  1648. cax = divider.append_axes("right", size="5%", pad=0.05)
  1649. Series(np.random.rand(10)).plot(ax=ax)
  1650. Series(np.random.rand(10)).plot(ax=cax)
  1651. fig, ax = self.plt.subplots()
  1652. from mpl_toolkits.axes_grid1.inset_locator import inset_axes
  1653. iax = inset_axes(ax, width="30%", height=1.0, loc=3)
  1654. Series(np.random.rand(10)).plot(ax=ax)
  1655. Series(np.random.rand(10)).plot(ax=iax)
  1656. @pytest.mark.parametrize("method", ["line", "barh", "bar"])
  1657. def test_secondary_axis_font_size(self, method):
  1658. # GH: 12565
  1659. df = (
  1660. DataFrame(np.random.randn(15, 2), columns=list("AB"))
  1661. .assign(C=lambda df: df.B.cumsum())
  1662. .assign(D=lambda df: df.C * 1.1)
  1663. )
  1664. fontsize = 20
  1665. sy = ["C", "D"]
  1666. kwargs = {"secondary_y": sy, "fontsize": fontsize, "mark_right": True}
  1667. ax = getattr(df.plot, method)(**kwargs)
  1668. self._check_ticks_props(axes=ax.right_ax, ylabelsize=fontsize)
  1669. def test_x_string_values_ticks(self):
  1670. # Test if string plot index have a fixed xtick position
  1671. # GH: 7612, GH: 22334
  1672. df = DataFrame(
  1673. {
  1674. "sales": [3, 2, 3],
  1675. "visits": [20, 42, 28],
  1676. "day": ["Monday", "Tuesday", "Wednesday"],
  1677. }
  1678. )
  1679. ax = df.plot.area(x="day")
  1680. ax.set_xlim(-1, 3)
  1681. xticklabels = [t.get_text() for t in ax.get_xticklabels()]
  1682. labels_position = dict(zip(xticklabels, ax.get_xticks()))
  1683. # Testing if the label stayed at the right position
  1684. assert labels_position["Monday"] == 0.0
  1685. assert labels_position["Tuesday"] == 1.0
  1686. assert labels_position["Wednesday"] == 2.0
  1687. def test_x_multiindex_values_ticks(self):
  1688. # Test if multiindex plot index have a fixed xtick position
  1689. # GH: 15912
  1690. index = MultiIndex.from_product([[2012, 2013], [1, 2]])
  1691. df = DataFrame(np.random.randn(4, 2), columns=["A", "B"], index=index)
  1692. ax = df.plot()
  1693. ax.set_xlim(-1, 4)
  1694. xticklabels = [t.get_text() for t in ax.get_xticklabels()]
  1695. labels_position = dict(zip(xticklabels, ax.get_xticks()))
  1696. # Testing if the label stayed at the right position
  1697. assert labels_position["(2012, 1)"] == 0.0
  1698. assert labels_position["(2012, 2)"] == 1.0
  1699. assert labels_position["(2013, 1)"] == 2.0
  1700. assert labels_position["(2013, 2)"] == 3.0
  1701. @pytest.mark.parametrize("kind", ["line", "area"])
  1702. def test_xlim_plot_line(self, kind):
  1703. # test if xlim is set correctly in plot.line and plot.area
  1704. # GH 27686
  1705. df = DataFrame([2, 4], index=[1, 2])
  1706. ax = df.plot(kind=kind)
  1707. xlims = ax.get_xlim()
  1708. assert xlims[0] < 1
  1709. assert xlims[1] > 2
  1710. def test_xlim_plot_line_correctly_in_mixed_plot_type(self):
  1711. # test if xlim is set correctly when ax contains multiple different kinds
  1712. # of plots, GH 27686
  1713. fig, ax = self.plt.subplots()
  1714. indexes = ["k1", "k2", "k3", "k4"]
  1715. df = DataFrame(
  1716. {
  1717. "s1": [1000, 2000, 1500, 2000],
  1718. "s2": [900, 1400, 2000, 3000],
  1719. "s3": [1500, 1500, 1600, 1200],
  1720. "secondary_y": [1, 3, 4, 3],
  1721. },
  1722. index=indexes,
  1723. )
  1724. df[["s1", "s2", "s3"]].plot.bar(ax=ax, stacked=False)
  1725. df[["secondary_y"]].plot(ax=ax, secondary_y=True)
  1726. xlims = ax.get_xlim()
  1727. assert xlims[0] < 0
  1728. assert xlims[1] > 3
  1729. # make sure axis labels are plotted correctly as well
  1730. xticklabels = [t.get_text() for t in ax.get_xticklabels()]
  1731. assert xticklabels == indexes
  1732. def test_plot_no_rows(self):
  1733. # GH 27758
  1734. df = DataFrame(columns=["foo"], dtype=int)
  1735. assert df.empty
  1736. ax = df.plot()
  1737. assert len(ax.get_lines()) == 1
  1738. line = ax.get_lines()[0]
  1739. assert len(line.get_xdata()) == 0
  1740. assert len(line.get_ydata()) == 0
  1741. def test_plot_no_numeric_data(self):
  1742. df = DataFrame(["a", "b", "c"])
  1743. with pytest.raises(TypeError, match="no numeric data to plot"):
  1744. df.plot()
  1745. @td.skip_if_no_scipy
  1746. @pytest.mark.parametrize(
  1747. "kind", ("line", "bar", "barh", "hist", "kde", "density", "area", "pie")
  1748. )
  1749. def test_group_subplot(self, kind):
  1750. d = {
  1751. "a": np.arange(10),
  1752. "b": np.arange(10) + 1,
  1753. "c": np.arange(10) + 1,
  1754. "d": np.arange(10),
  1755. "e": np.arange(10),
  1756. }
  1757. df = DataFrame(d)
  1758. axes = df.plot(subplots=[("b", "e"), ("c", "d")], kind=kind)
  1759. assert len(axes) == 3 # 2 groups + single column a
  1760. expected_labels = (["b", "e"], ["c", "d"], ["a"])
  1761. for ax, labels in zip(axes, expected_labels):
  1762. if kind != "pie":
  1763. self._check_legend_labels(ax, labels=labels)
  1764. if kind == "line":
  1765. assert len(ax.lines) == len(labels)
  1766. def test_group_subplot_series_notimplemented(self):
  1767. ser = Series(range(1))
  1768. msg = "An iterable subplots for a Series"
  1769. with pytest.raises(NotImplementedError, match=msg):
  1770. ser.plot(subplots=[("a",)])
  1771. def test_group_subplot_multiindex_notimplemented(self):
  1772. df = DataFrame(np.eye(2), columns=MultiIndex.from_tuples([(0, 1), (1, 2)]))
  1773. msg = "An iterable subplots for a DataFrame with a MultiIndex"
  1774. with pytest.raises(NotImplementedError, match=msg):
  1775. df.plot(subplots=[(0, 1)])
  1776. def test_group_subplot_nonunique_cols_notimplemented(self):
  1777. df = DataFrame(np.eye(2), columns=["a", "a"])
  1778. msg = "An iterable subplots for a DataFrame with non-unique"
  1779. with pytest.raises(NotImplementedError, match=msg):
  1780. df.plot(subplots=[("a",)])
  1781. @pytest.mark.parametrize(
  1782. "subplots, expected_msg",
  1783. [
  1784. (123, "subplots should be a bool or an iterable"),
  1785. ("a", "each entry should be a list/tuple"), # iterable of non-iterable
  1786. ((1,), "each entry should be a list/tuple"), # iterable of non-iterable
  1787. (("a",), "each entry should be a list/tuple"), # iterable of strings
  1788. ],
  1789. )
  1790. def test_group_subplot_bad_input(self, subplots, expected_msg):
  1791. # Make sure error is raised when subplots is not a properly
  1792. # formatted iterable. Only iterables of iterables are permitted, and
  1793. # entries should not be strings.
  1794. d = {"a": np.arange(10), "b": np.arange(10)}
  1795. df = DataFrame(d)
  1796. with pytest.raises(ValueError, match=expected_msg):
  1797. df.plot(subplots=subplots)
  1798. def test_group_subplot_invalid_column_name(self):
  1799. d = {"a": np.arange(10), "b": np.arange(10)}
  1800. df = DataFrame(d)
  1801. with pytest.raises(ValueError, match=r"Column label\(s\) \['bad_name'\]"):
  1802. df.plot(subplots=[("a", "bad_name")])
  1803. def test_group_subplot_duplicated_column(self):
  1804. d = {"a": np.arange(10), "b": np.arange(10), "c": np.arange(10)}
  1805. df = DataFrame(d)
  1806. with pytest.raises(ValueError, match="should be in only one subplot"):
  1807. df.plot(subplots=[("a", "b"), ("a", "c")])
  1808. @pytest.mark.parametrize("kind", ("box", "scatter", "hexbin"))
  1809. def test_group_subplot_invalid_kind(self, kind):
  1810. d = {"a": np.arange(10), "b": np.arange(10)}
  1811. df = DataFrame(d)
  1812. with pytest.raises(
  1813. ValueError, match="When subplots is an iterable, kind must be one of"
  1814. ):
  1815. df.plot(subplots=[("a", "b")], kind=kind)
  1816. @pytest.mark.parametrize(
  1817. "index_name, old_label, new_label",
  1818. [
  1819. (None, "", "new"),
  1820. ("old", "old", "new"),
  1821. (None, "", ""),
  1822. (None, "", 1),
  1823. (None, "", [1, 2]),
  1824. ],
  1825. )
  1826. @pytest.mark.parametrize("kind", ["line", "area", "bar"])
  1827. def test_xlabel_ylabel_dataframe_single_plot(
  1828. self, kind, index_name, old_label, new_label
  1829. ):
  1830. # GH 9093
  1831. df = DataFrame([[1, 2], [2, 5]], columns=["Type A", "Type B"])
  1832. df.index.name = index_name
  1833. # default is the ylabel is not shown and xlabel is index name
  1834. ax = df.plot(kind=kind)
  1835. assert ax.get_xlabel() == old_label
  1836. assert ax.get_ylabel() == ""
  1837. # old xlabel will be overridden and assigned ylabel will be used as ylabel
  1838. ax = df.plot(kind=kind, ylabel=new_label, xlabel=new_label)
  1839. assert ax.get_ylabel() == str(new_label)
  1840. assert ax.get_xlabel() == str(new_label)
  1841. @pytest.mark.parametrize(
  1842. "xlabel, ylabel",
  1843. [
  1844. (None, None),
  1845. ("X Label", None),
  1846. (None, "Y Label"),
  1847. ("X Label", "Y Label"),
  1848. ],
  1849. )
  1850. @pytest.mark.parametrize("kind", ["scatter", "hexbin"])
  1851. def test_xlabel_ylabel_dataframe_plane_plot(self, kind, xlabel, ylabel):
  1852. # GH 37001
  1853. xcol = "Type A"
  1854. ycol = "Type B"
  1855. df = DataFrame([[1, 2], [2, 5]], columns=[xcol, ycol])
  1856. # default is the labels are column names
  1857. ax = df.plot(kind=kind, x=xcol, y=ycol, xlabel=xlabel, ylabel=ylabel)
  1858. assert ax.get_xlabel() == (xcol if xlabel is None else xlabel)
  1859. assert ax.get_ylabel() == (ycol if ylabel is None else ylabel)
  1860. @pytest.mark.parametrize("secondary_y", (False, True))
  1861. def test_secondary_y(self, secondary_y):
  1862. ax_df = DataFrame([0]).plot(
  1863. secondary_y=secondary_y, ylabel="Y", ylim=(0, 100), yticks=[99]
  1864. )
  1865. for ax in ax_df.figure.axes:
  1866. if ax.yaxis.get_visible():
  1867. assert ax.get_ylabel() == "Y"
  1868. assert ax.get_ylim() == (0, 100)
  1869. assert ax.get_yticks()[0] == 99
  1870. def _generate_4_axes_via_gridspec():
  1871. import matplotlib as mpl
  1872. import matplotlib.gridspec
  1873. import matplotlib.pyplot as plt
  1874. gs = mpl.gridspec.GridSpec(2, 2)
  1875. ax_tl = plt.subplot(gs[0, 0])
  1876. ax_ll = plt.subplot(gs[1, 0])
  1877. ax_tr = plt.subplot(gs[0, 1])
  1878. ax_lr = plt.subplot(gs[1, 1])
  1879. return gs, [ax_tl, ax_ll, ax_tr, ax_lr]