Suppose I have the following data with co-ordinates: {{1,190},{1,200},{1,210},{1,180},{1.5,175},{1.5,200},{1.5,190},{2,100},{2,150}}

How do I make a listplot of this data set, by taking the mean of the y-values for a particular x-value and then plotting an error bar. Since this is a small data set, I can evaluate mean and error easily, but what of there is a large data set, is there some way to automate the process?


1 Answer 1

data = {{1, 190}, {1, 200}, {1, 210}, {1, 180}, {1.5, 175}, {1.5, 
    200}, {1.5, 190}, {2, 100}, {2, 150}} ;

data2 = KeyValueMap[List]@ GroupBy[data, First -> Last, Around] (*thanks: Lukas Lang*)


enter image description here

 PlotStyle -> Directive[Blue, PointSize[Large]],
 PlotRangePadding -> Scaled[.15], 
 IntervalMarkersStyle -> <|"FenceWidth" -> .1, 
   "FenceStyle" -> Directive[Thick, Red], 
      "WhiskerStyle" -> Directive[Green, CapForm["Butt"], AbsoluteThickness[20]]|>] 

enter image description here

ListPlot[data2, Joined -> True, 
 IntervalMarkers -> "Bands", IntervalMarkersStyle -> Green, PlotRange -> {0, 220}] 

enter image description here

  • $\begingroup$ Thank you, can you tell e what attribute should I change to change the color, boldness etc. of the error bar? $\endgroup$ Oct 7, 2020 at 20:39
  • $\begingroup$ @Indeterminate, please see the update. $\endgroup$
    – kglr
    Oct 7, 2020 at 21:59
  • $\begingroup$ You should be able to just use Around[#], and Around will automatically compute the mean and standard deviation (see the fourth usage case in the docs) $\endgroup$
    – Lukas Lang
    Oct 8, 2020 at 7:43
  • $\begingroup$ Thank you @LukasLang; excellent point. Updated with your suggestion. $\endgroup$
    – kglr
    Oct 8, 2020 at 7:52
  • $\begingroup$ @kglr the solution with the 'whiskers' does not work on logarithmic axes. can it be adapted? $\endgroup$
    – Luigi
    Oct 8, 2020 at 9:32

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.