Label positioning within regions :: Visual test


In [1]:
from pylab import *
from matplotlib_venn import _region
from tests.utils import point_in_patch
import warnings
warnings.simplefilter("error") # Have warnings raise exceptions in this test

def show_region(r, c='r', xlim=(-0.5, 1.5), ylim=(-0.5, 1.5)):
    gca().set_aspect('equal')
    gca().set_xlim(*xlim)
    gca().set_ylim(*ylim)
    p = r.make_patch()
    if p is not None:
        p.set_color(c)
        p.set_ec('none')
        p.set_alpha(0.5)
        gca().add_patch(p)
    lblpos = r.label_position()
    scatter(lblpos[0], lblpos[1])
    assert point_in_patch(p, lblpos)

In [2]:
# For circles, the label is positioned in the center
vcr = _region.VennCircleRegion((0.5, 0.5), 0.5)
show_region(vcr)



In [3]:
# For two-arc regions, the label is positioned in the middle between the two arcs
twoarc1, twoarc2 = vcr.subtract_and_intersect_circle((1.0, 0.5), 0.5)
subplot(121); show_region(twoarc1); subplot(122); show_region(twoarc2)



In [4]:
# For three-arc regions, the label is in the middle of the three arcs
threearc1, threearc2 = twoarc1.subtract_and_intersect_circle((1, 0), 0.5)
subplot(121); show_region(threearc1); subplot(122); show_region(threearc2)



In [5]:
# For multi-piece regions, the label will be positioned in the largest piece
# For four-arcs, the mean of four edges is simply used
multipiece, fourarc = twoarc1.subtract_and_intersect_circle((0.25, 0.25), 0.35)
subplot(121); show_region(multipiece); subplot(122); show_region(fourarc)



In [6]:
# An example of bad label positioning
bad_piece, _ = twoarc1.subtract_and_intersect_circle((0.25, 0.25), 0.25)
show_region(bad_piece);