Plotting multi-colored line in Matlab

前端 未结 3 1968
醉酒成梦
醉酒成梦 2020-12-31 06:12

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

相关标签:
3条回答
  • 2020-12-31 06:50

    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);
      
    0 讨论(0)
  • 2020-12-31 06:53

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

    0 讨论(0)
  • 2020-12-31 07:15

    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.

    0 讨论(0)
提交回复
热议问题