test_usetex.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import numpy as np
  2. import pytest
  3. import matplotlib as mpl
  4. from matplotlib.testing.decorators import check_figures_equal, image_comparison
  5. import matplotlib.pyplot as plt
  6. if not mpl.checkdep_usetex(True):
  7. pytestmark = pytest.mark.skip('Missing TeX of Ghostscript or dvipng')
  8. @image_comparison(
  9. baseline_images=['test_usetex'],
  10. extensions=['pdf', 'png'],
  11. style="mpl20")
  12. def test_usetex():
  13. mpl.rcParams['text.usetex'] = True
  14. fig = plt.figure()
  15. ax = fig.add_subplot(111)
  16. kwargs = {"verticalalignment": "baseline", "size": 24,
  17. "bbox": dict(pad=0, edgecolor="k", facecolor="none")}
  18. ax.text(0.2, 0.7,
  19. # the \LaTeX macro exercises character sizing and placement,
  20. # \left[ ... \right\} draw some variable-height characters,
  21. # \sqrt and \frac draw horizontal rules, \mathrm changes the font
  22. r'\LaTeX\ $\left[\int\limits_e^{2e}'
  23. r'\sqrt\frac{\log^3 x}{x}\,\mathrm{d}x \right\}$',
  24. **kwargs)
  25. ax.text(0.2, 0.3, "lg", **kwargs)
  26. ax.text(0.4, 0.3, r"$\frac{1}{2}\pi$", **kwargs)
  27. ax.text(0.6, 0.3, "$p^{3^A}$", **kwargs)
  28. ax.text(0.8, 0.3, "$p_{3_2}$", **kwargs)
  29. for x in {t.get_position()[0] for t in ax.texts}:
  30. ax.axvline(x)
  31. for y in {t.get_position()[1] for t in ax.texts}:
  32. ax.axhline(y)
  33. ax.set_axis_off()
  34. @check_figures_equal()
  35. def test_empty(fig_test, fig_ref):
  36. mpl.rcParams['text.usetex'] = True
  37. fig_test.text(.5, .5, "% a comment")
  38. @check_figures_equal()
  39. def test_unicode_minus(fig_test, fig_ref):
  40. mpl.rcParams['text.usetex'] = True
  41. fig_test.text(.5, .5, "$-$")
  42. fig_ref.text(.5, .5, "\N{MINUS SIGN}")
  43. def test_mathdefault():
  44. plt.rcParams["axes.formatter.use_mathtext"] = True
  45. fig = plt.figure()
  46. fig.add_subplot().set_xlim(-1, 1)
  47. # Check that \mathdefault commands generated by tickers don't cause
  48. # problems when later switching usetex on.
  49. mpl.rcParams['text.usetex'] = True
  50. fig.canvas.draw()
  51. def test_minus_no_descent():
  52. # Test special-casing of minus descent in DviFont._height_depth_of, by
  53. # checking that overdrawing a 1 and a -1 results in an overall height
  54. # equivalent to drawing either of them separately.
  55. mpl.style.use("mpl20")
  56. heights = {}
  57. fig = plt.figure()
  58. for vals in [(1,), (-1,), (-1, 1)]:
  59. fig.clf()
  60. for x in vals:
  61. fig.text(.5, .5, f"${x}$", usetex=True)
  62. fig.canvas.draw()
  63. # The following counts the number of non-fully-blank pixel rows.
  64. heights[vals] = ((np.array(fig.canvas.buffer_rgba())[..., 0] != 255)
  65. .any(axis=1).sum())
  66. assert len({*heights.values()}) == 1
  67. def test_textcomp_full():
  68. plt.rcParams["text.latex.preamble"] = r"\usepackage[full]{textcomp}"
  69. fig = plt.figure()
  70. fig.text(.5, .5, "hello, world", usetex=True)
  71. fig.canvas.draw()