在Matlab中绘制多色线 [英] Plotting multi-colored line in Matlab

查看:102
本文介绍了在Matlab中绘制多色线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用两个颜色的虚线绘制一条垂直线(我希望使用任何方向,但现在我都希望垂直线),例如红色-蓝色-红色-蓝色-...

I would like to plot a vertical line (I'd prefer any orientation, but I'd be happy with just vertical right now) with two-color dashes, say red-blue-red-blue-...

我知道我可以这样做:

plot([1,1],[0,1],'r'),
hold on,
plot([1,1],[0,1],'--b')

但是,由于我需要能够移动这条线,所以它只能有一个手柄.我该怎么办?

However, since I need to be able to move the line, among others, it should only have a single handle. How could I do this?

编辑 谢谢您的回答.我想我确实应该提供更多信息.

EDIT Thank you for your answers. I guess I should indeed give some more information.

我有一些数据分为不同的部分.我希望能够手动调整类之间的边界.为此,我在分类边界处绘制垂直线,并使用可拖动进行允许移动线.

I have some data that is classified into different parts. I want to be able to manually adjust the boundaries between classes. For this, I'm drawing vertical lines at the classification boundaries and use draggable to allow moving the lines.

对于红色和蓝色类之间的边界,我想有一条红色/蓝色线.

For the boundary between the red and the blue class, I'd like to have a red/blue line.

plot(ones(10,1),linspace(0,1,10),'-bs','MarkerFaceColor','r','MarkerEdgeColor','none','linewidth',6)

是我目前正在实际使用的.但是,它并不是那么漂亮(如果我想要相等的间距,这将成为一种真正的痛苦,并且我想给两种颜色相同的权重),并且我希望能够使用三种颜色(而不是使用标记边缘和脸变了,因为它使我的眼睛流血了.

is what I'm actually using at the moment. However, it's not so pretty (if I want equal spacing, it becomes a real pain, and I want to give both colors the same weight), and I would like to have the possibility to use three colors (and not with marker edge and face being different, because it makes my eyes bleed).

不幸的是,draggable不允许我使用多个句柄,并且用hggroup分组行似乎无法创建可拖动的对象.

Unfortunately, draggable does not allow me to use multiple handles, and grouping the lines with hggroup does not seem to create a draggable object.

cline 看起来像是一种有前途的方法,但是彩虹色将不起作用为我的应用程序.

cline looks like a promising approach, but rainbow colors won't work for my application.

推荐答案

您可以使用已有的代码,只需将每行中的句柄连接到句柄向量中即可.如果要同时更改两行的属性,请 SET 函数能够接受句柄向量作为参数.从 SET :

You can use the code you have, and just concatenate the handles from each line into a vector of handles. When you want to change the properties of both lines simultaneously, the SET function is able to accept the vector of handles as an argument. From the documentation for SET:

set(H,'PropertyName',PropertyValue,...) 将命名属性设置为 对象上的指定值 由H标识. H可以是 句柄,在这种情况下,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


更新:由于您提到您正在使用可拖动来自 MathWorks文件交换,这是另一种解决方案.从可拖动的描述:

UPDATE: Since you mention you are using draggable from the MathWorks File Exchange, here's an alternate solution. From the description of 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.

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

You could then try the following solution:

  • 绘制两条线,并保存每条线的手柄(即h1h2).
  • 在另一个的'UserData'属性中分别放置每个句柄:

  • Plot your two lines, saving the handle for each (i.e. h1 and h2).
  • Put the handle for each in the 'UserData' property of the other:

set(h1,'UserData',h2);
set(h2,'UserData',h1);

  • 创建以下功能:

  • Create the following function:

    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
    

  • 为上述两行打开可拖动,使用上面的方法当任何一个对象移动时被调用的函数:

  • Turn on draggable for both lines, using the above function as the one called when either object is moved:

    draggable(h1,@motionFcn);
    draggable(h2,@motionFcn);
    

  • 这篇关于在Matlab中绘制多色线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆