在Matlab中绘制多色线

发布于 2024-08-25 07:01:16 字数 909 浏览 4 评论 0原文

我想用两种颜色的破折号绘制一条垂直线(我更喜欢任何方向,但我现在只对垂直感到满意),比如红-蓝-红-蓝-...

我知道我可以这样做:

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

但是,由于我需要能够移动线等,所以它应该只有一个手柄。我怎么能这样做呢?

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

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

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

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

是我现在实际使用的。然而,它并不是那么漂亮(如果我想要相等的间距,它会变得非常痛苦,并且我想给两种颜色相同的权重),并且我希望能够使用三种颜色(而不是使用标记边缘和脸不一样,因为它让我的眼睛流血)。

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

cline 看起来是一种很有前途的方法,但彩虹色不适用于我的应用程序。

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-...

I know I could do it like this:

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).

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 looks like a promising approach, but rainbow colors won't work for my application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北斗星光 2024-09-01 07:01:16

您可以使用现有的代码,只需将每行的句柄连接到句柄向量中即可。当您想要同时更改两条线的属性时,SET 函数能够接受句柄向量作为参数。从 SET 的文档中:

set(H,'PropertyName',PropertyValue,...)
将命名属性设置为
对象上的指定值
H标识。 H 可以是向量
句柄,在这种情况下 set 设置
所有属性的值
对象。

这是一个示例:

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 File Exchange 拖动,这是一个替代解决方案。从 draggable 的描述来看:

一个函数,当
移动的对象可以提供为
可选参数,以便
移动会触发进一步的操作。

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

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,...)
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.

Here's an example:

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


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:

  • 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);
    
提笔落墨 2024-09-01 07:01:16

我从未使用过它,但 Sebastian Hölz 提交了一个名为 CLINE在似乎相关的 Mathworks 文件交换上。

I've never used it, but there's a submission by Sebastian Hölz called CLINE on the Mathworks File Exchange that seems related.

[浮城] 2024-09-01 07:01:16

我不知道如何准确地执行您想要的操作,但想必您想要执行此操作的原因是想通过某种方式将该行与其他行区分开来。沿着这些思路,请查看 MathWorks 的 有关 2- 的文档D 线图。具体来说,这个例子:

plot(x,y,'--rs','LineWidth',2,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','g',...
                'MarkerSize',10)

应该会给你带来很多变化的想法。如果您确实需要双色破折号,那么说明原因可能会有所帮助。这样,即使我们无法回答这个问题,也许我们可以让您相信您并不真正需要双色破折号。由于您已经排除了重叠解决方案,因此我相当确定没有解决方案可以满足您的所有需求。我假设两种颜色是这些需求中最具流动性的。

I don't know how to do exactly what you want, but presumably the reason you want to do this is to have some way of distinguishing this line from other lines. Along those lines, take a look at MathWorks' documentation on 2-D line plots. Specifically, this example:

plot(x,y,'--rs','LineWidth',2,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','g',...
                'MarkerSize',10)

should give you plenty of ideas for variation. If you really need the two-color dashes, it might help to specify why. That way, even if we can't answer the question, perhaps we can convince you that you don't really need the two-color dashes. Since you've already ruled out the over-lapping solution, I'm fairly certain there's no solution that answers all of your needs. I'm assuming the two-colorness is the most fluid of those needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文