5
$\begingroup$

I'm trying to use an image as a ChartLabel and I'm getting strange results.

Here is a bar chart, with labels, that looks ok:

chart ok

But when I try to replace the "A" label with an image, the output is confusing:

chart busted

Specifically, the image overlaps the plot and is scaled weirdly.

I'd like it to be small, and centered, as the "A" label is in the image above.

What's the right way to use an image as a ChartLabel?

$\endgroup$
8
$\begingroup$

Update: The option LabelingSize provides a convenient way to size the images.

images = ExampleData[{"TestImage", #}] & /@ {"Lena", "Elaine", "Mandrill"};

is = 60;
bs = .2; 

BarChart[Thread[Labeled[{1, 2, 3}, images, Below]], 
 BarSpacing -> bs, LabelingSize -> is (1 - bs), ImageSize -> 1 -> is]

enter image description here

With is = 120 and bs = .3 we get:

enter image description here

Original answer:

images = ExampleData[{"TestImage", #}] & /@ {"Lena", "Elaine", "Mandrill"};

BarChart[{1, 2, 3}, ChartLabels -> 
 Placed[Thumbnail[#, Tiny] & /@ images, Axis, Panel[#, FrameMargins -> 0] &]]

enter image description here

Alternatively, you could use Magnify instead of Thumbnail:

BarChart[{1, 2, 3}, 
 ChartLabels -> Placed[images, Axis, Framed[Magnify[#, .3], FrameStyle -> None] &]]

enter image description here

Few more alternatives that produce similar pictures:

BarChart[{1, 2, 3}, ChartLabels -> (Framed[Magnify[#, .3], FrameStyle->None] & /@ images)]
BarChart[{1, 2, 3}, ChartLabels -> Placed[Pane[Magnify[#, .1]] & /@ images, Axis]]
BarChart[{1, 2, 3}, ChartLabels->Placed[Framed[Magnify[#, .3], 
  FrameStyle -> None]& /@ images, Axis]]
$\endgroup$
5
  • 2
    $\begingroup$ I just hope that the vertical scale does not represent human beauty. $\endgroup$ – John Morganthau Jan 4 '15 at 21:06
  • $\begingroup$ Thanks, @kguler. I didn't know about Placed or Thumbnail or Panel. There is much to learn. $\endgroup$ – Harold Jan 4 '15 at 21:09
  • $\begingroup$ Harold, my pleasure. $\endgroup$ – kglr Jan 4 '15 at 21:29
  • $\begingroup$ @JohnMorganthau, let's hope so indeed:) $\endgroup$ – kglr Jan 4 '15 at 21:35
  • $\begingroup$ Was doing this again today and found that Pane worked best. Thanks again for this answer, and good luck to future searchers! $\endgroup$ – Harold Mar 29 '15 at 19:05
1
$\begingroup$

How about using ChartElements instead of ChartLabels.

images = ExampleData[{"TestImage", #}] & /@ {"Lena", "Mandrill"};

BarChart[{{1, 2, 3}, {4, 5, 6}}, ChartElements -> {images, None}]

enter image description here

$\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.