test_datetime.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. import datetime
  2. import numpy as np
  3. import pytest
  4. import matplotlib.pyplot as plt
  5. import matplotlib as mpl
  6. class TestDatetimePlotting:
  7. @mpl.style.context("default")
  8. def test_annotate(self):
  9. mpl.rcParams["date.converter"] = 'concise'
  10. fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, layout="constrained")
  11. start_date = datetime.datetime(2023, 10, 1)
  12. dates = [start_date + datetime.timedelta(days=i) for i in range(31)]
  13. data = list(range(1, 32))
  14. test_text = "Test Text"
  15. ax1.plot(dates, data)
  16. ax1.annotate(text=test_text, xy=(dates[15], data[15]))
  17. ax2.plot(data, dates)
  18. ax2.annotate(text=test_text, xy=(data[5], dates[26]))
  19. ax3.plot(dates, dates)
  20. ax3.annotate(text=test_text, xy=(dates[15], dates[3]))
  21. ax4.plot(dates, dates)
  22. ax4.annotate(text=test_text, xy=(dates[5], dates[30]),
  23. xytext=(dates[1], dates[7]), arrowprops=dict(facecolor='red'))
  24. @pytest.mark.xfail(reason="Test for arrow not written yet")
  25. @mpl.style.context("default")
  26. def test_arrow(self):
  27. fig, ax = plt.subplots()
  28. ax.arrow(...)
  29. @mpl.style.context("default")
  30. def test_axhline(self):
  31. mpl.rcParams["date.converter"] = 'concise'
  32. fig, (ax1, ax2, ax3) = plt.subplots(3, 1, layout='constrained')
  33. ax1.set_ylim(bottom=datetime.datetime(2020, 4, 1),
  34. top=datetime.datetime(2020, 8, 1))
  35. ax2.set_ylim(bottom=np.datetime64('2005-01-01'),
  36. top=np.datetime64('2005-04-01'))
  37. ax3.set_ylim(bottom=datetime.datetime(2023, 9, 1),
  38. top=datetime.datetime(2023, 11, 1))
  39. ax1.axhline(y=datetime.datetime(2020, 6, 3), xmin=0.5, xmax=0.7)
  40. ax2.axhline(np.datetime64('2005-02-25T03:30'), xmin=0.1, xmax=0.9)
  41. ax3.axhline(y=datetime.datetime(2023, 10, 24), xmin=0.4, xmax=0.7)
  42. @mpl.style.context("default")
  43. def test_axhspan(self):
  44. mpl.rcParams["date.converter"] = 'concise'
  45. start_date = datetime.datetime(2023, 1, 1)
  46. dates = [start_date + datetime.timedelta(days=i) for i in range(31)]
  47. numbers = list(range(1, 32))
  48. fig, (ax1, ax2, ax3) = plt.subplots(3, 1,
  49. constrained_layout=True,
  50. figsize=(10, 12))
  51. ax1.plot(dates, numbers, marker='o', color='blue')
  52. for i in range(0, 31, 2):
  53. ax1.axhspan(ymin=i+1, ymax=i+2, facecolor='green', alpha=0.5)
  54. ax1.set_title('Datetime vs. Number')
  55. ax1.set_xlabel('Date')
  56. ax1.set_ylabel('Number')
  57. ax2.plot(numbers, dates, marker='o', color='blue')
  58. for i in range(0, 31, 2):
  59. ymin = start_date + datetime.timedelta(days=i)
  60. ymax = ymin + datetime.timedelta(days=1)
  61. ax2.axhspan(ymin=ymin, ymax=ymax, facecolor='green', alpha=0.5)
  62. ax2.set_title('Number vs. Datetime')
  63. ax2.set_xlabel('Number')
  64. ax2.set_ylabel('Date')
  65. ax3.plot(dates, dates, marker='o', color='blue')
  66. for i in range(0, 31, 2):
  67. ymin = start_date + datetime.timedelta(days=i)
  68. ymax = ymin + datetime.timedelta(days=1)
  69. ax3.axhspan(ymin=ymin, ymax=ymax, facecolor='green', alpha=0.5)
  70. ax3.set_title('Datetime vs. Datetime')
  71. ax3.set_xlabel('Date')
  72. ax3.set_ylabel('Date')
  73. @pytest.mark.xfail(reason="Test for axline not written yet")
  74. @mpl.style.context("default")
  75. def test_axline(self):
  76. fig, ax = plt.subplots()
  77. ax.axline(...)
  78. @mpl.style.context("default")
  79. def test_axvline(self):
  80. mpl.rcParams["date.converter"] = 'concise'
  81. fig, (ax1, ax2, ax3) = plt.subplots(3, 1, layout='constrained')
  82. ax1.set_xlim(left=datetime.datetime(2020, 4, 1),
  83. right=datetime.datetime(2020, 8, 1))
  84. ax2.set_xlim(left=np.datetime64('2005-01-01'),
  85. right=np.datetime64('2005-04-01'))
  86. ax3.set_xlim(left=datetime.datetime(2023, 9, 1),
  87. right=datetime.datetime(2023, 11, 1))
  88. ax1.axvline(x=datetime.datetime(2020, 6, 3), ymin=0.5, ymax=0.7)
  89. ax2.axvline(np.datetime64('2005-02-25T03:30'), ymin=0.1, ymax=0.9)
  90. ax3.axvline(x=datetime.datetime(2023, 10, 24), ymin=0.4, ymax=0.7)
  91. @mpl.style.context("default")
  92. def test_axvspan(self):
  93. mpl.rcParams["date.converter"] = 'concise'
  94. start_date = datetime.datetime(2023, 1, 1)
  95. dates = [start_date + datetime.timedelta(days=i) for i in range(31)]
  96. numbers = list(range(1, 32))
  97. fig, (ax1, ax2, ax3) = plt.subplots(3, 1,
  98. constrained_layout=True,
  99. figsize=(10, 12))
  100. ax1.plot(dates, numbers, marker='o', color='blue')
  101. for i in range(0, 31, 2):
  102. xmin = start_date + datetime.timedelta(days=i)
  103. xmax = xmin + datetime.timedelta(days=1)
  104. ax1.axvspan(xmin=xmin, xmax=xmax, facecolor='red', alpha=0.5)
  105. ax1.set_title('Datetime vs. Number')
  106. ax1.set_xlabel('Date')
  107. ax1.set_ylabel('Number')
  108. ax2.plot(numbers, dates, marker='o', color='blue')
  109. for i in range(0, 31, 2):
  110. ax2.axvspan(xmin=i+1, xmax=i+2, facecolor='red', alpha=0.5)
  111. ax2.set_title('Number vs. Datetime')
  112. ax2.set_xlabel('Number')
  113. ax2.set_ylabel('Date')
  114. ax3.plot(dates, dates, marker='o', color='blue')
  115. for i in range(0, 31, 2):
  116. xmin = start_date + datetime.timedelta(days=i)
  117. xmax = xmin + datetime.timedelta(days=1)
  118. ax3.axvspan(xmin=xmin, xmax=xmax, facecolor='red', alpha=0.5)
  119. ax3.set_title('Datetime vs. Datetime')
  120. ax3.set_xlabel('Date')
  121. ax3.set_ylabel('Date')
  122. @mpl.style.context("default")
  123. def test_bar(self):
  124. mpl.rcParams["date.converter"] = "concise"
  125. fig, (ax1, ax2) = plt.subplots(2, 1, layout="constrained")
  126. x_dates = np.array(
  127. [
  128. datetime.datetime(2020, 6, 30),
  129. datetime.datetime(2020, 7, 22),
  130. datetime.datetime(2020, 8, 3),
  131. datetime.datetime(2020, 9, 14),
  132. ],
  133. dtype=np.datetime64,
  134. )
  135. x_ranges = [8800, 2600, 8500, 7400]
  136. x = np.datetime64(datetime.datetime(2020, 6, 1))
  137. ax1.bar(x_dates, x_ranges, width=np.timedelta64(4, "D"))
  138. ax2.bar(np.arange(4), x_dates - x, bottom=x)
  139. @mpl.style.context("default")
  140. def test_bar_label(self):
  141. # Generate some example data with dateTime inputs
  142. date_list = [datetime.datetime(2023, 1, 1) +
  143. datetime.timedelta(days=i) for i in range(5)]
  144. values = [10, 20, 15, 25, 30]
  145. # Creating the plot
  146. fig, ax = plt.subplots(1, 1, figsize=(10, 8), layout='constrained')
  147. bars = ax.bar(date_list, values)
  148. # Add labels to the bars using bar_label
  149. ax.bar_label(bars, labels=[f'{val}%' for val in values],
  150. label_type='edge', color='black')
  151. @mpl.style.context("default")
  152. def test_barbs(self):
  153. plt.rcParams["date.converter"] = 'concise'
  154. start_date = datetime.datetime(2022, 2, 8, 22)
  155. dates = [start_date + datetime.timedelta(hours=i) for i in range(12)]
  156. numbers = np.sin(np.linspace(0, 2 * np.pi, 12))
  157. u = np.ones(12) * 10
  158. v = np.arange(0, 120, 10)
  159. fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(12, 6))
  160. axes[0].barbs(dates, numbers, u, v, length=7)
  161. axes[0].set_title('Datetime vs. Numeric Data')
  162. axes[0].set_xlabel('Datetime')
  163. axes[0].set_ylabel('Numeric Data')
  164. axes[1].barbs(numbers, dates, u, v, length=7)
  165. axes[1].set_title('Numeric vs. Datetime Data')
  166. axes[1].set_xlabel('Numeric Data')
  167. axes[1].set_ylabel('Datetime')
  168. @mpl.style.context("default")
  169. def test_barh(self):
  170. mpl.rcParams["date.converter"] = 'concise'
  171. fig, (ax1, ax2) = plt.subplots(2, 1, layout='constrained')
  172. birth_date = np.array([datetime.datetime(2020, 4, 10),
  173. datetime.datetime(2020, 5, 30),
  174. datetime.datetime(2020, 10, 12),
  175. datetime.datetime(2020, 11, 15)])
  176. year_start = datetime.datetime(2020, 1, 1)
  177. year_end = datetime.datetime(2020, 12, 31)
  178. age = [21, 53, 20, 24]
  179. ax1.set_xlabel('Age')
  180. ax1.set_ylabel('Birth Date')
  181. ax1.barh(birth_date, width=age, height=datetime.timedelta(days=10))
  182. ax2.set_xlim(left=year_start, right=year_end)
  183. ax2.set_xlabel('Birth Date')
  184. ax2.set_ylabel('Order of Birth Dates')
  185. ax2.barh(np.arange(4), birth_date-year_start, left=year_start)
  186. @pytest.mark.xfail(reason="Test for boxplot not written yet")
  187. @mpl.style.context("default")
  188. def test_boxplot(self):
  189. fig, ax = plt.subplots()
  190. ax.boxplot(...)
  191. @mpl.style.context("default")
  192. def test_broken_barh(self):
  193. # Horizontal bar plot with gaps
  194. mpl.rcParams["date.converter"] = 'concise'
  195. fig, ax = plt.subplots()
  196. ax.broken_barh([(datetime.datetime(2023, 1, 4), datetime.timedelta(days=2)),
  197. (datetime.datetime(2023, 1, 8), datetime.timedelta(days=3))],
  198. (10, 9), facecolors='tab:blue')
  199. ax.broken_barh([(datetime.datetime(2023, 1, 2), datetime.timedelta(days=1)),
  200. (datetime.datetime(2023, 1, 4), datetime.timedelta(days=4))],
  201. (20, 9), facecolors=('tab:red'))
  202. @mpl.style.context("default")
  203. def test_bxp(self):
  204. mpl.rcParams["date.converter"] = 'concise'
  205. fig, ax = plt.subplots()
  206. data = [{
  207. "med": datetime.datetime(2020, 1, 15),
  208. "q1": datetime.datetime(2020, 1, 10),
  209. "q3": datetime.datetime(2020, 1, 20),
  210. "whislo": datetime.datetime(2020, 1, 5),
  211. "whishi": datetime.datetime(2020, 1, 25),
  212. "fliers": [
  213. datetime.datetime(2020, 1, 3),
  214. datetime.datetime(2020, 1, 27)
  215. ]
  216. }]
  217. ax.bxp(data, orientation='horizontal')
  218. ax.xaxis.set_major_formatter(mpl.dates.DateFormatter("%Y-%m-%d"))
  219. ax.set_title('Box plot with datetime data')
  220. @pytest.mark.xfail(reason="Test for clabel not written yet")
  221. @mpl.style.context("default")
  222. def test_clabel(self):
  223. fig, ax = plt.subplots()
  224. ax.clabel(...)
  225. @mpl.style.context("default")
  226. def test_contour(self):
  227. mpl.rcParams["date.converter"] = "concise"
  228. range_threshold = 10
  229. fig, (ax1, ax2, ax3) = plt.subplots(3, 1, layout="constrained")
  230. x_dates = np.array(
  231. [datetime.datetime(2023, 10, delta) for delta in range(1, range_threshold)]
  232. )
  233. y_dates = np.array(
  234. [datetime.datetime(2023, 10, delta) for delta in range(1, range_threshold)]
  235. )
  236. x_ranges = np.array(range(1, range_threshold))
  237. y_ranges = np.array(range(1, range_threshold))
  238. X_dates, Y_dates = np.meshgrid(x_dates, y_dates)
  239. X_ranges, Y_ranges = np.meshgrid(x_ranges, y_ranges)
  240. Z_ranges = np.cos(X_ranges / 4) + np.sin(Y_ranges / 4)
  241. ax1.contour(X_dates, Y_dates, Z_ranges)
  242. ax2.contour(X_dates, Y_ranges, Z_ranges)
  243. ax3.contour(X_ranges, Y_dates, Z_ranges)
  244. @mpl.style.context("default")
  245. def test_contourf(self):
  246. mpl.rcParams["date.converter"] = "concise"
  247. range_threshold = 10
  248. fig, (ax1, ax2, ax3) = plt.subplots(3, 1, layout="constrained")
  249. x_dates = np.array(
  250. [datetime.datetime(2023, 10, delta) for delta in range(1, range_threshold)]
  251. )
  252. y_dates = np.array(
  253. [datetime.datetime(2023, 10, delta) for delta in range(1, range_threshold)]
  254. )
  255. x_ranges = np.array(range(1, range_threshold))
  256. y_ranges = np.array(range(1, range_threshold))
  257. X_dates, Y_dates = np.meshgrid(x_dates, y_dates)
  258. X_ranges, Y_ranges = np.meshgrid(x_ranges, y_ranges)
  259. Z_ranges = np.cos(X_ranges / 4) + np.sin(Y_ranges / 4)
  260. ax1.contourf(X_dates, Y_dates, Z_ranges)
  261. ax2.contourf(X_dates, Y_ranges, Z_ranges)
  262. ax3.contourf(X_ranges, Y_dates, Z_ranges)
  263. @mpl.style.context("default")
  264. def test_errorbar(self):
  265. mpl.rcParams["date.converter"] = "concise"
  266. fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, layout="constrained")
  267. limit = 7
  268. start_date = datetime.datetime(2023, 1, 1)
  269. x_dates = np.array([datetime.datetime(2023, 10, d) for d in range(1, limit)])
  270. y_dates = np.array([datetime.datetime(2023, 10, d) for d in range(1, limit)])
  271. x_date_error = datetime.timedelta(days=1)
  272. y_date_error = datetime.timedelta(days=1)
  273. x_values = list(range(1, limit))
  274. y_values = list(range(1, limit))
  275. x_value_error = 0.5
  276. y_value_error = 0.5
  277. ax1.errorbar(x_dates, y_values,
  278. yerr=y_value_error,
  279. capsize=10,
  280. barsabove=True,
  281. label='Data')
  282. ax2.errorbar(x_values, y_dates,
  283. xerr=x_value_error, yerr=y_date_error,
  284. errorevery=(1, 2),
  285. fmt='-o', label='Data')
  286. ax3.errorbar(x_dates, y_dates,
  287. xerr=x_date_error, yerr=y_date_error,
  288. lolims=True, xlolims=True,
  289. label='Data')
  290. ax4.errorbar(x_dates, y_values,
  291. xerr=x_date_error, yerr=y_value_error,
  292. uplims=True, xuplims=True,
  293. label='Data')
  294. @mpl.style.context("default")
  295. def test_eventplot(self):
  296. mpl.rcParams["date.converter"] = "concise"
  297. fig, (ax1, ax2, ax3) = plt.subplots(3, 1, layout="constrained")
  298. x_dates1 = np.array([datetime.datetime(2020, 6, 30),
  299. datetime.datetime(2020, 7, 22),
  300. datetime.datetime(2020, 8, 3),
  301. datetime.datetime(2020, 9, 14),],
  302. dtype=np.datetime64,
  303. )
  304. ax1.eventplot(x_dates1)
  305. np.random.seed(19680801)
  306. start_date = datetime.datetime(2020, 7, 1)
  307. end_date = datetime.datetime(2020, 10, 15)
  308. date_range = end_date - start_date
  309. dates1 = start_date + np.random.rand(30) * date_range
  310. dates2 = start_date + np.random.rand(10) * date_range
  311. dates3 = start_date + np.random.rand(50) * date_range
  312. colors1 = ['C1', 'C2', 'C3']
  313. lineoffsets1 = np.array([1, 6, 8])
  314. linelengths1 = [5, 2, 3]
  315. ax2.eventplot([dates1, dates2, dates3],
  316. colors=colors1,
  317. lineoffsets=lineoffsets1,
  318. linelengths=linelengths1)
  319. lineoffsets2 = np.array([
  320. datetime.datetime(2020, 7, 1),
  321. datetime.datetime(2020, 7, 15),
  322. datetime.datetime(2020, 8, 1)
  323. ], dtype=np.datetime64)
  324. ax3.eventplot([dates1, dates2, dates3],
  325. colors=colors1,
  326. lineoffsets=lineoffsets2,
  327. linelengths=linelengths1)
  328. @mpl.style.context("default")
  329. def test_fill(self):
  330. mpl.rcParams["date.converter"] = "concise"
  331. fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, layout="constrained")
  332. np.random.seed(19680801)
  333. x_base_date = datetime.datetime(2023, 1, 1)
  334. x_dates = [x_base_date]
  335. for _ in range(1, 5):
  336. x_base_date += datetime.timedelta(days=np.random.randint(1, 5))
  337. x_dates.append(x_base_date)
  338. y_base_date = datetime.datetime(2023, 1, 1)
  339. y_dates = [y_base_date]
  340. for _ in range(1, 5):
  341. y_base_date += datetime.timedelta(days=np.random.randint(1, 5))
  342. y_dates.append(y_base_date)
  343. x_values = np.random.rand(5) * 5
  344. y_values = np.random.rand(5) * 5 - 2
  345. ax1.fill(x_dates, y_values)
  346. ax2.fill(x_values, y_dates)
  347. ax3.fill(x_values, y_values)
  348. ax4.fill(x_dates, y_dates)
  349. @mpl.style.context("default")
  350. def test_fill_between(self):
  351. mpl.rcParams["date.converter"] = "concise"
  352. np.random.seed(19680801)
  353. y_base_date = datetime.datetime(2023, 1, 1)
  354. y_dates1 = [y_base_date]
  355. for i in range(1, 10):
  356. y_base_date += datetime.timedelta(days=np.random.randint(1, 5))
  357. y_dates1.append(y_base_date)
  358. y_dates2 = [y_base_date]
  359. for i in range(1, 10):
  360. y_base_date += datetime.timedelta(days=np.random.randint(1, 5))
  361. y_dates2.append(y_base_date)
  362. x_values = np.random.rand(10) * 10
  363. x_values.sort()
  364. y_values1 = np.random.rand(10) * 10
  365. y_values2 = y_values1 + np.random.rand(10) * 10
  366. y_values1.sort()
  367. y_values2.sort()
  368. x_base_date = datetime.datetime(2023, 1, 1)
  369. x_dates = [x_base_date]
  370. for i in range(1, 10):
  371. x_base_date += datetime.timedelta(days=np.random.randint(1, 10))
  372. x_dates.append(x_base_date)
  373. fig, (ax1, ax2, ax3) = plt.subplots(3, 1, layout="constrained")
  374. ax1.fill_between(x_values, y_dates1, y_dates2)
  375. ax2.fill_between(x_dates, y_values1, y_values2)
  376. ax3.fill_between(x_dates, y_dates1, y_dates2)
  377. @mpl.style.context("default")
  378. def test_fill_betweenx(self):
  379. mpl.rcParams["date.converter"] = "concise"
  380. np.random.seed(19680801)
  381. x_base_date = datetime.datetime(2023, 1, 1)
  382. x_dates1 = [x_base_date]
  383. for i in range(1, 10):
  384. x_base_date += datetime.timedelta(days=np.random.randint(1, 5))
  385. x_dates1.append(x_base_date)
  386. x_dates2 = [x_base_date]
  387. for i in range(1, 10):
  388. x_base_date += datetime.timedelta(days=np.random.randint(1, 5))
  389. x_dates2.append(x_base_date)
  390. y_values = np.random.rand(10) * 10
  391. y_values.sort()
  392. x_values1 = np.random.rand(10) * 10
  393. x_values2 = x_values1 + np.random.rand(10) * 10
  394. x_values1.sort()
  395. x_values2.sort()
  396. y_base_date = datetime.datetime(2023, 1, 1)
  397. y_dates = [y_base_date]
  398. for i in range(1, 10):
  399. y_base_date += datetime.timedelta(days=np.random.randint(1, 10))
  400. y_dates.append(y_base_date)
  401. fig, (ax1, ax2, ax3) = plt.subplots(1, 3, layout="constrained")
  402. ax1.fill_betweenx(y_values, x_dates1, x_dates2)
  403. ax2.fill_betweenx(y_dates, x_values1, x_values2)
  404. ax3.fill_betweenx(y_dates, x_dates1, x_dates2)
  405. @pytest.mark.xfail(reason="Test for hexbin not written yet")
  406. @mpl.style.context("default")
  407. def test_hexbin(self):
  408. fig, ax = plt.subplots()
  409. ax.hexbin(...)
  410. @mpl.style.context("default")
  411. def test_hist(self):
  412. mpl.rcParams["date.converter"] = 'concise'
  413. start_date = datetime.datetime(2023, 10, 1)
  414. time_delta = datetime.timedelta(days=1)
  415. values1 = np.random.randint(1, 10, 30)
  416. values2 = np.random.randint(1, 10, 30)
  417. values3 = np.random.randint(1, 10, 30)
  418. bin_edges = [start_date + i * time_delta for i in range(31)]
  419. fig, (ax1, ax2, ax3) = plt.subplots(3, 1, constrained_layout=True)
  420. ax1.hist(
  421. [start_date + i * time_delta for i in range(30)],
  422. bins=10,
  423. weights=values1
  424. )
  425. ax2.hist(
  426. [start_date + i * time_delta for i in range(30)],
  427. bins=10,
  428. weights=values2
  429. )
  430. ax3.hist(
  431. [start_date + i * time_delta for i in range(30)],
  432. bins=10,
  433. weights=values3
  434. )
  435. fig, (ax4, ax5, ax6) = plt.subplots(3, 1, constrained_layout=True)
  436. ax4.hist(
  437. [start_date + i * time_delta for i in range(30)],
  438. bins=bin_edges,
  439. weights=values1
  440. )
  441. ax5.hist(
  442. [start_date + i * time_delta for i in range(30)],
  443. bins=bin_edges,
  444. weights=values2
  445. )
  446. ax6.hist(
  447. [start_date + i * time_delta for i in range(30)],
  448. bins=bin_edges,
  449. weights=values3
  450. )
  451. @pytest.mark.xfail(reason="Test for hist2d not written yet")
  452. @mpl.style.context("default")
  453. def test_hist2d(self):
  454. fig, ax = plt.subplots()
  455. ax.hist2d(...)
  456. @mpl.style.context("default")
  457. def test_hlines(self):
  458. mpl.rcParams["date.converter"] = 'concise'
  459. fig, axs = plt.subplots(2, 4, layout='constrained')
  460. dateStrs = ['2023-03-08',
  461. '2023-04-09',
  462. '2023-05-13',
  463. '2023-07-28',
  464. '2023-12-24']
  465. dates = [datetime.datetime(2023, m*2, 10) for m in range(1, 6)]
  466. date_start = [datetime.datetime(2023, 6, d) for d in range(5, 30, 5)]
  467. date_end = [datetime.datetime(2023, 7, d) for d in range(5, 30, 5)]
  468. npDates = [np.datetime64(s) for s in dateStrs]
  469. axs[0, 0].hlines(y=dates,
  470. xmin=[0.1, 0.2, 0.3, 0.4, 0.5],
  471. xmax=[0.5, 0.6, 0.7, 0.8, 0.9])
  472. axs[0, 1].hlines(dates,
  473. xmin=datetime.datetime(2020, 5, 10),
  474. xmax=datetime.datetime(2020, 5, 31))
  475. axs[0, 2].hlines(dates,
  476. xmin=date_start,
  477. xmax=date_end)
  478. axs[0, 3].hlines(dates,
  479. xmin=0.45,
  480. xmax=0.65)
  481. axs[1, 0].hlines(y=npDates,
  482. xmin=[0.5, 0.6, 0.7, 0.8, 0.9],
  483. xmax=[0.1, 0.2, 0.3, 0.4, 0.5])
  484. axs[1, 2].hlines(y=npDates,
  485. xmin=date_start,
  486. xmax=date_end)
  487. axs[1, 1].hlines(npDates,
  488. xmin=datetime.datetime(2020, 5, 10),
  489. xmax=datetime.datetime(2020, 5, 31))
  490. axs[1, 3].hlines(npDates,
  491. xmin=0.45,
  492. xmax=0.65)
  493. @mpl.style.context("default")
  494. def test_imshow(self):
  495. fig, ax = plt.subplots()
  496. a = np.diag(range(5))
  497. dt_start = datetime.datetime(2010, 11, 1)
  498. dt_end = datetime.datetime(2010, 11, 11)
  499. extent = (dt_start, dt_end, dt_start, dt_end)
  500. ax.imshow(a, extent=extent)
  501. ax.tick_params(axis="x", labelrotation=90)
  502. @pytest.mark.xfail(reason="Test for loglog not written yet")
  503. @mpl.style.context("default")
  504. def test_loglog(self):
  505. fig, ax = plt.subplots()
  506. ax.loglog(...)
  507. @mpl.style.context("default")
  508. def test_matshow(self):
  509. a = np.diag(range(5))
  510. dt_start = datetime.datetime(1980, 4, 15)
  511. dt_end = datetime.datetime(2020, 11, 11)
  512. extent = (dt_start, dt_end, dt_start, dt_end)
  513. fig, ax = plt.subplots()
  514. ax.matshow(a, extent=extent)
  515. for label in ax.get_xticklabels():
  516. label.set_rotation(90)
  517. @pytest.mark.xfail(reason="Test for pcolor not written yet")
  518. @mpl.style.context("default")
  519. def test_pcolor(self):
  520. fig, ax = plt.subplots()
  521. ax.pcolor(...)
  522. @pytest.mark.xfail(reason="Test for pcolorfast not written yet")
  523. @mpl.style.context("default")
  524. def test_pcolorfast(self):
  525. fig, ax = plt.subplots()
  526. ax.pcolorfast(...)
  527. @pytest.mark.xfail(reason="Test for pcolormesh not written yet")
  528. @mpl.style.context("default")
  529. def test_pcolormesh(self):
  530. fig, ax = plt.subplots()
  531. ax.pcolormesh(...)
  532. @mpl.style.context("default")
  533. def test_plot(self):
  534. mpl.rcParams["date.converter"] = 'concise'
  535. N = 6
  536. fig, (ax1, ax2, ax3) = plt.subplots(3, 1, layout='constrained')
  537. x = np.array([datetime.datetime(2023, 9, n) for n in range(1, N)])
  538. ax1.plot(x, range(1, N))
  539. ax2.plot(range(1, N), x)
  540. ax3.plot(x, x)
  541. @mpl.style.context("default")
  542. def test_plot_date(self):
  543. mpl.rcParams["date.converter"] = "concise"
  544. range_threshold = 10
  545. fig, (ax1, ax2, ax3) = plt.subplots(3, 1, layout="constrained")
  546. x_dates = np.array(
  547. [datetime.datetime(2023, 10, delta) for delta in range(1, range_threshold)]
  548. )
  549. y_dates = np.array(
  550. [datetime.datetime(2023, 10, delta) for delta in range(1, range_threshold)]
  551. )
  552. x_ranges = np.array(range(1, range_threshold))
  553. y_ranges = np.array(range(1, range_threshold))
  554. with pytest.warns(mpl.MatplotlibDeprecationWarning):
  555. ax1.plot_date(x_dates, y_dates)
  556. ax2.plot_date(x_dates, y_ranges)
  557. ax3.plot_date(x_ranges, y_dates)
  558. @pytest.mark.xfail(reason="Test for quiver not written yet")
  559. @mpl.style.context("default")
  560. def test_quiver(self):
  561. fig, ax = plt.subplots()
  562. ax.quiver(...)
  563. @mpl.style.context("default")
  564. def test_scatter(self):
  565. mpl.rcParams["date.converter"] = 'concise'
  566. base = datetime.datetime(2005, 2, 1)
  567. dates = [base + datetime.timedelta(hours=(2 * i)) for i in range(10)]
  568. N = len(dates)
  569. np.random.seed(19680801)
  570. y = np.cumsum(np.random.randn(N))
  571. fig, axs = plt.subplots(3, 1, layout='constrained', figsize=(6, 6))
  572. # datetime array on x axis
  573. axs[0].scatter(dates, y)
  574. for label in axs[0].get_xticklabels():
  575. label.set_rotation(40)
  576. label.set_horizontalalignment('right')
  577. # datetime on y axis
  578. axs[1].scatter(y, dates)
  579. # datetime on both x, y axes
  580. axs[2].scatter(dates, dates)
  581. for label in axs[2].get_xticklabels():
  582. label.set_rotation(40)
  583. label.set_horizontalalignment('right')
  584. @pytest.mark.xfail(reason="Test for semilogx not written yet")
  585. @mpl.style.context("default")
  586. def test_semilogx(self):
  587. fig, ax = plt.subplots()
  588. ax.semilogx(...)
  589. @pytest.mark.xfail(reason="Test for semilogy not written yet")
  590. @mpl.style.context("default")
  591. def test_semilogy(self):
  592. fig, ax = plt.subplots()
  593. ax.semilogy(...)
  594. @mpl.style.context("default")
  595. def test_stackplot(self):
  596. mpl.rcParams["date.converter"] = 'concise'
  597. N = 10
  598. stacked_nums = np.tile(np.arange(1, N), (4, 1))
  599. dates = np.array([datetime.datetime(2020 + i, 1, 1) for i in range(N - 1)])
  600. fig, ax = plt.subplots(layout='constrained')
  601. ax.stackplot(dates, stacked_nums)
  602. @mpl.style.context("default")
  603. def test_stairs(self):
  604. mpl.rcParams["date.converter"] = 'concise'
  605. start_date = datetime.datetime(2023, 12, 1)
  606. time_delta = datetime.timedelta(days=1)
  607. baseline_date = datetime.datetime(1980, 1, 1)
  608. bin_edges = [start_date + i * time_delta for i in range(31)]
  609. edge_int = np.arange(31)
  610. np.random.seed(123456)
  611. values1 = np.random.randint(1, 100, 30)
  612. values2 = [start_date + datetime.timedelta(days=int(i))
  613. for i in np.random.randint(1, 10000, 30)]
  614. values3 = [start_date + datetime.timedelta(days=int(i))
  615. for i in np.random.randint(-10000, 10000, 30)]
  616. fig, (ax1, ax2, ax3) = plt.subplots(3, 1, constrained_layout=True)
  617. ax1.stairs(values1, edges=bin_edges)
  618. ax2.stairs(values2, edges=edge_int, baseline=baseline_date)
  619. ax3.stairs(values3, edges=bin_edges, baseline=baseline_date)
  620. @mpl.style.context("default")
  621. def test_stem(self):
  622. mpl.rcParams["date.converter"] = "concise"
  623. fig, (ax1, ax2, ax3, ax4, ax5, ax6) = plt.subplots(6, 1, layout="constrained")
  624. limit_value = 10
  625. above = datetime.datetime(2023, 9, 18)
  626. below = datetime.datetime(2023, 11, 18)
  627. x_ranges = np.arange(1, limit_value)
  628. y_ranges = np.arange(1, limit_value)
  629. x_dates = np.array(
  630. [datetime.datetime(2023, 10, n) for n in range(1, limit_value)]
  631. )
  632. y_dates = np.array(
  633. [datetime.datetime(2023, 10, n) for n in range(1, limit_value)]
  634. )
  635. ax1.stem(x_dates, y_dates, bottom=above)
  636. ax2.stem(x_dates, y_ranges, bottom=5)
  637. ax3.stem(x_ranges, y_dates, bottom=below)
  638. ax4.stem(x_ranges, y_dates, orientation="horizontal", bottom=above)
  639. ax5.stem(x_dates, y_ranges, orientation="horizontal", bottom=5)
  640. ax6.stem(x_ranges, y_dates, orientation="horizontal", bottom=below)
  641. @mpl.style.context("default")
  642. def test_step(self):
  643. mpl.rcParams["date.converter"] = "concise"
  644. N = 6
  645. fig, (ax1, ax2, ax3) = plt.subplots(3, 1, layout='constrained')
  646. x = np.array([datetime.datetime(2023, 9, n) for n in range(1, N)])
  647. ax1.step(x, range(1, N))
  648. ax2.step(range(1, N), x)
  649. ax3.step(x, x)
  650. @pytest.mark.xfail(reason="Test for streamplot not written yet")
  651. @mpl.style.context("default")
  652. def test_streamplot(self):
  653. fig, ax = plt.subplots()
  654. ax.streamplot(...)
  655. @mpl.style.context("default")
  656. def test_text(self):
  657. mpl.rcParams["date.converter"] = 'concise'
  658. fig, (ax1, ax2, ax3) = plt.subplots(3, 1, layout="constrained")
  659. limit_value = 10
  660. font_properties = {'family': 'serif', 'size': 12, 'weight': 'bold'}
  661. test_date = datetime.datetime(2023, 10, 1)
  662. x_data = np.array(range(1, limit_value))
  663. y_data = np.array(range(1, limit_value))
  664. x_dates = np.array(
  665. [datetime.datetime(2023, 10, n) for n in range(1, limit_value)]
  666. )
  667. y_dates = np.array(
  668. [datetime.datetime(2023, 10, n) for n in range(1, limit_value)]
  669. )
  670. ax1.plot(x_dates, y_data)
  671. ax1.text(test_date, 5, "Inserted Text", **font_properties)
  672. ax2.plot(x_data, y_dates)
  673. ax2.text(7, test_date, "Inserted Text", **font_properties)
  674. ax3.plot(x_dates, y_dates)
  675. ax3.text(test_date, test_date, "Inserted Text", **font_properties)
  676. @pytest.mark.xfail(reason="Test for tricontour not written yet")
  677. @mpl.style.context("default")
  678. def test_tricontour(self):
  679. fig, ax = plt.subplots()
  680. ax.tricontour(...)
  681. @pytest.mark.xfail(reason="Test for tricontourf not written yet")
  682. @mpl.style.context("default")
  683. def test_tricontourf(self):
  684. fig, ax = plt.subplots()
  685. ax.tricontourf(...)
  686. @pytest.mark.xfail(reason="Test for tripcolor not written yet")
  687. @mpl.style.context("default")
  688. def test_tripcolor(self):
  689. fig, ax = plt.subplots()
  690. ax.tripcolor(...)
  691. @pytest.mark.xfail(reason="Test for triplot not written yet")
  692. @mpl.style.context("default")
  693. def test_triplot(self):
  694. fig, ax = plt.subplots()
  695. ax.triplot(...)
  696. @pytest.mark.xfail(reason="Test for violin not written yet")
  697. @mpl.style.context("default")
  698. def test_violin(self):
  699. fig, ax = plt.subplots()
  700. ax.violin(...)
  701. @pytest.mark.xfail(reason="Test for violinplot not written yet")
  702. @mpl.style.context("default")
  703. def test_violinplot(self):
  704. fig, ax = plt.subplots()
  705. ax.violinplot(...)
  706. @mpl.style.context("default")
  707. def test_vlines(self):
  708. mpl.rcParams["date.converter"] = 'concise'
  709. fig, (ax1, ax2, ax3) = plt.subplots(3, 1, layout='constrained')
  710. ax1.set_xlim(left=datetime.datetime(2023, 1, 1),
  711. right=datetime.datetime(2023, 6, 30))
  712. ax1.vlines(x=[datetime.datetime(2023, 2, 10),
  713. datetime.datetime(2023, 5, 18),
  714. datetime.datetime(2023, 6, 6)],
  715. ymin=[0, 0.25, 0.5],
  716. ymax=[0.25, 0.5, 0.75])
  717. ax2.set_xlim(left=0,
  718. right=0.5)
  719. ax2.vlines(x=[0.3, 0.35],
  720. ymin=[np.datetime64('2023-03-20'), np.datetime64('2023-03-31')],
  721. ymax=[np.datetime64('2023-05-01'), np.datetime64('2023-05-16')])
  722. ax3.set_xlim(left=datetime.datetime(2023, 7, 1),
  723. right=datetime.datetime(2023, 12, 31))
  724. ax3.vlines(x=[datetime.datetime(2023, 9, 1), datetime.datetime(2023, 12, 10)],
  725. ymin=datetime.datetime(2023, 1, 15),
  726. ymax=datetime.datetime(2023, 1, 30))