How to shade area under curve and x axis in log-log scale?

25 views (last 30 days)
Hello!!!
I have two plots of power spectral density in log-log scale (please see the code below). I would to shade area under these two curves. Can anyone please guide me?
figure(61)
h1=loglog(f_1(:,1),Su_1(:,2), 'color', color_plot_i...
, 'DisplayName', legend_name_i,'linewidth',2); %% f_1 is the frequency, Su_1 is the power
hold on
h2=loglog(f_2(:,1),Su_2(:,2), 'color', color_plot_i...
, 'DisplayName', legend_name_i,'linewidth',2);
xlabel('Frequency (Hz)','fontsize',20);
xlim([0.01 1]);
ylabel('Power Spectrum','fontsize',20);
set(gca, 'XScale', 'log', 'YScale','log')
% set unit for figure size to inches
set(gcf, 'unit', 'inches');
% get the original size of figure before the legends are added
figure_size = get(gcf, 'position');
% add legends and get its handle
h_legend=findobj(gcf,'type','legend');
if Switch_Plot_Legend==1
set(h_legend,'Location','NorthOutside') % legend is outside
else
set(h_legend,'Location','BestOutside') % legend is outside
end
% set unit for legend size to inches
set(h_legend, 'unit', 'inches');
% get legend size
legend_size = get(h_legend, 'position');
% new figure width
figure_size(3) = figure_size(3) + legend_size(3);
% set new figure size
set(gcf, 'position', figure_size)
ImageName61 = sprintf('%s%s%s','PSD_plot, '.png'); % Save the image
print('-dpng',ImageName61,'-painters'); % Print = save
newChr61 = strrep(ImageName61,'png','fig');
savefig(newChr61);

Answers (2)

Star Strider
Star Strider on 4 May 2020
Edited: Star Strider on 4 May 2020
It is not possible to use patch directly on a logarithmic axis scale (at least I’ve never been able to get that to work), so the only option is to plot the patch objects on a linear scale, then set the appropriate axis scales to 'log'.
Example —
x = linspace(1, 10);
y = exp(-(x-5).^2);
figure
patch([x flip(x)], [y ones(size(y))*min(y)], [1 1 1]*0.6)
set(gca, 'XScale','log', 'YScale','log')
grid
That usually works, and it worked in this example.
EDIT — Added plot image:
.

Adam Danz
Adam Danz on 4 May 2020
Edited: Adam Danz on 4 May 2020
Apply a patch to a loglog plot
If the curve is defined by x and y which are both row vectors,
bottom = min(ylim); % or bottom = 0
p = patch([x,fliplr(x)], [y, repmat(bottom,size(y))], 'y')
if x and y are column vectors,
p = patch([x;flipud(x)], [y; repmat(bottom,size(y))], 'y')
See patch properties to learn how to make the patches partially transparent.
Demo
x = 1:1:20
y = 2.^x + 10;
loglog(x,y)
hold on
bottom = min(ylim); % or bottom = 0
p = patch([x,fliplr(x)], [y, repmat(bottom,size(y))], 'y');

Categories

Find more on Contour Plots in Help Center and File Exchange

Tags

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!