4
$\begingroup$

The documentation shows how Mathematica can attach behavior to words in WordClouds, but I couldn't find anything on retrieving the list of bounding box coordinates.
Is it available to the user?

__
I'm on version 11.2

$\endgroup$
8
$\begingroup$
words = {"apple", "banana", "grapes", "orange", "grapes", "apple", 
  "grapes", "grapes", "strawberry", "strawberry", "grapes", "grapes", 
  "banana", "apple", "orange", "grapes", "strawberry", "banana", 
  "apple", "grapes"};

WordCloud[words]

enter image description here

You can get the bounding boxes using the option "BoundingBoxes" -> True:

wc = WordCloud[words, "BoundingBoxes" -> True, ImagePadding -> 10]

enter image description here

rectangles = Cases[wc, _Rectangle, All]

{Rectangle[{-107,-(81/2)},{107,81/2}],
Rectangle[{-38.5,15.},{38.5,52.}],
Rectangle[{6.5,-48.},{105.5,-21.}],
Rectangle[{-70.,-43.},{-2.,-24.}],
Rectangle[{-26.5,-53.},{10.5,-38.}]}

Show[WordCloud[words, ImagePadding -> 10], 
 Graphics[{EdgeForm[Gray], Opacity[.2, RandomColor[]], #} & /@ rectangles]]

enter image description here

WordCloud[words, "BoundingBoxes" -> True, 
  WordOrientation -> "Random", ImagePadding -> 15] /.
    r_Rectangle :> { Opacity[.3, RandomColor[]], r}

enter image description here

$Version

"11.3.0 for Microsoft Windows (64-bit) (March 7, 2018)"

This also works in version 12.0.0 (Wolfram Cloud).

Note: I came across this option using

Image`CompositionOperationsDump`WordCloudHiddenOptions // Short

{BitmapOutput -> False, BoundingBoxes-> False, Caching -> False,<<16>>, WordSelectionFunction -> Automatic, WordSpacings -> 1}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.