Hello,
I have been trying to plot a shaded area (right y-axis) behind two lines (left y-axis), as shown in the code below. However, the shaded area always appears last, covering up the two lines. I've done this kind of graph before but have never had this issue. Best I can tell, it is because the right y-axis is plotted last. Is there a way to make the right y-axis plotted first? I can't simply switch which data is on the axis, unfortunately: they need to be in this order. Thanks for any help!
clear; clc; close all;
Data = xlsread("FigureOfMeritResults.xlsx");
% Extract data
Date = Data(469:1908,1);
FoM = Data(469:1908,3);
GHI = Data(469:1908,4);
% Calculate the running average over 5 data points
FoM_avg = movmean(FoM, 5);
GHI_avg = movmean(GHI, 5);
dFoM = zeros(length(FoM),1) + mean(FoM);
ticks = {'12:00AM','4:00AM','8:00AM','12:00PM','4:00PM','8:00PM','12:00AM'};
fz = 15;
%%
fig = figure;
set(gcf,'Color','w','Units','inches')
set(gcf,'Position',[1,1,5,6])
GHI_color = [0.8 0.8 1];
yyaxis left;
p1 = scatter(Date, FoM, 'Marker', '.', 'SizeData', 15, 'MarkerFaceColor', "#0072BD");
hold on;
p2 = scatter(Date, dFoM, 'Marker', '_', 'SizeData', 15, 'MarkerFaceColor', 'black');
hold on;
ax = gca;
ax.YColor = 'k';
yyaxis right;
xData = [Date; flipud(Date)];
yData = [zeros(size(GHI)); flipud(GHI)];
fillHandle = fill(xData, yData, GHI_color,'EdgeColor','none');
hold on;
ax = gca;
ax.YColor = 'k';
xlabel('Time of Day (hrs)', 'FontSize', fz);
numTicks = length(ticks);
tickPositions = linspace(min(Date), max(Date), numTicks);
xticks(tickPositions);
xticklabels(ticks);
set(gca, 'FontSize', fz);
ax = gca;
box on;
set(ax,'BoxStyle','full','LineWidth',2,'XGrid','off','XMinorTick','off','YGrid','off',...
'YMinorTick','on');

 Réponse acceptée

Jatin
Jatin le 9 Sep 2024
Modifié(e) : Jatin le 11 Sep 2024
If you refer to the documentation on "Graphics Objects Hierarchy," you'll see that the plotting order is based on the sequence in which graphic objects are created. In your code, since the shaded area is plotted last, it should appear on top of the lines.
A solution to this should be using "uistack" function to bring the lines to the front. Apparently, If you go through the following MATLAB Answers post, people have noticed that using "uistack" does not seem to work with "yyaxis" and have adviced to use "SortMethod" property to fix this as a workaround.
Please check if the following workaround is effective for you:
clear; clc; close all;
Data = xlsread("FigureOfMeritResults.xlsx");
% Extract data
Date = Data(469:1908,1);
FoM = Data(469:1908,3);
GHI = Data(469:1908,4);
% Calculate the running average over 5 data points
FoM_avg = movmean(FoM, 5);
GHI_avg = movmean(GHI, 5);
dFoM = zeros(length(FoM),1) + mean(FoM);
ticks = {'12:00AM','4:00AM','8:00AM','12:00PM','4:00PM','8:00PM','12:00AM'};
fz = 15;
%%
fig = figure;
set(gcf,'Color','w','Units','inches')
set(gcf,'Position',[1,1,5,6])
GHI_color = [0.8 0.8 1];
yyaxis left;
p1 = scatter(Date, FoM, 'Marker', '.', 'SizeData', 15, 'MarkerFaceColor', "#0072BD");
hold on;
p2 = scatter(Date, dFoM, 'Marker', '_', 'SizeData', 15, 'MarkerFaceColor', 'black');
hold on;
ax = gca;
ax.YColor = 'k';
yyaxis right;
xData = [Date; flipud(Date)];
yData = [zeros(size(GHI)); flipud(GHI)];
fillHandle = fill(xData, yData, GHI_color,'EdgeColor','none');
hold on;
ax = gca;
ax.YColor = 'k';
xlabel('Time of Day (hrs)', 'FontSize', fz);
numTicks = length(ticks);
tickPositions = linspace(min(Date), max(Date), numTicks);
xticks(tickPositions);
xticklabels(ticks);
set(gca, 'FontSize', fz);
set(gca, 'SortMethod', 'depth')
ax = gca;
box on;
set(ax,'BoxStyle','full','LineWidth',2,'XGrid','off','XMinorTick','off','YGrid','off',...
'YMinorTick','on');
You can know more about 'SortMethod' by referring to the documentation below:
Hope this helps!

5 commentaires

Emily
Emily le 9 Sep 2024
Thank you for this quick response. Oddly, I have tried switching this before to no avail, and using the uistack function causes the following error:
Error using matlab.graphics.axis.Axes/set
Children may only be set to a permutation of itself.
Error in uistack (line 160)
set(UParent,'Children',AllChildren);
Error in Backgroundarea (line 43)
uistack(p1)
I have attached the input file to this comment. Please let me know if I can provide any more information.
Emily
Emily le 10 Sep 2024
Thanks. Yes, I noticed that the right y-axis is being plotted second. Unfortunately I am not able to switch the axis in this case. Thank you for trying to help, however.
Jatin
Jatin le 10 Sep 2024
Modifié(e) : Jatin le 11 Sep 2024
Please see if my revised answer helps resolve the issue for you.
Emily
Emily le 11 Sep 2024
This worked! Thank you so much!
Jatin
Jatin le 11 Sep 2024
My pleasure!

Connectez-vous pour commenter.

Plus de réponses (1)

Voss
Voss le 10 Sep 2024
Data = readmatrix("FigureOfMeritResults.xlsx");
% Extract data
Date = Data(469:1908,1);
FoM = Data(469:1908,3);
GHI = Data(469:1908,4);
% Calculate the running average over 5 data points
FoM_avg = movmean(FoM, 5);
GHI_avg = movmean(GHI, 5);
dFoM = zeros(length(FoM),1) + mean(FoM);
ticks = {'12:00AM','4:00AM','8:00AM','12:00PM','4:00PM','8:00PM','12:00AM'};
fz = 15;
%%
fig = figure;
set(gcf,'Color','w','Units','inches','Position',[1,1,5,6])
GHI_color = [0.8 0.8 1];
xData = [Date; flipud(Date)];
yData = [zeros(size(GHI)); flipud(GHI)];
fillHandle = fill(xData, yData, GHI_color,'EdgeColor','none');
ax1 = gca();
set(ax1, ...
'Box','off', ...
'FontSize',fz);
xlabel(ax1,'Time of Day (hrs)', 'FontSize', fz);
tickPositions = linspace(min(Date), max(Date), numel(ticks));
xticks(ax1,tickPositions);
xticklabels(ax1,ticks);
drawnow
ax1.PositionConstraint = 'innerposition';
ax2 = axes(fig, ...
'Units',ax1.Units, ...
'Position',ax1.Position, ...
'Color','none', ...
'FontSize',fz, ...
'XAxisLocation','top', ...
'YAxisLocation','right', ...
'XTick',tickPositions, ...
'XTickLabel',{}, ...
'PositionConstraint','innerposition', ...
'NextPlot','add');
p1 = scatter(ax2, Date, FoM, 'Marker', '.', 'SizeData', 15, 'MarkerFaceColor', "#0072BD");
p2 = scatter(ax2, Date, dFoM, 'Marker', '_', 'SizeData', 15, 'MarkerFaceColor', 'black');
set([ax1 ax2], ...
'LineWidth',2, ...
'XGrid','off', ...
'XMinorTick','off', ...
'YGrid','off',...
'YMinorTick','on');
linkaxes([ax1 ax2],'x')

Catégories

En savoir plus sur Graphics Performance dans Centre d'aide et File Exchange

Produits

Version

R2023a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by