Issue with plotting order
Afficher commentaires plus anciens
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
Plus de réponses (1)
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
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


