move legend position in an empty subplot - matlab

I have a figure with 5 subplots. I declare subplot(2,3,X) which includes 6 subplots. The 6th subplot is empty. I am going to move legend to 6th empty position for all plots.
How is it possible?

if you just want to use standard-matlab, you need the handle of the subplot and then you need its position. Then you set the position of the legend to the position of the subplot.
Referring to the docs:
Note You can set the legend location by passing the 4–element position vector to the legend function using the ‘Location' option. To define the position of an existing legend, use the set function to assign the 4–element position vector to the ‘Position' property. You cannot use the Location option with the set function
for example:
subplot(2,3,1), plot(1:10,2:11)
myLegend=legend('text1')
set(myLegend,'Units', 'pixels')
myOldLegendPos=get(myLegend,'Position')
hold on
h=subplot(2,3,6)
set(h,'Units', 'pixels')
myPosition=get(h,'Position')
set(myLegend,'Position',[myPosition(1) myPosition(2) myOldLegendPos(3) myOldLegendPos(4)])

Maybe try legendflex from the File Exchange, it looks like it can do what you want.

Related

Colorbar properties

I am trying to plot a contour with a colorbar that has a small values (10e-9). This value appears at top of the colorbar. How can I change the location of this value from top to bottom of the colorbar. I want to move this value beneath the colorbar because when I added label above the colorbar it overlapped with this value
I attached image for the figure.
You can accomplish what you want by first finding the handle to the color bar, then changing the yticklabel ticklabels property. This is a cell array of stings, one for each tick mark. You can fill in whatever you want to show there. The multiplier at the top will go away. With the text function you can add your own modifier anywhere you want. But I think it looks nicer within the axis label.
However, the simple solution is to change the units you plot. Multiply everything by 10e9 before plotting, then add a nano prefix to your units.

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)

matlab multiple plots sizes

I have question regarding width and height of Plots. If we have multiple plots in same figure how can we set the size for each plot on our own.
Also is there a way to have multiple plots without using subplots?
Start with subplots (calling subplot returns you an axes handle). Then set the Position property on each subplot axes handle.
Create all the subplots before you start moving them, because creating a subplot afterwards will automatically remove any existing subplot that overlaps the default location of the new one.

How to draw vertical line on axes in Matlab GUI?

I have a Matlab GUI with 3 axes components. Their tags are predicted_ax, cost_ax and error_ax. I want to draw vertical line on particular position on the first axes component (the one with tag predicted_ax). How do I do that?
I tried this code:
ylim = get(handles.predicted_ax, 'ylim');
line([linePos, linePos], ylim);
But it draws the line on different axes (the ones with tag error_ax)! I'm sure I did not confuse tags or axes components. In the fact another test
ylim = get(handles.cost_ax, 'ylim');
line([linePos, linePos], ylim);
gives exactly the same result: the line is drawn on the last axes component with tag error_ax. So how do I draw the line on the right axes?
You need to set the 'parent' property of the line, as by default it will always be the current axis:
h = line([linePos, linePos], ylim);
set(h, 'parent', handles.predicted_ax);
I think you need to use the axes command to set the current axis on which the line will be drawn. Try axes(handles.predicted_ax); prior to your line command.
(Getting the ylim value for the axis apparently does not make it current.)

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