Wrong 'patch' area when plotting

2 次查看(过去 30 天)
Hello Community,
I am using the 'patch' function to plot 4 areas to aid with the grouping of some data when it is plotted. I have tried to make the code so that it will work with any size of plot, hence defining the x-limits (x_lim). The problem is, with the code I have written, the patches dont align as I would like:
% Define limits of axis
x_lim = get(gca,'xlim');
y_lim = get(gca,'ylim');
% Colour in the patches
patch([0 0 (x_lim(1,2)/4) (x_lim(1,2)/4)],[y_lim(1) y_lim(2) y_lim(2) y_lim(1) ],[1 0.5 0.2],'FaceAlpha',0.2)
patch([(x_lim(1,2)/4) (x_lim(1,2)/4) ((x_lim(1,2)/4)*2) ((x_lim(1,2)/4)*2) ],[y_lim(1) y_lim(2) y_lim(2) y_lim(1) ],[1 1 0.2],'FaceAlpha',0.2)
patch([((x_lim(1,2)/4)*2) ((x_lim(1,2)/4)*2) ((x_lim(1,2)/4)*3) ((x_lim(1,2)/4)*3)],[y_lim(1) y_lim(2) y_lim(2) y_lim(1) ],[0.5 1 0.7],'FaceAlpha',0.2)
patch([((x_lim(1,2)/4)*3) ((x_lim(1,2)/4)*3) x_lim(2) x_lim(2)],[y_lim(1) y_lim(2) y_lim(2) y_lim(1) ],[0.4 0.3 0.8],'FaceAlpha',0.2)
and they produce this plot:
What I would like to happen is that the patch groups around the first 4 box plots (group A), then the next patch covers the next 4 boxplots (group B) and so on.
I have tried various combinations of my code but haven't managed to get this to work in the way that I want. Can anyone offer a bit of help to resolve this please?
Kind regards,
10B.
  1 个评论
10B
10B 2016-5-30
Another combination - again not quite right.
patch([0 0 (x_lim(1,2)/3) (x_lim(1,2)/3)],[y_lim(1) y_lim(2) y_lim(2) y_lim(1) ],[1 0.5 0.2],'FaceAlpha',0.2)
patch([(x_lim(1,2)/3) (x_lim(1,2)/3) ((x_lim(1,2)/2)) ((x_lim(1,2)/2)) ],[y_lim(1) y_lim(2) y_lim(2) y_lim(1) ],[1 1 0.2],'FaceAlpha',0.2)
patch([((x_lim(1,2)/2)) ((x_lim(1,2)/2)) ((x_lim(1,2)/3)*2) ((x_lim(1,2)/3)*2)],[y_lim(1) y_lim(2) y_lim(2) y_lim(1) ],[0.5 1 0.7],'FaceAlpha',0.2)
patch([((x_lim(1,2)/3)*2) ((x_lim(1,2)/3)*2) x_lim(2) x_lim(2)],[y_lim(1) y_lim(2) y_lim(2) y_lim(1) ],[0.4 0.3 0.8],'FaceAlpha',0.2)

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2016-5-30
You will have to define the patch x-values to correspond to the x-values of your data instead of segments of the x-axis. Define them for instance as the mean value of the 4th and 5th, 8th and 9th, and so for the rest.
  2 个评论
10B
10B 2016-5-30
Yet again, Star Strider to the rescue...
Thank you very much for your insight!
Star Strider
Star Strider 2016-5-30
As always, my pleasure!
I wish you all the best in your research!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Polygons 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by