在Matlab中,如何绘制从曲线到特定x轴位置的线? [英] In Matlab, how to draw lines from the curve to specific xaxis position?
问题描述
我有一个光谱数据(x轴上有1000个变量,峰强度为y),还有我在一个特定的x位置(称为峰"的矩阵)上感兴趣的峰的列表,这些峰是我根据函数求出的.在这里,我想从每个峰的最大值到x轴画一条线-或最终在每个峰的上方放置一个垂直箭头,但我读到它很麻烦,因此只欢迎一条垂直线.但是,使用下面的代码,我得到使用行值错误必须是数字类型的向量".有什么想法吗?
I have a spectral data (1000 variables on xaxis, and peak intensities as y) and a list of peaks of interest at various specific x locations (a matrix called Peak) which I obtained from a function I made. Here, I would like to draw a line from the maximum value of each peaks to the xaxis - or, eventually, place a vertical arrow above each peaks but I read it is quite troublesome, so just a vertical line is welcome. However, using the following code, I get "Error using line Value must be a vector of numeric type". Any thoughts?
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
推荐答案
简单注释:
这是注释峰的简单方法:
Here is a simple way to annotate the peaks:
plot(x,y,x_peak,y_peak+0.1,'v','MarkerFaceColor','r');
其中x
和y
是您的数据,而x_peak
和y_peak
是您要注释的峰的坐标. 0.1
的添加只是为了更好地放置批注,应针对您的数据进行校准.
例如(带有一些任意数据):
where x
and y
is your data, and x_peak
and y_peak
is the coordinates of the peaks you want to annotate. The add of 0.1
is just for a better placing of the annotation and should be calibrated for your data.
For example (with some arbitrary data):
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
:
This is just a little bit more complicated because we need 4 values for each line. Again, assuming x_peak
and y_peak
as before:
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
和结果:
箭头注释:
如果您确实想要这些箭头,则需要先将峰位置转换为归一化的图形单位.这里是如何做到的:
If you really want those arrows, then you need to first convert the peak location to the normalized figure units. Here how to do that:
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
和结果:
这篇关于在Matlab中,如何绘制从曲线到特定x轴位置的线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!