Clear Filters
Clear Filters

Error envelope/shaded bar on stair plot

4 views (last 30 days)
Philipp
Philipp on 18 Apr 2023
Answered: Abhinaya Kennedy on 30 Jul 2024 at 9:31
Hello together,
I am quite new to Matlab and tried to plot a stairplot with a shaded, symmetric error envelope. The x,y data and the error come in 20x1 double array and are extracted from bigger ones in a loop. Now I wanted to draw shaded bars above and under each bar with fill, however it did not give me the desired result. Here is my code:
fill([x;flipud(x)],[y-dy;flipud(y+dy)], color_err, 'linestyle', 'none','HandleVisibility','off');
I also played around with leaving out or changing the
flipud
function but this code is always drawing triangles around the bars of the stairplot. the stairplot itself looks like the following and should have rectangular shaded regions around single bars that are assigned with an error.
Does someone have an idea how to do this? Would be very grateful since I am stuck with this problem for quite a while.
Kind regards,
Philipp

Answers (1)

Abhinaya Kennedy
Abhinaya Kennedy on 30 Jul 2024 at 9:31
As far as I can see, you want to create a shaded error envelope around a stair plot. You need to ensure that the coordinates for the fill are correctly specified to form the rectangles around each step.
Create the stair plot:
figure;
stairs(x, y, 'b', 'LineWidth', 2);
hold on;
Fill Coordinates:
  • x_fill: This array contains the x-coordinates for the vertices of the rectangles. Each column corresponds to one rectangle.
  • y_fill_upper and y_fill_lower: These arrays contain the y-coordinates for the upper and lower bounds of the error envelope, respectively.
x_fill = [x(1:end-1)'; x(2:end)'; x(2:end)'; x(1:end-1)'];
y_fill_upper = [y(1:end-1)'+dy(1:end-1)'; y(1:end-1)'+dy(1:end-1)'; y(2:end)'+dy(2:end)'; y(2:end)'+dy(2:end)'];
y_fill_lower = [y(1:end-1)'-dy(1:end-1)'; y(1:end-1)'-dy(1:end-1)'; y(2:end)'-dy(2:end)'; y(2:end)'-dy(2:end)'];
Plot the Shaded Error Envelope:
for i = 1:length(x)-1
fill(x_fill(:,i), y_fill_upper(:,i), color_err, 'LineStyle', 'none', 'HandleVisibility', 'off');
fill(x_fill(:,i), y_fill_lower(:,i), color_err, 'LineStyle', 'none', 'HandleVisibility', 'off');
end

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!