Creating a graph with error bars

3 views (last 30 days)
Ran Kagan
Ran Kagan on 25 Jun 2022
Commented: Dyuman Joshi on 25 Jun 2022
Hi,
For this given data arrays:
three_o=[0.933, 0.92];
error_three_o=[0.0019,0.0021];
three_r=[0.926,0.909];
error_three_r=[0.0017,0.0018];
four_o=[0.876,0.934];
error_four_o=[0.0017, 0.0012];
four_r=[0.875,0.903];
error_four_r=[0.0016,0.0015];
I've been trying to use errorbar function to create a readable error whiskers, like so:
This is the code I wrote, which kinda did the trick, although I hav some issues with the errorbars...:
scatter(3, [three_o],'red','*')
hold on
scatter(3, [three_r],'blue','+')
scatter(4, [four_o],'red','*')
scatter(4, [four_r],'blue','+')
legend({'Oven','', 'Regular'})
errorbar([3 3], [three_o],error_three_o)
errorbar([3 3], [three_r],error_three_r)
I'm getting very mixed-up error bars, with weird colors (I want them to be simply black), weird proportions (the errors themselves are really low and yet the vertical lines seem very long, representing a large error and overlapping with one another. The errors are also somehow added to the legend of the graph which I do not want to (I wrote the legend function before the errorbar function so I expected the errors not to be included).
Any thoughts?
Thanks!

Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 25 Jun 2022
Edited: Bjorn Gustavsson on 25 Jun 2022
You can use the errorbar function for plots like this. Check the help and documentation for that function. If that function doesn't entirely satisfy you there are a couple of alternatives on the file exchange. For example: al_goodplot-boxblot-violin-plot, alternative-box-plot, boxplot2 and notboxplot.
HTH
  3 Comments
Bjorn Gustavsson
Bjorn Gustavsson on 25 Jun 2022
The errorbar function by default connect the central points with solid lines. In your plot you get that line vertical since you have the same x-coordinate for both. Try this:
ph1 = scatter(3, [three_o],'red','*');
hold on
ph2 = scatter(3, [three_r],'blue','+');
ph3 = scatter(4, [four_o],'red','*');
ph4 = scatter(4, [four_r],'blue','+');
errorbar([3 3], [three_o],error_three_o,'.')
errorbar([3 3], [three_r],error_three_r,'.')
legend([ph1(1),ph2(1)],{'Oven','', 'Regular'})
"Always" make a habit of taking the plot-handles out from the plotting-calls that you want to include in the legend - that way you have explicit control over what goes into the legend.
Dyuman Joshi
Dyuman Joshi on 25 Jun 2022
I was not aware of errorbar. TIL, Thanks!

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!