简体   繁体   English

创建具有变化的轴值的水平线和垂直线? (MatLab)

[英]Creating Horizontal and Vertical lines with varying axes values? (MatLab)

I am trying to plot both horizontal and vertical lines on a histogram that will be accurate to changing limits on both the x and y axes. 我正在尝试在直方图中绘制水平和垂直线,以精确地更改x和y轴上的限制。 I was using the line(X,Y) function, but cannot find a useful way to get the lines to be set depending on the parameters of the graph window. 我使用的是line(X,Y)函数,但是找不到一种有用的方式来获取要设置的线,具体取决于图形窗口的参数。

I'm not entirely clear on what you want but here's the simplest answer to what I think you want: 对于您想要什么我还不太清楚,但是这是我认为您想要的最简单的答案:

Makes a sample histogram 制作样本直方图

y = randn(100,1);
hist(y,10)

Get the current limits of the x and y axes 获取x和y轴的当前极限

xlimits = get(gca, 'XLim');
ylimits = get(gca, 'YLim');

Computes a single numeric value to plot a horizontal line.You'll want to replace this with your specific function of the axes limits 计算单个数值以绘制水平线。您需要用特定的轴限制功能替换它

halfpt = ((ylimits(2)-ylimits(1))/2) + ylimits(1);
line(xlimits, [halfpt halfpt])

I'm not sure, but from your comment I'm suspecting that you aren't changing your axes programmatically, with say set(gca,'Xlim', [0 10]) but that you want to be able to drag the axes of your your figure with the mouse, say by using that hand/pointer button in the figure editor. 我不确定,但是根据您的评论,我怀疑您不是以编程方式更改轴,例如set(gca,'Xlim', [0 10])但是您希望能够拖动轴用鼠标来绘制图形,例如使用图形编辑器中的“手/指针”按钮。 In which case, one solution is to make your figure a GUI and write a callback function that handles line plotting that is a function of the xlim and ylim . 在这种情况下,一种解决方案是使您的图形成为GUI并编写一个回调函数来处理线图,该函数是xlimylim的函数。 Here's an example that always keeps the line in the middle of the axes regardless of how they are dragged: 这是一个示例,始终将线保持在轴的中间,而不管它们如何拖动:

function myGUI
figure('WindowButtonMotionFcn',@myCallback)
y = randn(100,1);
hist(y,10)

function myCallback(src,eventdata)
xlimits = get(gca, 'XLim');
ylimits = get(gca, 'YLim');
halfpt = ((ylimits(2)-ylimits(1))/2) + ylimits(1);

lh = findall(gcf,'Type','Line');
delete(lh);
myline = line(xlimits, [halfpt halfpt])

end

end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM