matlab - 在 Matlab 中绘制多色线

标签 matlab plot

我想用双色虚线绘制一条垂直线(我更喜欢任何方向,但我现在对垂直很满意),比如红-蓝-红-蓝-...

我知道我可以这样做:

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 by H. H can be a vector of handles, in which case set 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.

然后您可以尝试以下解决方案:

  • 绘制两条线,保存每条线的句柄(即 h1h2)。
  • 将每个句柄放在另一个的 '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/

相关文章:

r - 从 rle() 类对象列表中绘制多个直方图

assembly - 将水平线和垂直线绘制到 VBE 返回的 LFB 的结果不正确

Matplotlib 和时间戳造成麻烦

matlab - 代表移动通信系统中使用的蜂窝网络的六角形网格

matlab - 非重叠圆的螺旋

excel - MATLAB:矩阵数据的散点图

python - 如何使用 plotly figure_factory 仅绘制散点图矩阵的下半部分?

image - RGB图像转二值图像

image - 使用 Image.CData 在 MATLAB 中快速重绘图像

matlab - 在创建动画 gif 时阻止 Matlab 调整表面大小