Skip to content

Commit 7841abc

Browse files
authored
explicitly test draw required (#97)
* explicitly test draw required * remove comment
1 parent ab03f4a commit 7841abc

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

mplotutils/tests/test_colorbar.py

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ def test_colorbar_vertical_aspect():
152152
cbar = colorbar_one_ax_vertical(aspect=5, pad=0)
153153

154154
expected = [0.8, 0, 0.2, 1.0]
155-
plt.gcf().canvas.draw()
156155

157156
assert_position(cbar, expected)
158157
assert_aspect(cbar, 5)
@@ -252,8 +251,6 @@ def test_colorbar_horizontal_aspect():
252251
# test pad=0, aspect=5
253252
cbar = colorbar_one_ax_horizontal(aspect=20, pad=0)
254253

255-
# f.canvas.draw()
256-
257254
expected = [0.0, 0.175, 1.0, 0.025]
258255
assert_position(cbar, expected)
259256
assert_aspect(cbar, 1 / 20)
@@ -430,6 +427,57 @@ def test_colorbar_horizontal_two_axes():
430427
assert_position(cbar, expected)
431428

432429

430+
def create_fig_aspect(aspect, orientation):
431+
432+
f = plt.gcf()
433+
434+
ax = f.subplots()
435+
436+
h = ax.contourf([[0, 1], [0, 1]], levels=[0, 1])
437+
438+
cbar = mpu.colorbar(h, ax, orientation=orientation)
439+
440+
ax.set_aspect(aspect)
441+
442+
return ax, cbar
443+
444+
445+
def test_colorbar_vertical_draw_required():
446+
447+
with figure_context() as f:
448+
ax, cbar = create_fig_aspect(aspect=0.5, orientation="vertical")
449+
450+
pos = cbar.ax.get_position()
451+
result = [pos.y0, pos.height]
452+
453+
pos = ax.get_position()
454+
expected = [pos.y0, pos.height]
455+
456+
assert result != expected
457+
458+
f.canvas.draw()
459+
result = [pos.y0, pos.height]
460+
assert result == expected
461+
462+
463+
def test_colorbar_horizontal_draw_required():
464+
465+
with figure_context() as f:
466+
ax, cbar = create_fig_aspect(aspect=2, orientation="horizontal")
467+
468+
pos = cbar.ax.get_position()
469+
result = [pos.x0, pos.width]
470+
471+
pos = ax.get_position()
472+
expected = [pos.x0, pos.width]
473+
474+
assert result != expected
475+
476+
f.canvas.draw()
477+
result = [pos.x0, pos.width]
478+
assert result == expected
479+
480+
433481
def test_colorbar_errors():
434482
with subplots_context() as (f, ax):
435483
h = ax.pcolormesh([[0, 1]])

0 commit comments

Comments
 (0)