# Matlab Plotting: Move Horizontal Line Over Graph Without Re-Plotting

• MATLAB
• JohnSimpson
In summary, the user is looking for a way to dynamically move a horizontal line plotted over a large amount of data without having to re-plot the data every time. This can be achieved by using the 'erase' property when drawing, specifically setting it to 'none' for the first plotted graph (the large amount of data) and 'normal' for the horizontal line. The user can then use the set() function to update the coordinates of the line and use drawnow to update the plot. The example code provided demonstrates this concept.

#### JohnSimpson

In my GUI I'm plotting a fairly large amount of data in an axes, and then plotting a horizontal line overtop that the user can control the vertical position of. Is there a way I can have the first plotted graph (the large amount of data) always be behind the line I am plotting overtop, and have the user be able to dynamically move the line up and down the axes without having to re-plot both graphs every time the user wants to move the horizontal line?

For clarity, here's a picture

http://img229.imageshack.us/my.php?image=exampleya8.jpg

Again, I want the user to be able to dynamically move the line up and down without altering the blue signal (because it takes a while to re-plot the blue signal). Suggestions?

Bump!

Gonna give it one last shot

Ok, I don't remember exactly how to do this, but I believe it has something to do with the 'erase' property when drawing.

http://www.mathworks.com/access/hel...-us&q=matlab+plotting+erase&ie=UTF-8&oe=UTF-8

Look at the part:
Erase Modes
...
none — The object is not erased when it is moved.
...

basically you do something like:
Code:
keep_me  = line('color','b','erase','none');
move_me = line('color','r','erase','normal');

keepX = [0 1 2 3 4];
keepY = [0 1 2 3 4];
set(keep_me, 'xdata', keepX, 'ydata', keepY);

moveX = [0 1 2 3 4];
moveY = [1 1 1 1 1];

set(move_me, 'xdata', moveX, 'ydata', moveY);
drawnow;

pause;

moveY = [2 2 2 2 2];

set(move_me, 'xdata', moveX, 'ydata', moveY);
drawnow;

Something like that... treat that as pseudo code cause I'm trying to remember off the top of my head. I don't remember exactly if you need that drawnow in there, or (to be honest) when to use the drawnow.

## 1. How can I move a horizontal line over a graph in Matlab without re-plotting the entire graph?

One way to do this is by using the "line" function in Matlab. You can specify the x and y coordinates for the line, and then use the "set" function to update the position of the line on the graph without re-plotting the entire graph.

## 2. Is there a way to animate the horizontal line movement over the graph?

Yes, you can use the "pause" function in a for loop to create an animation effect. Inside the for loop, you can update the coordinates of the line and use the "drawnow" function to show the changes on the graph in real-time.

## 3. Can I change the color or style of the horizontal line?

Yes, you can use the "set" function to specify the properties of the line, such as color, line width, and line style. You can also use the "get" function to retrieve the current properties of the line.

## 4. How can I add multiple horizontal lines to a single graph?

You can use the "line" function multiple times, each with different coordinates and properties, to add multiple horizontal lines to a single graph. Alternatively, you can also store the lines in a vector and use the "set" function to update their positions and properties.

## 5. Are there any other ways to move a horizontal line on a graph without re-plotting?

Yes, you can also use the "patch" function to create a horizontal line as a patch object, and then use the "set" function to update its position. Another option is to use the "annotation" function to add a horizontal line annotation to the graph. Both of these methods also allow for customization of the line's properties.