4
$\begingroup$

I want to plot some {x,y} data as points. For each x and y coordinate of each point I got a range I want to plot as well. The range should look like whiskers/fences of a BoxWhiskerChart but both vertically and horizontal.

I tried some options of BoxWhiskerChart, but I'm not sure if that is the right kind of plot to begin with. Is it possible to plot X- and Y-whiskers? How can I hide or replace the box? Here is a solution to hide the whiskers which is basically the opposite of what I want.

To get a better idea how the plot should look like I added an ungly version I created in Excel. (I couldn't turn the fences for the horizontal whiskers.)

Plot I want

Example data used:

Points

point1={1,2}
point2={2,3}

Range

point1xrange={0.5,1.5}
point1yrange={1.5,2.5}
point2xrange={1,3}
point2yrange={2,4}

To specify: all points lie within their range, but not always exactly in the middle.

Any suggestions?

$\endgroup$
4
  • 5
    $\begingroup$ Have you see the ErrorBarPlots and ErrorBarFunction? $\endgroup$ Aug 27, 2018 at 9:03
  • 2
    $\begingroup$ Not yet, I wasn't familiar with Needs and the possibility of loading packages. ErrorListPlot does the trick. Since I'm not familiar with StackExchange either: should I answer my own question? Or edit the answer into the question? $\endgroup$
    – phoenix-bs
    Aug 27, 2018 at 9:50
  • 1
    $\begingroup$ Well, you may. Or you may leave it like it is, since I believe this is an off-topic, since the answer exists in the documentation. For this reason I propose to close this question. But important here is that you got a satisfactory solution. $\endgroup$ Aug 27, 2018 at 9:55
  • 2
    $\begingroup$ @phoenix-bs Self-answers are encouraged! Do go ahead and enter a self-answer to your question. $\endgroup$
    – MarcoB
    Aug 27, 2018 at 13:27

3 Answers 3

3
$\begingroup$

An alternative to ErrorListPlot is BubbleChart with a custom ChartElementFunction:

ClearAll[errorBubble]
errorBubble[ps_: 15, th_: 5, ws_: 5] := Module[{xmean = Mean@#[[1]], ymean = Mean@#[[2]], 
     xerror = #3[[1, 1]], yerror = #3[[1, 2]], 
     color = Charting`ChartStyleInformation["Color"]}, 
  {Lighter @ color, AbsoluteThickness[th], 
   Line[{{xmean - xerror[[1]], ymean}, {xmean + xerror[[2]],  ymean}}], 
   Line[{{xmean, ymean - yerror[[1]]}, {xmean,  ymean + yerror[[2]]}}], 
   Line[{Offset[{0, -ws}, {xmean - xerror[[1]], ymean}], 
      Offset[{0, ws}, {xmean - xerror[[1]], ymean}]}],
   Line[{Offset[{0, -ws}, {xmean + xerror[[2]], ymean}], 
      Offset[{0, ws}, {xmean + xerror[[2]], ymean}]}] ,
   Line[{Offset[{-ws, 0}, {xmean, ymean - yerror[[1]]}],
      Offset[{ws, 0},  {xmean,  ymean - yerror[[1]]}]}],
   Line[{Offset[{-ws,0}, {xmean, ymean + yerror[[2]]}], 
      Offset[{ws, 0},  {xmean,  ymean + yerror[[2]]}]}],
   color, AbsolutePointSize[ps], Point[{xmean, ymean}]}] &;

Examples:

SeedRandom[1]
data = RandomReal[20, {10, 2}];
errors = RandomReal[{1, 3}, {10, 2, 2}];
bcdata = Append[#, 1] -> #2 & @@@ Transpose[{data, errors}];

BubbleChart[bcdata, ChartStyle -> 97, ChartElementFunction -> errorBubble[]]

enter image description here

BubbleChart[bcdata, ChartStyle -> 97, ChartElementFunction -> errorBubble[20, 3, 10]] 

enter image description here

$\endgroup$
2
$\begingroup$

In versions 12.0+, you can use Around + ListPlot as follows:

ClearAll[toAround]
toAround = MapThread[MapThread[Around]@*List, {##}] &;

points = {point1, point2};
ranges = {{point1xrange, point1yrange}, {point2xrange, point2yrange}};

toAround[points, ranges]

enter image description here

ListPlot[toAround[points, ranges], PlotRange -> All]

enter image description here

Wrap each data set with List to have points styled individually:

ListPlot[List /@ toAround[points, ranges], PlotRange -> All] 

enter image description here

You can use the option IntervalMarkersStyle (also new in version 12.0) to style the whiskers and fences:

ListPlot[List /@ toAround[points, ranges], 
  PlotRange -> All,
  BaseStyle -> AbsolutePointSize[10],
  IntervalMarkersStyle ->
    <|"FenceStyle" ->Directive[Thick, Red], 
      "FenceWidth"->.2,
      "WhiskerStyle" -> Thick|>]

enter image description here

$\endgroup$
1
$\begingroup$

Alexei Boulbitch suggested the ErrorBar Plotting Package. With that, I came up with the following solution.

A normal ListPlot for the data points:

dataPointsPlot = ListPlot[{dataA, dataB, dataC, dataD, dataE},
PlotMarkers -> {Automatic, 20}];

The ErrorListPlot from the package:

Needs["ErrorBarPlots`"]
errorListPlot=ErrorListPlot[{rangeData}, PlotStyle->AbsolutePointSize[0]}];

Then I use Show[] to combine both plots and configure them as I want.

final-plot

dataA = {{0.0154, 0.0171}, {0.017, 0.0215}, {0.0271, 0.0337}};
dataB = {{0.0321, 0.0339}, {0.0337, 0.0368}, {0.0398, 0.0466}};
dataC = {{0.0884, 0.1001}, {0.0826, 0.0948}, {0.0859, 0.0739}};
dataD = {{0.0842, 0.0862}, {0.1235, 0.1262}, {0.1199, 
    0.1303}, {0.1352, 0.1294}};
dataE = {{0.1363, 0.1406}, {0.1438, 0.131}, {0.1501, 0.1474}};

rangeData = {
{{0.0154, 0.0171}, 
    ErrorBar[{-0.0015, 0.0031}, {-0.0019, 0.0025}]}, {{0.017, 0.0215},
     ErrorBar[{-0.0017, 0.0034}, {-0.0024, 0.0031}]}, {{0.0271, 
     0.0337}, 
    ErrorBar[{-0.0027, 0.0054}, {-0.0038, 0.0049}]}, {{0.0321, 
     0.0339}, 
    ErrorBar[{-0.0032, 0.0064}, {-0.0038, 0.0049}]}, {{0.0337, 
     0.0368}, 
    ErrorBar[{-0.0034, 0.0067}, {-0.0041, 0.0053}]}, {{0.0398, 
     0.0466}, 
    ErrorBar[{-0.004, 0.008}, {-0.0052, 0.0067}]}, {{0.0884, 0.1001}, 
    ErrorBar[{-0.0088, 0.0177}, {-0.0112, 0.0145}]}, {{0.0826, 
     0.0948}, 
    ErrorBar[{-0.0083, 0.0165}, {-0.0106, 0.0137}]}, {{0.0859, 
     0.0739}, 
    ErrorBar[{-0.00860000000000001, 0.0172}, {-0.00829999999999999, 
      0.0107}]}, {{0.0842, 0.0862}, 
    ErrorBar[{-0.00839999999999999, 0.0168}, {-0.0097, 
      0.0125}]}, {{0.1235, 0.1262}, 
    ErrorBar[{-0.0123, 0.0247}, {-0.0142, 0.0183}]}, {{0.1199, 
     0.1303}, 
    ErrorBar[{-0.012, 0.024}, {-0.0146, 0.0189}]}, {{0.1352, 0.1294}, 
    ErrorBar[{-0.0135, 0.027}, {-0.0145, 0.0187}]}, {{0.1363, 0.1406},
     ErrorBar[{-0.0136, 0.0273}, {-0.0158, 0.0204}]}, {{0.1438, 
     0.131}, ErrorBar[{-0.0144, 0.0288}, {-0.0147, 0.019}]}, {{0.1501,
      0.1474}, ErrorBar[{-0.015, 0.03}, {-0.0165, 0.0213}]}
   };
$\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.