Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting multi-colored line in Matlab

Tags:

plot

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

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.

like image 222
Jonas Avatar asked Mar 15 '10 01:03

Jonas


People also ask

How do I color a line in MATLAB?

Specify Line Width, Marker Size, and Marker ColorCreate a line plot and use the LineSpec option to specify a dashed green line with square markers. Use Name,Value pairs to specify the line width, marker size, and marker colors. Set the marker edge color to blue and set the marker face color using an RGB color value.

How do you add color to a plot in MATLAB?

Answers (1) % plot your lines and they will be plotted with these colors in order. plot(x2,y,2); Then plot your other lines ... plot(x,y,'Color',color);

How do you put multiple lines on one graph in MATLAB?

Plot Multiple Lines By default, MATLAB clears the figure before each plotting command. Use the figure command to open a new figure window. You can plot multiple lines using the hold on command. Until you use hold off or close the window, all plots appear in the current figure window.


1 Answers

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);
    
like image 115
gnovice Avatar answered Oct 12 '22 03:10

gnovice