フィルターのクリア

How to get the y-axis in the foreground while having the grids in the background?

8 ビュー (過去 30 日間)
Amelie
Amelie 2024 年 4 月 20 日
コメント済み: Adam Danz 2024 年 4 月 23 日
Dear all,
this is a version of my code:
figure()
hold on
grid on
set(gca, 'Layer', 'top')
jbfill(1:100,x(:,1)',y(:,1)',color.red, color.red,0,1);
hold off
xticks(0:1:100);
title('Temperature');
ylabel('Percent');
xlabel('Months');
I have the following problem:
I try to get the grids in the background behind jbfill and the yaxis/ yticks above the jbfill, but jbfill is lying above the y-axis / y ticks and thus covering y-axis / y ticks. So in order to get the y-axis / y ticks above jbfill (to make them visible) I use " set (gca, 'Layer','top') " . But then the grids are automatically "above" jbfill although they should be not visible where the jbfill plot is.
Does someone have a hint how I can solve this problem?
Many thanks for your help!

採用された回答

Voss
Voss 2024 年 4 月 20 日
"I try to get the grids in the background behind jbfill and the yaxis/ yticks above the jbfill"
That's no going to work; the grids and ticks must "go together" - eitther all behind other stuff in the axes (if Layer is 'bottom') or all in front of other stuff in the axes (if Layer is 'top').
However, you can create your own lines that represent grid lines and place those behind of or in front of whatever you want.
Example:
x = 0:50;
y = rand(1,numel(x));
figure()
hold on
grid off
box on
set(gca, 'Layer', 'top')
% vertical grid line coordinates:
vxd = x([1 1],1:5:end);
vxd(end+1,:) = NaN;
vyd = repmat([0;1;NaN],1,size(vxd,2));
% horizontal grid line coordinates:
hxd = repmat([x([1 end]).'; NaN],1,11);
hyd = repmat(linspace(0,1,11),2,1);
hyd(end+1,:) = NaN;
% create the grid lines (before creating the patch):
line(vxd(:),vyd(:),'Color',0.15*[1 1 1],'LineStyle','--')
line(hxd(:),hyd(:),'Color',0.15*[1 1 1],'LineStyle','--')
% create a patch (on top of the grid lines):
patch(x([1 1:end end]),[0 y 0],'r')
  3 件のコメント
Voss
Voss 2024 年 4 月 21 日
You're welcome!
Adam Danz
Adam Danz 2024 年 4 月 23 日
Starting in R2024a you can control the stack order of ConstantLine objects. The benefits of using a ConstantLine rather than a line() is that ConstantLines are 1-dimensional, you won't see the end of the line if the axes limits change, and the code is simpler.
Using @Voss's helpful demo,
x = 0:50;
y = rand(1,numel(x));
figure()
hold on
grid off
box on
set(gca, 'Layer', 'top')
% create the grid lines (before or after the patch)
xlines = x(1:5:end);
ylines = 0:.1:1;
xline(xlines, '--', 'Color', 0.15*[1 1 1], 'Layer','bottom')
yline(ylines, '--', 'Color', 0.15*[1 1 1], 'Layer','bottom')
% create a patch (on top of the grid lines):
patch(x([1 1:end end]),[0 y 0],'r')
  • xline|yline released in R2018b
  • array support in xline|yline: R2021a
  • Layer property added R2024a
  • Note that xline|yline have a default alpha value of 0.7 which can be changed using the Alpha property.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by