Пример #1
0
def _get_wordcloud(img,
                   patch,
                   words,
                   word_to_frequency=None,
                   **wordcloud_kwargs):

    # get the boolean mask corresponding to each patch
    if isinstance(patch, Circle):
        # We need to solve two problems here:
        # 1) The path of Circle patches is always the unit circle.
        #    We need to apply the circle transform to the `path` attribute to get
        #    the correct path.
        # 2) The Circle object returned by matplotlib-venn is messed up.
        #    The transform is not doing what it should do.
        # path = transform.transform_path(old_path)

        # Create a new patch with the same parameters as the given Circle patch.
        dummy = Circle(patch.center, patch.radius)

        # Apply transform to get the correct path.
        transform = dummy.get_transform()
        unit_circle_path = dummy.get_path()
        path = transform.transform_path(unit_circle_path)
    else:
        path = patch.get_path()

    mask = path.contains_points(img.pixel_coordinates).reshape(
        (img.y_resolution, img.x_resolution))

    # make mask matplotlib-venn compatible
    mask = (~mask * 255).astype(np.uint8)  # black indicates mask position
    mask = np.flipud(mask)  # origin is in upper left

    # create wordcloud
    wc = WordCloud(mask=mask,
                   background_color=None,
                   mode="RGBA",
                   **wordcloud_kwargs)

    if not word_to_frequency:
        # create mapping word : int
        word_to_frequency = dict()
        for word in words:
            try:
                word_to_frequency[word] += 1
            except KeyError:
                word_to_frequency[word] = 1

    wc.generate_from_frequencies(
        {word: word_to_frequency[word]
         for word in words})

    return wc