我想用双色虚线绘制一条垂直线(我更喜欢任何方向,但我现在对垂直很满意),比如红-蓝-红-蓝-...
我知道我可以这样做:
plot([1,1],[0,1],'r'),
hold on,
plot([1,1],[0,1],'--b')
但是,由于我需要能够移动线,除其他外,它应该只有一个 handle 。我怎么能这样做?
编辑 谢谢您的回答。我想我确实应该提供更多信息。
我有一些数据被分为不同的部分。我希望能够手动调整类之间的边界。为此,我在分类边界处绘制垂直线并使用 draggable允许移动线。
对于红色和蓝色类之间的边界,我想要一条红/蓝线。
plot(ones(10,1),linspace(0,1,10),'-bs','MarkerFaceColor','r','MarkerEdgeColor','none','linewidth',6)
是我目前实际使用的。然而,它不是那么漂亮(如果我想要相等的间距,它会变得很痛苦,我想给两种颜色相同的权重),我希望有可能使用三种颜色(而不是标记边缘和脸是不同的,因为它让我的眼睛流血)。
不幸的是,draggable 不允许我使用多个句柄,并且使用 hggroup 对行进行分组似乎无法创建可拖动对象。
cline看起来是一种很有前途的方法,但彩虹色不适用于我的应用程序。
最佳答案
您可以使用已有的代码,只需将每一行的句柄连接成一个句柄向量。当您想同时更改两条线的属性时,SET函数能够接受句柄向量作为参数。来自 SET 的文档:
set(H,'PropertyName',PropertyValue,...)
sets the named properties to the specified values on the object(s) identified byH
.H
can be a vector of handles, in which caseset
sets the properties' values for all the objects.
这是一个例子:
h1 = plot([1 1],[0 1],'r'); %# Plot line 1
hold on;
h2 = plot([1 1],[0 1],'--b'); %# Plot line 2
hVector = [h1 h2]; %# Vector of handles
set(hVector,'XData',[2 3]); %# Shifts the x data points for both lines
更新:既然你提到你正在使用 draggable来自 MathWorks File Exchange ,这是一个替代解决方案。来自 draggable 的描述:
A function which is called when the object is moved can be provided as an optional argument, so that the movement triggers further actions.
然后您可以尝试以下解决方案:
- 绘制两条线,保存每条线的句柄(即
h1
和h2
)。 将每个句柄放在另一个的
'UserData'
属性中:set(h1,'UserData',h2); set(h2,'UserData',h1);
创建以下函数:
function motionFcn(hMoving) %# Currently moving handle is passed in hOther = get(hMoving,'UserData'); %# Get the other plot handle set(hOther,'XData',get(hMoving,'XData'),... %# Update the x data 'YData',get(hMoving,'YData')); %# Update the y data end
开启 draggable对于这两行,使用上述函数作为移动任一对象时调用的函数:
draggable(h1,@motionFcn); draggable(h2,@motionFcn);
关于matlab - 在 Matlab 中绘制多色线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2444575/