Issue with plotting order

5 visualizaciones (últimos 30 días)
Emily Barber
Emily Barber el 9 de Sept. de 2024
Comentada: Jatin el 11 de Sept. de 2024
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');

Respuesta aceptada

Jatin
Jatin el 9 de Sept. de 2024
Editada: Jatin el 11 de Sept. de 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 comentarios
Emily Barber
Emily Barber el 11 de Sept. de 2024
This worked! Thank you so much!
Jatin
Jatin el 11 de Sept. de 2024
My pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

Voss
Voss el 10 de Sept. de 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')

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by