How can plot with smooth line?
354 ビュー (過去 30 日間)
古いコメントを表示
Riyadh Muttaleb
2016 年 8 月 8 日
コメント済み: Muhammad Usman Saleem
2017 年 11 月 29 日
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.
Muhammad Usman Saleem
2017 年 11 月 29 日
Dear Sir;
その他の回答 (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!