How can plot with smooth line?
350 views (last 30 days)
Show older comments
Riyadh Muttaleb
on 8 Aug 2016
Commented: Muhammad Usman Saleem
on 29 Nov 2017
I have these data:
x=[1.6 1.1 .6 .1 .4 .9 1.4 1.9];
y=[1 1.5 2 2.5 3 3.5 4 4.5];
I would like to plot them smoothly w/o any angles.
I have used smooth function (like smooth(x)) but still I have angles.
Thanks in advance,
Riyadh
0 Comments
Accepted Answer
Image Analyst
on 9 Aug 2016
With your data, my demo would become this:
% Create the original knot points.
xInitial = [1.6 1.1 .6 .1 .4 .9 1.4 1.9];
yInitial = [1 1.5 2 2.5 3 3.5 4 4.5];
lengthX = length(xInitial);
% flip sideways so there is only 1 y for every x
x = yInitial;
y = xInitial;
% Plot it and show how the line has sharp bends.
plot(x, y, '-sr', 'LineWidth', 2);
grid on;
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Use splines to interpolate a smoother curve,
% with 10 times as many points,
% that goes exactly through the same data points.
samplingRateIncrease = 10;
newXSamplePoints = linspace(min(x), max(x), lengthX * samplingRateIncrease);
smoothedY = spline(x, y, newXSamplePoints);
% Now flip back
ySmooth = newXSamplePoints;
xSmooth = smoothedY;
% Plot smoothedY and show how the line is
% smooth, and has no sharp bends.
hold on; % Don't destroy the first curve we plotted.
plot(newXSamplePoints, smoothedY, '-ob');
title('Spline Interpolation Demo', 'FontSize', 20);
legend('Original Points', 'Spline Points');
figure;
plot(xInitial, yInitial, '-sr', 'LineWidth', 2);
% Plot smoothedY and show how the line is
% smooth, and has no sharp bends.
hold on; % Don't destroy the first curve we plotted.
plot(xSmooth, ySmooth, '-ob');
grid on;
You can also use a Savitzky-Golay filter to smooth curves, and I attach a demo for that.
4 Comments
Image Analyst
on 24 Nov 2017
Not sure what you mean. If you have a problem with your data, then post your data.
Muhammad Usman Saleem
on 29 Nov 2017
Dear Sir;
More Answers (2)
Walter Roberson
on 8 Aug 2016
It is not possible to plot without any angles. Bit-mapped displaces have angles at every pixel, and vector displays are not able to support true curves. In MATLAB, ultimately every curve is approximated by straight lines or discretized into pixels.
What is possible is to create a line that appears to a be somewhat smooth curve, provided that a high enough density display is used.
My guess is that you want to use cubic spline interpolation to invent bogus intermediate points for the sake of disguising how sparse your justifiable data is. (I get kind of negative about the overuse of splines; not everyone agrees with me.)
Image Analyst
on 9 Aug 2016
See my spline demo:
% Demo to show spline interpolation.
% Clean up / initialize
clc;
close all;
clear all;
workspace; % Display workspace panel.
% Create the original knot points.
lengthX = 10;
x = 1:lengthX;
y = rand (lengthX,1);
% Plot it and show how the line has sharp bends.
plot(x, y, '-sr', 'LineWidth', 2);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Use splines to interpolate a smoother curve,
% with 10 times as many points,
% that goes exactly through the same data points.
samplingRateIncrease = 10;
newXSamplePoints = linspace(1, lengthX, lengthX * samplingRateIncrease);
smoothedY = spline(x, y, newXSamplePoints);
% Plot smoothedY and show how the line is
% smooth, and has no sharp bends.
hold on; % Don't destroy the first curve we plotted.
plot(newXSamplePoints, smoothedY, '-ob');
title('Spline Interpolation Demo', 'FontSize', 20);
legend('Original Points', 'Spline Points');
% Mathworks Demo code from their Help
% x = 0:10;
% y = sin(x);
% xx = 0:.25:10;
% yy = spline(x,y,xx);
% plot(x,y,'o',xx,yy)
slopes = [0, diff(smoothedY)];
plot(newXSamplePoints, slopes, 'k-', 'LineWidth', 3);
% Draw x axis
line(xlim, [0,0], 'Color', 'k', 'LineWidth', 2);
grid on;
legend('Original Points', 'Spline Points', 'Slope');
0 Comments
See Also
Categories
Find more on Splines 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!