在 Matlab 中,如何从曲线绘制直线到特定的 x 轴位置?
In Matlab, how to draw lines from the curve to specific xaxis position?
我有一个光谱数据(x 轴上有 1000 个变量,y 轴上有峰值强度)和各种特定 x 位置的感兴趣峰值列表(称为峰值的矩阵),这是我从我制作的函数中获得的。在这里,我想从每个峰值的最大值到 x 轴画一条线 - 或者,最终,在每个峰值上方放置一个垂直箭头,但我读到这很麻烦,所以欢迎使用垂直线。但是,使用以下代码,我得到 "Error using line Value must be a vector of numeric type"。有什么想法吗?
X = spectra;
[Peak,intensity]=PeakDetection(X);
nrow = length(Peak);
Peak2=Peak; % to put inside the real xaxis value
plot(xaxis,X);
hold on
for i = 1 : nbrow
Peak2(:,i) = round(xaxis(:,i)); % to get the real xaxis value and round it
xline = Peak2(:,i);
line('XData',xline,'YData',X,'Color','red','LineWidth',2);
end
hold off
简单注释:
这里有一个简单的方法来注释峰:
plot(x,y,x_peak,y_peak+0.1,'v','MarkerFaceColor','r');
其中 x
和 y
是您的数据,x_peak
和 y_peak
是您要注释的峰的坐标。添加 0.1
只是为了更好地放置注释,应针对您的数据进行校准。
例如(带有一些任意数据):
x = 1:1000;
y = sin(0.01*x).*cos(0.05*x);
[y_peak,x_peak] = PeakDetection(y); % this is just a sketch based on your code...
plot(x,y,x_peak,y_peak+0.1,'v','MarkerFaceColor','r');
结果:
行注释:
这有点复杂,因为我们每行需要 4 个值。同样,假设 x_peak
和 y_peak
和以前一样:
plot(x,y);
hold on
ax = gca;
ymin = ax.YLim(1);
plot([x_peak;x_peak],[ymin*ones(1,numel(y_peak));y_peak],'r')
% you could write instead:
% line([x_peak;x_peak],[ymin*ones(1,numel(y_peak));y_peak],'Color','r')
% but I prefer the PLOT function.
hold off
结果:
箭头注释:
如果你真的想要那些箭头,那么你需要先将峰值位置转换为标准化的图形单位。如何做到这一点:
plot(x,y);
ylim([-1.5 1.5]) % only for a better look of the arrows
peaks = [x_peak.' y_peak.'];
ax = gca;
% This prat converts the axis unites to the figure normalized unites
% AX is a handle to the figure
% PEAKS is a n-by-2 matrix, where the first column is the x values and the
% second is the y values
pos = ax.Position;
% NORMPEAKS is a matrix in the same size of PEAKS, but with all the values
% converted to normalized units
normpx = pos(3)*((peaks(:,1)-ax.XLim(1))./range(ax.XLim))+ pos(1);
normpy = pos(4)*((peaks(:,2)-ax.YLim(1))./range(ax.YLim))+ pos(2);
normpeaks = [normpx normpy];
for k = 1:size(normpeaks,1)
annotation('arrow',[normpeaks(k,1) normpeaks(k,1)],...
[normpeaks(k,2)+0.1 normpeaks(k,2)],...
'Color','red','LineWidth',2)
end
结果:
我有一个光谱数据(x 轴上有 1000 个变量,y 轴上有峰值强度)和各种特定 x 位置的感兴趣峰值列表(称为峰值的矩阵),这是我从我制作的函数中获得的。在这里,我想从每个峰值的最大值到 x 轴画一条线 - 或者,最终,在每个峰值上方放置一个垂直箭头,但我读到这很麻烦,所以欢迎使用垂直线。但是,使用以下代码,我得到 "Error using line Value must be a vector of numeric type"。有什么想法吗?
X = spectra;
[Peak,intensity]=PeakDetection(X);
nrow = length(Peak);
Peak2=Peak; % to put inside the real xaxis value
plot(xaxis,X);
hold on
for i = 1 : nbrow
Peak2(:,i) = round(xaxis(:,i)); % to get the real xaxis value and round it
xline = Peak2(:,i);
line('XData',xline,'YData',X,'Color','red','LineWidth',2);
end
hold off
简单注释:
这里有一个简单的方法来注释峰:
plot(x,y,x_peak,y_peak+0.1,'v','MarkerFaceColor','r');
其中 x
和 y
是您的数据,x_peak
和 y_peak
是您要注释的峰的坐标。添加 0.1
只是为了更好地放置注释,应针对您的数据进行校准。
例如(带有一些任意数据):
x = 1:1000;
y = sin(0.01*x).*cos(0.05*x);
[y_peak,x_peak] = PeakDetection(y); % this is just a sketch based on your code...
plot(x,y,x_peak,y_peak+0.1,'v','MarkerFaceColor','r');
结果:
行注释:
这有点复杂,因为我们每行需要 4 个值。同样,假设 x_peak
和 y_peak
和以前一样:
plot(x,y);
hold on
ax = gca;
ymin = ax.YLim(1);
plot([x_peak;x_peak],[ymin*ones(1,numel(y_peak));y_peak],'r')
% you could write instead:
% line([x_peak;x_peak],[ymin*ones(1,numel(y_peak));y_peak],'Color','r')
% but I prefer the PLOT function.
hold off
结果:
箭头注释:
如果你真的想要那些箭头,那么你需要先将峰值位置转换为标准化的图形单位。如何做到这一点:
plot(x,y);
ylim([-1.5 1.5]) % only for a better look of the arrows
peaks = [x_peak.' y_peak.'];
ax = gca;
% This prat converts the axis unites to the figure normalized unites
% AX is a handle to the figure
% PEAKS is a n-by-2 matrix, where the first column is the x values and the
% second is the y values
pos = ax.Position;
% NORMPEAKS is a matrix in the same size of PEAKS, but with all the values
% converted to normalized units
normpx = pos(3)*((peaks(:,1)-ax.XLim(1))./range(ax.XLim))+ pos(1);
normpy = pos(4)*((peaks(:,2)-ax.YLim(1))./range(ax.YLim))+ pos(2);
normpeaks = [normpx normpy];
for k = 1:size(normpeaks,1)
annotation('arrow',[normpeaks(k,1) normpeaks(k,1)],...
[normpeaks(k,2)+0.1 normpeaks(k,2)],...
'Color','red','LineWidth',2)
end
结果: