test_backend_bases.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import re
  2. from matplotlib.backend_bases import (
  3. FigureCanvasBase, LocationEvent, RendererBase)
  4. import matplotlib.pyplot as plt
  5. import matplotlib.transforms as transforms
  6. import matplotlib.path as path
  7. import os
  8. import numpy as np
  9. import pytest
  10. def test_uses_per_path():
  11. id = transforms.Affine2D()
  12. paths = [path.Path.unit_regular_polygon(i) for i in range(3, 7)]
  13. tforms = [id.rotate(i) for i in range(1, 5)]
  14. offsets = np.arange(20).reshape((10, 2))
  15. facecolors = ['red', 'green']
  16. edgecolors = ['red', 'green']
  17. def check(master_transform, paths, all_transforms,
  18. offsets, facecolors, edgecolors):
  19. rb = RendererBase()
  20. raw_paths = list(rb._iter_collection_raw_paths(
  21. master_transform, paths, all_transforms))
  22. gc = rb.new_gc()
  23. ids = [path_id for xo, yo, path_id, gc0, rgbFace in
  24. rb._iter_collection(gc, master_transform, all_transforms,
  25. range(len(raw_paths)), offsets,
  26. transforms.IdentityTransform(),
  27. facecolors, edgecolors, [], [], [False],
  28. [], 'data')]
  29. uses = rb._iter_collection_uses_per_path(
  30. paths, all_transforms, offsets, facecolors, edgecolors)
  31. if raw_paths:
  32. seen = np.bincount(ids, minlength=len(raw_paths))
  33. assert set(seen).issubset([uses - 1, uses])
  34. check(id, paths, tforms, offsets, facecolors, edgecolors)
  35. check(id, paths[0:1], tforms, offsets, facecolors, edgecolors)
  36. check(id, [], tforms, offsets, facecolors, edgecolors)
  37. check(id, paths, tforms[0:1], offsets, facecolors, edgecolors)
  38. check(id, paths, [], offsets, facecolors, edgecolors)
  39. for n in range(0, offsets.shape[0]):
  40. check(id, paths, tforms, offsets[0:n, :], facecolors, edgecolors)
  41. check(id, paths, tforms, offsets, [], edgecolors)
  42. check(id, paths, tforms, offsets, facecolors, [])
  43. check(id, paths, tforms, offsets, [], [])
  44. check(id, paths, tforms, offsets, facecolors[0:1], edgecolors)
  45. def test_get_default_filename(tmpdir):
  46. plt.rcParams['savefig.directory'] = str(tmpdir)
  47. fig = plt.figure()
  48. canvas = FigureCanvasBase(fig)
  49. filename = canvas.get_default_filename()
  50. assert filename == 'image.png'
  51. @pytest.mark.backend('pdf')
  52. def test_non_gui_warning(monkeypatch):
  53. plt.subplots()
  54. monkeypatch.setitem(os.environ, "DISPLAY", ":999")
  55. with pytest.warns(UserWarning) as rec:
  56. plt.show()
  57. assert len(rec) == 1
  58. assert ('Matplotlib is currently using pdf, which is a non-GUI backend'
  59. in str(rec[0].message))
  60. with pytest.warns(UserWarning) as rec:
  61. plt.gcf().show()
  62. assert len(rec) == 1
  63. assert ('Matplotlib is currently using pdf, which is a non-GUI backend'
  64. in str(rec[0].message))
  65. @pytest.mark.parametrize(
  66. "x, y", [(42, 24), (None, 42), (None, None), (200, 100.01), (205.75, 2.0)])
  67. def test_location_event_position(x, y):
  68. # LocationEvent should cast its x and y arguments to int unless it is None.
  69. fig, ax = plt.subplots()
  70. canvas = FigureCanvasBase(fig)
  71. event = LocationEvent("test_event", canvas, x, y)
  72. if x is None:
  73. assert event.x is None
  74. else:
  75. assert event.x == int(x)
  76. assert isinstance(event.x, int)
  77. if y is None:
  78. assert event.y is None
  79. else:
  80. assert event.y == int(y)
  81. assert isinstance(event.y, int)
  82. if x is not None and y is not None:
  83. assert re.match(
  84. "x={} +y={}".format(ax.format_xdata(x), ax.format_ydata(y)),
  85. ax.format_coord(x, y))
  86. ax.fmt_xdata = ax.fmt_ydata = lambda x: "foo"
  87. assert re.match("x=foo +y=foo", ax.format_coord(x, y))