How to shade area under curve and x axis in log-log scale?
조회 수: 28 (최근 30일)
이전 댓글 표시
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);
댓글 수: 0
답변 (2개)
Star Strider
2020년 5월 4일
편집: Star Strider
2020년 5월 4일
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:
.
댓글 수: 0
Adam Danz
2020년 5월 4일
편집: Adam Danz
2020년 5월 4일
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')
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');
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Contour Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!