6
$\begingroup$

I am trying to make a ListPlot with a bunch of points, each of which I want represented with a distinct image (e.g. *.png). I've tried the following code as an example (with img standing for a pasted image).

ListPlot[{Style[{1, 1}, img], Style[{-1, -1}, Red]}, 
 PlotRange -> {{-1.3, 1.3}, {-1.3, 1.3}}, Frame -> True, 
 LabelStyle -> Directive[Medium, Black], 
 PlotStyle -> {Automatic, AbsoluteThickness[1.5], 
   AbsoluteThickness[1.5]}, ImageSize -> 800]

enter image description here

As you can see, Style[{-1,-1}, Red] successfully makes the image Red, but using the same pattern with an img doesn't work.

NOTE: I'm aware of PlotMarkers -> {img1, img2, ...}, but the problem with this pattern is that it makes it associate which image should go with each data point. It'd be nice if I can find a way to do this where the data point and the image are kept tightly together in one data structure.


EDIT: Substituting for img the following works:

Labeled[{1, 1}, Import[
   "https://writings.stephenwolfram.com/data/uploads/2018/12/spikey-\
wolfram-logo.png"], Center]

The problem is that I'd like the logos to show up as bigger. Adjusting to

img = Import[
   "https://writings.stephenwolfram.com/data/uploads/2018/12/spikey-\
wolfram-logo.png", ImageSize->Large];

does not fix the issue. So my new question is: how (using this method), can I control the size of the image labels to be larger?

$\endgroup$
  • 1
    $\begingroup$ Does Labeled[{1, 1}, img, Center] (instead of Style[{1, 1}, img] ) give what you need? $\endgroup$ – kglr Dec 11 '19 at 19:46
  • $\begingroup$ kglr: That works, but raises a new issue. I modified my question above. $\endgroup$ – George Dec 11 '19 at 20:04
5
$\begingroup$

Update: A more convenient approach is to generate a list of Insets with the desired images using the fourth argument of Inset to control the sizes:

img = Import[
 "https://writings.stephenwolfram.com/data/uploads/2018/12/spikey-wolfram-logo.png"]

enter image description here

SeedRandom[77]
n = 20;
colors = RandomColor[n];
positions = RandomReal[50, {n, 2}];
image = RemoveBackground[img, {"Background", White}];
imglist = ColorReplace[image, Red -> #] & /@ colors;
sizes = RandomInteger[{10, 70}, n];

insets = MapThread[Inset[#, #2, Center, Offset[#3]] &, {imglist, positions, sizes}];

ListPlot

You can use insets as plot markers in ListPlot:

Show[ListPlot[List /@ positions, ImageSize -> Medium, 
  PlotMarkers -> (Graphics /@ insets),  
  Frame -> True, PlotRangeClipping -> False, AspectRatio -> 1,
  PlotRangePadding -> Scaled[.15]], ListLinePlot[positions]]

enter image description here

or as Prolog or Epilog option value

ListPlot[positions, Joined -> True, Prolog -> insets, 
 AspectRatio -> 1, Frame -> True, PlotRangeClipping -> False, 
 PlotRangePadding -> Scaled[.15], ImageSize -> Medium]

enter image description here

BubbleChart

or with BubbleChart as ChartElements:

BubbleChart[Flatten /@ Thread[{positions, sizes}], 
 ChartElements -> (Graphics /@ insets), 
 BubbleSizes -> {.1, .3}, 
 Epilog -> {ColorData[97][1], Line@positions}]

enter image description here

Graphics

or directly as graphics primitives in Graphics:

Graphics[{insets, Blue, Line @ positions}, ImageSize -> Medium, Frame -> True, 
 PlotRangePadding -> Scaled[.1]]

enter image description here

Original answer:

img = Import[
  "https://writings.stephenwolfram.com/data/uploads/2018/12/spikey-wolfram-logo.png"]

You can wrap img with Magnify:

ListPlot[{Labeled[{1, 1}, Magnify[ColorReplace[img, Red -> Blue], .3], Center], 
  Labeled[{1, 1}/2, Magnify[img, .15], Center], 
  Labeled[{-1, 0}/2, Magnify[ColorReplace[img, Red -> Orange], .45], Center], 
  {-1, -1}}, 
 PlotRange -> {{-1.3, 1.3}, {-1.3, 1.3}}, 
 Frame -> True, PlotStyle -> Green, PlotMarkers -> {Automatic, 24}, 
 LabelStyle -> Directive[Medium, Black], ImageSize -> Large]

enter image description here

Alternatively, you can wrap it with Style with the option ImageSizeMultipliers:

ListPlot[{Labeled[{1, 1}, Style[img, ImageSizeMultipliers -> {.2, 1/2}], Center], 
  Labeled[{1, 1}/2, Style[img, ImageSizeMultipliers -> {.1, 1/2}], Center], 
  Style[{-1, -1}, Red]}, 
 PlotRange -> {{-1.3, 1.3}, {-1.3, 1.3}}, Frame -> True, 
 LabelStyle -> Directive[Medium, Black], ImageSize -> 800]

enter image description here

| improve this answer | |
$\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.