15
$\begingroup$

WordCloud can arrange the words inside a shape.

data = EntityValue[CountryData[], {"Name", "Population"}];
WordCloud[data, Disk[], WordOrientation -> "Random", Frame -> True]

Mathematica graphics

As you can see from the plot range, the coordinates in the final word cloud don't seem to be related to the coordinates in the original shape specification. This is true regardless of whether the shape is specified as an image, graphics or region.

I would like to show the word cloud together the original shape. E.g. something like this (manually positioned):

enter image description here

I need to do this not for a disk/circle but any complex arbitrary shape. How can I achieve this? Is there any way to retrieve the mapping between the coordinates of the original shape and resulting word cloud?

Here's a random complex shape to test on: CountryData["Ethiopia", "Shape"].

$\endgroup$
  • $\begingroup$ I think there's an issue with using a shape beyond the matching of coordinates. Try a complicated country shape like Italy or Greece and you'll see that the words are not at all resembling the shape of the resp. country. This might have to do with the weights and the scaling of course but still... $\endgroup$ – gpap Jul 22 '15 at 11:17
  • $\begingroup$ @gpap But shouldn't they still fit inside of the shape, even though they can't follow the boundary very closely? $\endgroup$ – Szabolcs Jul 22 '15 at 11:57
  • $\begingroup$ @Szabolcs Making the word cloud having the shape is easy. To see whether we can work in the original shapes dimension, I have to inspect this further. $\endgroup$ – halirutan Jul 22 '15 at 12:30
18
$\begingroup$
shape = CountryData["Ethiopia", "Shape"]; 
mask =  ColorNegate@Binarize[shape, .99];

data = EntityValue[CountryData[], {"Name", "Population"}];
wc = WordCloud[data, mask, WordOrientation -> "Random", Frame -> False];

shapeRange = PlotRange /. AbsoluteOptions[shape, PlotRange];
cloudRange = PlotRange /. AbsoluteOptions[wc, PlotRange];

tr = FindGeometricTransform[cloudRange\[Transpose], shapeRange\[Transpose]][[2]];

Show[
 wc,
 Graphics[
  {
   EdgeForm[Black], FaceForm[],
   GeometricTransformation[shape[[1, 3]], tr]
   }
  ]
 ]

Mathematica graphics

Since gpap reported problems with Italy here's Italy. I increased the image size of the bitmapped mask a bit to catch more of the complex coastal shape using:

shape = CountryData["Italy", "Shape"];
mask = ColorNegate@Binarize[Image[shape, ImageSize -> 1000], .99];

Mathematica graphics

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ A wholehearted +1 from me. Syria seems to like being engulfed by China's "C". $\endgroup$ – gpap Jul 22 '15 at 12:51
  • $\begingroup$ Thanks. I gather you used the shape directly in the word cloud function? At least that's what I did initially. $\endgroup$ – Sjoerd C. de Vries Jul 22 '15 at 12:56
  • 1
    $\begingroup$ Yes, I did exactly that and gave up :) $\endgroup$ – gpap Jul 22 '15 at 13:02

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.