-1

edit: I figured it out and don't see a place to mark this as answered. Thanks for the suggestions though everyone!

A couple of weeks ago I was trying to force MATLAB to display a y = 0 line for a plot I was making. It was easy enough to search for, but apparently I made it automatic. Now I can't find anything even remotely similar to this new problem. When I run this code:

plot(x,y_known,x,y_simulated);
legend('Simulated','This stupid line right here','Known')

I get the following: plot Notice the line at y = 0, it is not in the code. I wouldn't really care, but it is the second line in the figure and it messes up my ability to create a legend. i.e., if the legend entry was just:

legend('Simulated','Known')

then the legend would say that the known value was green, which is certainly not the case.

I don't really want to create a handle for every single line I plot in the future, and would much rather just get rid of this line. Can anyone provide some help, or at least point me in the right direction?

edit: The y = 0 line also changes its line properties based on whatever is supplied to the first plot entry. So plot(x,y1,'--',x,y2); makes both y1 and y = 0 dashed, but plot(x,y1,x,y2,'--'); would just render the second line dashed

vayn23
  • 1
  • 2
  • Does this happen for anything you plot, or is it just when you run this specific code? If the latter is the case, you'll have to post some of the code around the two lines you have here. – btmcnellis Oct 11 '15 at 21:38
  • I would assume you're doing some `hold` somewhere.. What if you create a new figure and then `plot` it? – Dev-iL Oct 11 '15 at 21:41
  • Would `h = plot(x,y_known,x,y_simulated); legend(h,'Simulated','Known')` suffice? – TroyHaskin Oct 11 '15 at 21:44
  • It was plotting a second column I didn't realize was there (or that it could even do that). Just more evidence I need to stop brute forcing things in matlab and use it vectorially. – vayn23 Oct 11 '15 at 21:48
  • The accept button is a tick mark that should show up next to each answer. – David Oct 12 '15 at 04:17

2 Answers2

1

As an absolute last resort (after failing to find how the line actually gets there), what you can do is access the Children property of your axes and delete the child which is the line you don't want.

Something along the lines of:

ch = get(gca,'Children');
delete(ch(2)); %// Where 2 should be replaced by the child index you're trying to delete.
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
0

There was an errant two much earlier in the code, giving a variable 2 columns instead of one. I didn't realize Matlab would be helpful be helpful and plot both columns (I've always explicitly told it to plot both). Just another case of someone not thinking in vector mode when using Matlab!

vayn23
  • 1
  • 2