Highlight the region of plot or find some reliable and proper evaluation for the plot
86 views (last 30 days)
Show older comments
Soumyajit Samal
on 24 Jul 2021
Answered: Afiq Azaibi
on 17 Mar 2023
Hello!
I was plotting a curve from a experimental data and the plot showed up like this. Now i want to highlight the region where the graph is plotted. Maybe kind of joining the peaks together and lows together or maybe through a trendline or simply joining the peaks and lows together.
Please guide me to do that. Or if any suggestions on how to plot something reliable and easy to infer from this plot, is highly appeciated.
Thank you in advance.
0 Comments
Accepted Answer
Afiq Azaibi
on 17 Mar 2023
If you'd like to highlight a region of the line chart, starting in R2023a you can use the xregion and yregion functions:
ydata = rand(1,100);
plot(ydata);
% Find the indices of the largest and smallest value in ydata.
minIndex = find(ydata==min(ydata));
maxIndex = find(ydata==max(ydata));
% Highlight the region between the highest and lowest value.
xregion(maxIndex, minIndex, 'FaceColor', 'g');
0 Comments
More Answers (1)
Image Analyst
on 24 Jul 2021
Try this, and adapt as needed:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 15;
fprintf('Beginning to run %s.m ...\n', mfilename);
theta = 0 : 32*pi;
period = 30;
y = cos(2 * pi * theta / period);
plot(theta, y, 'b-', 'LineWidth', 2)
grid on;
% Shade the plot between 20 and 60 with red
ShadePlot(20, 60, 'r')
% --------------------------------------------------------------------
% Shades the plot with the specified color.
% Sample call:
% % Have a red zone from 0-159.
% colors = 'r';
% ShadePlot(0, 159, colors);
function ShadePlot(x1, x2, theColor)
try
hold on;
xl = xlim();
yl = ylim();
y = [yl(1), yl(1), yl(2), yl(2), yl(1)];
if theColor ~= 0
% If the zone is not supposed to be transparent/unshaded/untinted.
x = [x1, x2, x2, x1, x1];
fillHandle = fill(x, y, theColor, 'FaceAlpha', 0.1);
% Put up text labels
if x1 > 0 && x1 < 255
text(x1 + 1, 0.95*yl(2), num2str(x1), 'Color', [0, 0.5, 0]);
end
if x2 > 0 && x2 < 255
text(x2 + 1, 0.95*yl(2), num2str(x2), 'Color', [0, 0.5, 0]);
end
end
% Force immediate update of display.
drawnow;
catch ME
% Alert the user of the error.
errorMessage = sprintf('Error in program %s.\nError Message:\n%s', ...
mfilename, ME.message);
% Print the error message out to a static text on the GUI.
fprintf('%s\n', errorMessage);
uiwait(errordlg(errorMessage)); % Pop up message.
end
return; % from ShadePlot()
end
3 Comments
Image Analyst
on 24 Jul 2021
Edited: Image Analyst
on 24 Jul 2021
You say "that part of the graph only" but don't say what "that part" is.
When you say "highlight the region where the graph is plotted", well to me, that means the entire graph. What does it mean to you? Can you attach a picture that shows where you want the shading to be?
Have you seen the FAQ:
See Also
Categories
Find more on Graphics Performance 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!