test_frame.py 97 KB

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