5
$\begingroup$

I'd like to label a pie chart with images, ideally resized to fit inside the sectors. The problem is I can't seem to control the size of the imported images. Here's an MWE with no attempt at resizing, pie chart image size of 1000;

a = Import["strawman.png"];
 a2 = RemoveBackground[a];
 b = Import["cherry.png"];
b2 = RemoveBackground[b];
c = Import["burden.png"];
c2 = RemoveBackground[c];
d = Import["gradhat.png"];
d2 = RemoveBackground[d];
PieChart[{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, SectorOrigin -> {Automatic, 2}, 
 ChartLabels -> {a2, b2, "blank", c2, "blank2", d2}, ImageSize -> 1000]

This results in an image like this; enter image description here

I initially thought I could just resize these images, but the problem is that simply reduces the quality and not the size- it seems ImageSize is overwriting everything. For example, if I try and make the scarecrow resized to 50 pixels with

  a2 = ImageResize[a2,50]

all this does is severely pix-elate the scarecrow element as below, with no impact on size: enter image description here

Is there a clever way around this? I've tried Inset and Epilog and had no obvious joy with them either.

$\endgroup$
1
  • $\begingroup$ try Magnify, e.g., Magnify[a2,.2]? $\endgroup$
    – kglr
    Jun 4, 2019 at 17:09

2 Answers 2

4
$\begingroup$

You could use Pane instead of ImageResize:. For example:

a1 = Pane[ExampleData[{"TestImage","Lena"}], 30];
a2 = Pane[ExampleData[{"TestImage", "Aerial"}], 40];

PieChart[
    ConstantArray[1, 15],
    SectorOrigin->{Automatic,2},
    ChartLabels -> {a1,"blank",a2,"blank2"}
]

enter image description here

$\endgroup$
1
  • $\begingroup$ Absolutely perfect, cheers! $\endgroup$
    – DRG
    Jun 4, 2019 at 17:45
4
$\begingroup$

As an alternative to using images as labels we can use them as Texture for sectors with a custom ChartElementFunction:

ClearAll[cedF]
cedF[opt_: "Fill"] := Module[{cedf = If[Head[#3[[1]]] === Image, 
  System`BarFunctionDump`TextureRadius[#, #2, "Placement" -> opt] /. 
     _Texture :> Texture[#3[[1]]]&,
  ChartElementDataFunction["Sector"][##] &]}, cedf[##]] &

Examples:

textures = Rasterize @ Thumbnail[#, Small] & /@ 
  ExampleData[{"TestImage", #}] & /@ {"Lena", "Apples", "Mandrill", "Flower"};

Attach textures to input data as metadata:

data = Thread[ConstantArray[1, 6] -> Join[textures, {Red, Blue} ]];

With default value of the argument ("Fill") a picture is cropped to the shape of the associated sector:

PieChart[data, SectorOrigin -> {Automatic, 1}, ChartElementFunction -> cedF[]]

enter image description here

With ChartElementFunction -> cedF["StretchToFill"]:

enter image description here

and with ChartElementFunction -> cedF["Tile"]:

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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