6
$\begingroup$

So I'm trying to make a plot using error bars, but instead of errorbars, I'd like to use an image. Let's say we've got the following plot: enter image description here Instead of these error bars, I want to use the image of a tie fighter (turned on its side) instead. So I've imported the image and turned it on its side. Now the problem I'm facing is how to scale and position it within the plot to function as error bars. Do any of you have any ideas on how to do this?

Thanks!

EDIT: So I still can't figure it out, the problem probably lies in that I'm just using uncertainties in y. My code is as follows:

Import["http://vignette2.wikia.nocookie.net/swmerchandise/images/4/45/\
Imperial_TIE_Fighter_(27644)_P.gif"];

Needs["ErrorBarPlots`"]
tijd = {-3, -2, 0, 1, 2}; (*x axis*)
positie = {27.9, 10.2, 1.8, 9.5, 23.3}; (*y axis*)
onzekerheid = {0.5, 0.9, 0.8, 0.7, 0.9}; (*Uncertainty*)

errorplot = {{tijd[[#]], positie[[#]]}, 
ErrorBar[onzekerheid[[#]]]} & /@ Range[1, Length[tijd]];
position = Transpose[{tijd, positie}];

errorbar[pos_, err_] :=
  With[
   {s = 0.05},
   With[
    {
     corners =
      {{pos[[#]] - s, pos[[#]] - err[[#]]},
         {pos[[#]] + s, pos[[#]] - err[[#]]},
         {pos[[#]] + s, pos[[#]] + err[[#]]},
         {pos[[#]] - s, pos[[#]] + err[[#]]}} & /@ 
       Range[1, Length[pos]]
 },
{
 Texture[ImageRotate[fighter, \[Pi]/2]],
 Polygon[
  corners,
  VertexTextureCoordinates -> {{0, 1}, {1, 1}, {1, 0}, {0, 0}}
  ]
 }
]
];
$\endgroup$
3
  • $\begingroup$ Have you tried ErrorBarFunction? $\endgroup$
    – lowriniak
    Dec 21, 2016 at 11:25
  • $\begingroup$ @lowriniak ErrorBarFunction is the correct way to approach this, but the examples are rather scarce. $\endgroup$
    – Feyre
    Dec 21, 2016 at 11:42
  • $\begingroup$ inleveropgave week 6? $\endgroup$ Dec 21, 2016 at 17:32

2 Answers 2

7
$\begingroup$

Import your image, you didn't provide one, so I chose this one:

im = Import["https://cdn2.iconfinder.com/data/icons/star-wars-5/24/Tie-\
Fighter-128.png"];

This method works only with 2d errorbars:

ErrorListPlot[{{{1, 1}, ErrorBar[0.2, 0.3]}, {{2, 2}, 
   ErrorBar[0.2, 0.3]}, {{3, 4}, ErrorBar[0.2, 0.5]}, {{4, 8}, 
   ErrorBar[1, 2]}},
 ErrorBarFunction -> 
  Function[{coords, errs}, {Opacity[0.5], Texture[im], 
    Polygon[{coords + {errs[[1, 1]], errs[[2, 1]]}, 
      coords + {errs[[1, 2]], errs[[2, 1]]}, 
      coords + {errs[[1, 2]], errs[[2, 2]]}, 
      coords + {errs[[1, 1]], errs[[2, 2]]}}, 
     VertexTextureCoordinates -> {{0, 1}, {1, 1}, {1, 0}, {0, 0}}]}]]

enter image description here

$\endgroup$
2
  • $\begingroup$ I haven't yet been able to figure it out, can you take a look at the code I posted in the top comment? $\endgroup$ Dec 21, 2016 at 13:16
  • $\begingroup$ @MitchellFaas You can use a constant error for your y-axis, or scale it with the x-error. $\endgroup$
    – Feyre
    Dec 23, 2016 at 12:03
6
$\begingroup$

I used a slightly different ErrorBarFunction to Feyre so I thought I would share my solution too. This should also account for x errors:

errorbar[coords_, errs : ErrorBar[{xn_, xp_}, {yn_, yp_}]] := With[
  {s = 0.3},
  With[
   {
      pts = {
        coords + {xn - s, yn},
        coords + {xn + s, yn},
       coords + {xp + s, yp},
       coords + {xp - s, yp}
      }
    },
    {
      Texture[ImageRotate[img, \[Pi]/2]],
      Polygon[
        pts,
        VertexTextureCoordinates -> {{0, 1}, {1, 1}, {1, 0}, {0, 0}}
      ]
    }
  ]
];

ErrorListPlot[
  Table[{i, RandomReal[0.5]}, {i, 10}], 
  ErrorBarFunction -> errorbar, 
  PlotStyle -> PointSize[Tiny]
]

tiefighters

Variables:

s : x scaling for better image size

xn, yn : negative error in x and y

xp, yp : positive error in x and y

img : your chosen TIE fighter image (I couldn't find a decent one labeled for re-use)

$\endgroup$
1
  • $\begingroup$ I like your style! Still trying to figure out exactly how to incorperate it though :P I'll leave a comment once I finished that. $\endgroup$ Dec 21, 2016 at 12:11

Your Answer

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

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