How to get rid of blue line in regression plot in matlab? - matlab

I have a regression plot with a blue (lsline) which I want to eliminate it but I don't know how.
plotregression(y.testTargets, y.outputs)

If you want to remove just the blue line you can use findall to retrieve the handle to the line object and then delete it. Also based on #rayreng's feedback, I made it so that the line is automatically removed from the legend as well.
r = plotregression(rand(5,1), rand(5,1));
%// Make the legend dynamic before removing the line
legend('-DynamicLegend', 'Location', get(legend, 'Location'));
%// Remove the blue line (with the "Fit" label)
delete(findall(r, 'DisplayName', 'Fit'));

Related

How to prevent the legend from updating in R2017a and newer?

Since MATLAB R2017a, figure legends update automatically when adding a plot to axes. Previously, one could do this:
data = randn(100,4);
plot(data)
legend('line1','line2','line3','line4')
hold on
plot([1,100],[0,0],'k-')
to plot four data lines with a legend, and then add a black line for y=0. However, since R2017a, this leads to the black line being added to the legend, with the name "data1".
How do I prevent this line from being added to the legend, so that the code behaves like it did in older versions of MATLAB?
The only solution I have found so far on Stack Overflow is to remove the legend item after it has been added. The syntax is not pretty:
h = plot([1,100],[0,0],'k-'); % keep a handle to the added line
set(get(get(h,'Annotation'),'LegendInformation'),'IconDisplayStyle','off');
The release notes for MATLAB R2017a mention this change, and provide 4 different ways of handling the situation. These two methods are easiest to put into existing code:
1: Turn off auto updating for the legend before adding the black line. This can be done at creation time:
legend({'line1','line2','line3','line4'}, 'AutoUpdate','off')
or after:
h = findobj(gcf,'type','legend');
set(h, 'AutoUpdate','off')
You can also change the default for all future legends:
set(groot,'defaultLegendAutoUpdate','off')
2: Turn off handle visibility for the black line that you don't want added to the legend:
plot([1,100],[0,0],'k-', 'HandleVisibility','off')
The IconDisplayStyle method is also shown here. However, they use the dot notation, which makes the syntax is a bit prettier:
h = plot([1,100],[0,0],'k-'); % keep a handle to the added line
h.Annotation.LegendInformation.IconDisplayStyle = 'off';

How can I multiple plot in one figure at Matlab?

Hi I'm trying to implement as following code.
plot(bins,r);
plot(bins,g);
plot(bins,b);
But I want to plot in one figure.
Is there any way?
For multiple plots in the same figure and not the same axis. You have to use subplot(x,y,z). The first argument 'x' is the number of plot you want to produce, in your case 3. Second 'y' just adjusts the size of the plots, you can use 1. The third 'z' is the position of the plot, whether a certain plot comes first, second or third.
subplot(3,1,1)
plot(bins,r);
subplot(3,1,2)
plot(bins,g);
subplot(3,1,3)
plot(bins,g);
To distinguish between all three plot you can add another argument to plot() so that you can change colors. For example:
plot(bins,r,'r')
'r' will make the color of the plot red, 'b' makes it blue, 'k' makes it black...so on.
Yes, you can plot everything in one go:
plot(bins,r,bins,g,bins,b)
or use hold on after the first call to plot.
You need to use hold on
hold on retains plots in the current axes so that new plots added to
the axes do not delete existing plots. New plots use the next colors
and line styles based on the ColorOrder and LineStyleOrder properties
of the axes. MATLAB® adjusts axes limits, tick marks, and tick labels
to display the full range of data.
hold on
plot(bins,r)
plot(bins,g)
plot(bins,b)

Remove Objects from Legend When You Have Also Used Fit, Matlab

I've plotted data points and fitted an exponential curve between them using 'fit' in Matlab. The problem is that with the fit-function I got the fitted line I wanted to plot, but also extra markers on top of my regular markers. I solved this problem by plotting the wanted markers on top of the unwanted so that they couldn't be seen. Now to the problem. When I want to show the legends those dots are also in there. How can I remove the markers from the legend without removing the fitted line since both of them are hidden inside the fit-function? Can I stop 'fit' from plotting the unwanted markers? So, I want to remove the blue dot called 'hoff' in the picture below.
You can leave out legend-entries by manually leaving out the handles of the lines, that you dont want to be in the legend. Try this:
%if you plot something, that you want showing up in the legend, save its handle:
h_line = plot(x,y)
%you dont want to show up this line? dont do anything, just plot it:
plot(myMarker)
%then set the legend-> you can add the text for your legend-entries with
%a cell-array containing the strings you want to show up:
legend([h_line another_line],{'Text1' 'Text2'})
with the example (see comments) I came to this solution:
close all
X=[1:10];
Y=X*0.5+0.1;
ft = fittype('poly2');
f = fit(X', Y',ft);
ha=plot(f)
hold on
hc=plot(X,Y)
hb=errorbar(X, Y, X*0.1, 'squarek','MarkerFaceColor','k','markersize',5)
hleg1 = legend([ha hc],{'hnh','ghg'});
-> this is just about splitting the plot-command. Hope that helps...
the result should look like this:

Color circle at end of stem in stem plot depending on value

I have a script that plots a Fourier series using stem(). I know that I can fill the circle at the end of the stems with stem( , 'fill'). However I would like it to only fill the circle if the magnitude is negative, or alternatively, fill all of them, but have an outer circle of another color on those that have a negative magnitude.
I tried using a variable inside stem(), and also a full if-else statement, but both returned errors.
Any way this can be done?
You can remove marker from stem plot with 'Marker' property set to 'none'. Then add markers where ever you like with plot or scatter function. Add hold on after the stem command and hold off after all plotting commands.
For example:
x = randn(20,1);
stem(x,'Marker','none')
hold on
plot(find(x>0),x(x>0),'ro')
plot(find(x<0),x(x<0),'bx')
hold off

MATLAB: Assign multiple colors to text in legend

I'm trying to color code text in a legend. (Since I'm trying to sort several plots into different categories, I can't just rely on the line colors in the legend.) I've managed to set the text color for the entire legend, but I can't manage to assign it line by line. Is this possible?
Code so far:
list={'Label 1','Label 2','Label 3'};
leg=legend(list);
set(leg,'Textcolor',[1 0 0])
sets the text color for the entire legend as red. I'd like to be able to make some red, and some black. I tried assigning the color array as an n x 3 matrix, but MATLAB doesn't like that very much. I also poked around the legend properties using get(leg), but I couldn't find anything else that seemed useful. Any suggestions?
While the answers by yuk and gnovice are correct, I would like to point out a little-known and yet fully-documented fact that the legend function returns additional handles that correspond to the legend components. From the documentation of the legend function:
[legend_h, object_h, plot_h, text_strings] = legend(...) returns
legend_h — Handle of the legend axes
object_h — Handles of the line, patch, and text graphics objects used in the legend
plot_h — Handles of the lines and other objects used in the plot
text_strings — Cell array of the text strings used in the legend
These handles enable you to modify the properties of the respective objects.
Here is the code:
legtxt=findobj(leg,'type','text');
set(legtxt(1),'color','k')
Just find out which legends correspond to which index.
To change the legend text colors individually, you have to first get the handles to the text objects, which are children of the legend object. Then you can change their text colors separately. Here's an example of how you can do it:
plot(1:10, rand(1, 10), 'r'); % Plot a random line in red
hold on;
plot(1:10, rand(1, 10), 'b'); % Plot a random line in blue
hLegend = legend('a', 'b'); % Create the legend
hKids = get(hLegend, 'Children'); % Get the legend children
hText = hKids(strcmp(get(hKids, 'Type'), 'text')); % Select the legend children
% of type 'text'
set(hText, {'Color'}, {'b'; 'r'}); % Set the colors
Note that the color order in the last line is blue then red, in reverse order of the way the labels are passed to the legend function. The above will give you the following plot: