How can plot with smooth line?
349 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Image Analyst
2016-8-9
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 个评论
Image Analyst
2017-11-24
Not sure what you mean. If you have a problem with your data, then post your data.
更多回答(2 个)
Walter Roberson
2016-8-8
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
2016-8-9
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Splines 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!