| 1 |
import numpy as np |
| 2 |
from numpy.testing import assert_warns |
| 3 |
|
| 4 |
from ..exporter import Exporter |
| 5 |
from ..renderers import FakeRenderer, FullFakeRenderer |
| 6 |
from . import plt |
| 7 |
|
| 8 |
|
| 9 |
def fake_renderer_output(fig, Renderer): |
| 10 |
renderer = Renderer() |
| 11 |
exporter = Exporter(renderer) |
| 12 |
exporter.run(fig) |
| 13 |
return renderer.output |
| 14 |
|
| 15 |
|
| 16 |
def _assert_output_equal(text1, text2): |
| 17 |
for line1, line2 in zip(text1.strip().split(), text2.strip().split()): |
| 18 |
assert line1 == line2 |
| 19 |
|
| 20 |
|
| 21 |
def test_lines(): |
| 22 |
fig, ax = plt.subplots() |
| 23 |
ax.plot(range(20), '-k') |
| 24 |
|
| 25 |
_assert_output_equal(fake_renderer_output(fig, FakeRenderer), |
| 26 |
""" |
| 27 |
opening figure |
| 28 |
opening axes |
| 29 |
draw path with 20 vertices |
| 30 |
closing axes |
| 31 |
closing figure |
| 32 |
""") |
| 33 |
|
| 34 |
_assert_output_equal(fake_renderer_output(fig, FullFakeRenderer), |
| 35 |
""" |
| 36 |
opening figure |
| 37 |
opening axes |
| 38 |
draw line with 20 points |
| 39 |
closing axes |
| 40 |
closing figure |
| 41 |
""") |
| 42 |
|
| 43 |
|
| 44 |
def test_markers(): |
| 45 |
fig, ax = plt.subplots() |
| 46 |
ax.plot(range(2), 'ok') |
| 47 |
|
| 48 |
_assert_output_equal(fake_renderer_output(fig, FakeRenderer), |
| 49 |
""" |
| 50 |
opening figure |
| 51 |
opening axes |
| 52 |
draw path with 25 vertices |
| 53 |
draw path with 25 vertices |
| 54 |
closing axes |
| 55 |
closing figure |
| 56 |
""") |
| 57 |
|
| 58 |
_assert_output_equal(fake_renderer_output(fig, FullFakeRenderer), |
| 59 |
""" |
| 60 |
opening figure |
| 61 |
opening axes |
| 62 |
draw 2 markers |
| 63 |
closing axes |
| 64 |
closing figure |
| 65 |
""") |
| 66 |
|
| 67 |
|
| 68 |
def test_path_collection(): |
| 69 |
fig, ax = plt.subplots() |
| 70 |
ax.scatter(range(3), range(3)) |
| 71 |
|
| 72 |
_assert_output_equal(fake_renderer_output(fig, FakeRenderer), |
| 73 |
""" |
| 74 |
opening figure |
| 75 |
opening axes |
| 76 |
draw path with 25 vertices |
| 77 |
draw path with 25 vertices |
| 78 |
draw path with 25 vertices |
| 79 |
closing axes |
| 80 |
closing figure |
| 81 |
""") |
| 82 |
|
| 83 |
_assert_output_equal(fake_renderer_output(fig, FullFakeRenderer), |
| 84 |
""" |
| 85 |
opening figure |
| 86 |
opening axes |
| 87 |
draw path collection with 3 offsets |
| 88 |
closing axes |
| 89 |
closing figure |
| 90 |
""") |
| 91 |
|
| 92 |
|
| 93 |
def test_text(): |
| 94 |
fig, ax = plt.subplots() |
| 95 |
ax.set_xlabel("my x label") |
| 96 |
ax.set_ylabel("my y label") |
| 97 |
ax.set_title("my title") |
| 98 |
ax.text(0.5, 0.5, "my text") |
| 99 |
|
| 100 |
_assert_output_equal(fake_renderer_output(fig, FakeRenderer), |
| 101 |
""" |
| 102 |
opening figure |
| 103 |
opening axes |
| 104 |
draw text 'my text' None |
| 105 |
draw text 'my x label' xlabel |
| 106 |
draw text 'my y label' ylabel |
| 107 |
draw text 'my title' title |
| 108 |
closing axes |
| 109 |
closing figure |
| 110 |
""") |
| 111 |
|
| 112 |
|
| 113 |
def test_path(): |
| 114 |
fig, ax = plt.subplots() |
| 115 |
ax.add_patch(plt.Circle((0, 0), 1)) |
| 116 |
ax.add_patch(plt.Rectangle((0, 0), 1, 2)) |
| 117 |
|
| 118 |
_assert_output_equal(fake_renderer_output(fig, FakeRenderer), |
| 119 |
""" |
| 120 |
opening figure |
| 121 |
opening axes |
| 122 |
draw path with 25 vertices |
| 123 |
draw path with 4 vertices |
| 124 |
closing axes |
| 125 |
closing figure |
| 126 |
""") |
| 127 |
|
| 128 |
def test_Figure(): |
| 129 |
""" if the fig is not associated with a canvas, FakeRenderer shall |
| 130 |
not fail. """ |
| 131 |
fig = plt.Figure() |
| 132 |
ax = fig.add_subplot(111) |
| 133 |
ax.add_patch(plt.Circle((0, 0), 1)) |
| 134 |
ax.add_patch(plt.Rectangle((0, 0), 1, 2)) |
| 135 |
|
| 136 |
_assert_output_equal(fake_renderer_output(fig, FakeRenderer), |
| 137 |
""" |
| 138 |
opening figure |
| 139 |
opening axes |
| 140 |
draw path with 25 vertices |
| 141 |
draw path with 4 vertices |
| 142 |
closing axes |
| 143 |
closing figure |
| 144 |
""") |
| 145 |
|
| 146 |
def test_multiaxes(): |
| 147 |
fig, ax = plt.subplots(2) |
| 148 |
ax[0].plot(range(4)) |
| 149 |
ax[1].plot(range(10)) |
| 150 |
|
| 151 |
_assert_output_equal(fake_renderer_output(fig, FakeRenderer), |
| 152 |
""" |
| 153 |
opening figure |
| 154 |
opening axes |
| 155 |
draw path with 4 vertices |
| 156 |
closing axes |
| 157 |
opening axes |
| 158 |
draw path with 10 vertices |
| 159 |
closing axes |
| 160 |
closing figure |
| 161 |
""") |
| 162 |
|
| 163 |
|
| 164 |
def test_image(): |
| 165 |
np.random.seed(0) |
| 166 |
fig, ax = plt.subplots() |
| 167 |
ax.imshow(np.random.random((10, 10)), |
| 168 |
cmap=plt.cm.jet, interpolation='nearest') |
| 169 |
|
| 170 |
_assert_output_equal(fake_renderer_output(fig, FakeRenderer), |
| 171 |
""" |
| 172 |
opening figure |
| 173 |
opening axes |
| 174 |
draw image of size 2848 |
| 175 |
closing axes |
| 176 |
closing figure |
| 177 |
""") |
| 178 |
|
| 179 |
|
| 180 |
def test_legend(): |
| 181 |
fig, ax = plt.subplots() |
| 182 |
ax.plot([1, 2, 3], label='label') |
| 183 |
ax.legend().set_visible(False) |
| 184 |
_assert_output_equal(fake_renderer_output(fig, FakeRenderer), |
| 185 |
""" |
| 186 |
opening figure |
| 187 |
opening axes |
| 188 |
draw path with 3 vertices |
| 189 |
opening legend |
| 190 |
closing legend |
| 191 |
closing axes |
| 192 |
closing figure |
| 193 |
""") |
| 194 |
|
| 195 |
|
| 196 |
def test_legend_dots(): |
| 197 |
fig, ax = plt.subplots() |
| 198 |
ax.plot([1, 2, 3], label='label') |
| 199 |
ax.plot([2, 2, 2], 'o', label='dots') |
| 200 |
ax.legend().set_visible(True) |
| 201 |
_assert_output_equal(fake_renderer_output(fig, FullFakeRenderer), |
| 202 |
""" |
| 203 |
opening figure |
| 204 |
opening axes |
| 205 |
draw line with 3 points |
| 206 |
draw 3 markers |
| 207 |
opening legend |
| 208 |
draw line with 2 points |
| 209 |
draw text 'label' None |
| 210 |
draw 2 markers |
| 211 |
draw text 'dots' None |
| 212 |
draw path with 5 vertices |
| 213 |
closing legend |
| 214 |
closing axes |
| 215 |
closing figure |
| 216 |
""") |
| 217 |
|
| 218 |
|
| 219 |
def test_blended(): |
| 220 |
fig, ax = plt.subplots() |
| 221 |
ax.axvline(0) |
| 222 |
assert_warns(UserWarning, fake_renderer_output, fig, FakeRenderer) |