TransWikia.com

How do I use an image as a `ChartLabel`?

Mathematica Asked by Harold on February 3, 2021

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?

2 Answers

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

Answered by MinHsuan Peng on February 3, 2021

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]]

Answered by kglr on February 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP