Loop to plot PDF over bar plot
5 views (last 30 days)
Show older comments
I am trying to write code to plot a line from a probability density function over the bar plot from which it's derived. The data for the bar plot is stored in an matrix called gsd. The first column in the scale and columns 3:19 are data. The PDF data is stored in a matrix called deco. The labels are stored in a 1x19 cell called label. Each PDF has a different scale. When I plot them one at a time I do as follows:
bar(gsd(:,1), gsd(:,3))
hold on
plot(deco(:,1), deco(:,2), 'LineWidth', 3)
xlabel ('Grain size (phi)')
ylabel ('Weight Percent')
title (label(:,3))
For each sample I change the y of the bar plot to be the next column, the x and y of the line to be the next two columns, and the title to the next column over. I'd like to do this as a loop, but I'm having trouble coding the loop to loop through 3 different matrices.
I've managed to loop making the bar plot but I can't figure out how to save the file or make the title each columns' label. I also split the deco matrix into just the scale and pdf because I couldn't figure out how to make it call two at a time from one matrix. This cycles through but it never stops and the plotted lines become increasing nonesensical. Below is my code. I'm sure some of the naming is non-standard. I'm self taught so please tell me everything I'm doing wrong so I can learn. Thanks!
for ColIdx= 3:19 % grain size data
for i= 3:19 % gsd labels
for j = 1:17 % pdf scale
for k= 1:17 % pdf
xdata= gsd(:,ColIdx); % tells loop what each loop is pulling from
qlabel= label(:, i);
qscale= scale(:,j)
qpdf= pdf(:,k)
filename = sprintf('i_GSD.png', ColIdx);
figure;
bar(gsd(:,1), xdata)
xlabel ('Grain size (phi)');
ylabel ('Weight Percent (%)');
title (sprintf ('i_GSD', ColIdx));
hold on
plot( qscale, qpdf, 'LineWidth', 3)
% Save the plot as a PNG file
saveas(gcf, filename);
close(figure)
end
end
end
end
0 Comments
Accepted Answer
dpb
on 26 Mar 2025
Edited: dpb
on 27 Mar 2025
It would be easier to be sure we understand if you would attach a .mat file with data arrays...but, here's a stab at it...
What works for one as template--
bar(gsd(:,1), gsd(:,3))
hold on
plot(deco(:,1), deco(:,2), 'LineWidth', 3)
xlabel ('Grain size (phi)')
ylabel ('Weight Percent')
title (label(:,3))
So looping construct could be something like...
nGS=width(gsd); % max number in the grain size array in case changes
ixGSx=1; % the location of x data in case it were to change
ixGS1 = 3; % the first data column in case it changes, too
ixLab1 = 1; % index into label array for first
iX=1; % first index of the X data (Y assumed next column)
for ixGS=ixGS1:nGS % grain size data
hF=figure; % start a new figure, keep a handle
hB=bar(gsd(:,ixGSx),gsd(:,ixGS)) % make the barplot, save handle so can decorate later if want
hold on
hL=plot(deco(:,iX),deco(:,iX+1),'linewidth',3);
xlabel ('Grain size (phi)');
ylabel ('Weight Percent (%)');
title(label(ixGS));
filename = sprintf('GSD_%02d.png',ixGS); % lead with alpha character and use leading zero so will sort
saveas(hF, filename);
close(hF)
iX=iX+2: % increment pointer to deco X column for next pass...
end
I think you need only the one outer loop; the "trick" in such things is to keep other counters to increment as needed into the other arrays; don't try to do everything with just the one loop index.
I didn't try to create fake data so the above is air code/untested and undoubtedly will have issues to fix...attach some real data files and we can sort things out.
More Answers (0)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!