How to 'un' bold titles for MATLAB figures? - matlab

I'm trying to combine a few Matlab plots into one figure and therefore I'm wondering how I can create 'normal' tiles above my plots instead of the bold titles provided by Matlab. Below an example.
figure
plot((1:10).^2)
title({'First line';'Second line'})

Make use of the 'FontWeight' argument:
figure
plot((1:10).^2)
title({'First line';'Second line'},'FontWeight','Normal')
Note also that you can access the 'FontWeight' argument for all text objects in your figure in one go---in case you have, e.g., several subplots in your figure---using findall:
myFig = figure;
subplot(2,1,1)
plot((1:10).^2)
title('First plot')
subplot(2,1,2)
plot((1:10).^2)
title('Second plot')
% Set 'Normal' font weight in both titles above
set(findall(myFig, 'Type', 'Text'),'FontWeight', 'Normal')
As stated in the comments above; for a single figure title, you can make use make use of \rm as an alternative. Note however that \rm depends on the (default) choice of 'Interpreter' as 'tex', whereas the approach above is valid for all choices of interpreter (however with no effect for text objects using interpreter 'latex').

Related

Hide MATLAB legend entries for some graphical objects in plots

MATLAB legends list everything in a plot, including guidelines that you have put on a plot.
A fudge to get around that is to do
*Plot
*Add legend
*Add guidelines
However, MATLAB puts the most recent lines in the front, meaning the guidelines then sit over the displayed data; ugly and distracting.
Similar problems occur any time you build up a complicated plot, legend freaks out and grabs everything, and workarounds with plotting order can be ugly
Example code:
%**** Optional guidelines
figure(1)
plot([2 2],[0,1],'k--'); hold on
%**** DATA
N = 4;
y=rand(5,N);
x=1:1:5;
for plotLoop=1:N;
%* Plot
figure(1)
plot(x,y(plotLoop,:));
hold on
end
%*****LEGEND
hLegend = legend(LegTxt,...
'interpreter','latex',...
'location','eastoutside')
(move the code block order to replicate the situations mentioned above)
How to reasonably fix this?
If you want a certain graphics object to not produce a legend (and that will work even if you toggle the legend off and on again), you can modify the LegendInformation:
%# plot something that shouldn't show up as legend
handleWithoutLegend = plot(something);
%# modify the LegendInformation of the Annotation-Property of the graphical object
set(get(get(handleWithoutLegend,'Annotation'),'LegendInformation'),...
'IconDisplayStyle','off');
%# toggle legend on and off at will, and never see the something-object appear
If you try to turn off the legend on an array of handles, the best way is just to loop over them, with a try-wrapper for graphical objects that cannot produce a legend:
for h = listOfHandles(:)'
try
set(get(get(h,'Annotation'),'LegendInformation'),...
'IconDisplayStyle','off');
end
end
Craft a custom handle that you feed into the legend. Plot handles can be concatenated to form an object that legend is happy to accept as input.
The required code isn't pretty, but it does work.
%**** Optional guidelines for periodicity
figure(1)
plot([2 2],[0,1],'k--'); hold on
%**** DATA
N = 4;
y=rand(5,N);
x=1:1:5;
for plotLoop=1:N;
LegTxt{plotLoop} = num2str(plotLoop);
%* Plot
figure(1)
% if statement to construct a handle for the legend later
if plotLoop==1
htot=plot(x,y(plotLoop,:));
else
h=plot(x,y(plotLoop,:));
% Append this info to the figure handle
htot= [htot, h];
end
hold on
end
%*****LEGEND
hLegend = legend(htot,LegTxt,...
'interpreter','latex','FontSize',16,...
'location','eastoutside')
For the pedantic or curious, the loop for plotLoop=1:N; is here because I extracted the example from some rather complex code where the data is extracted from cell arrays. Obviously you could eliminate that loop for a lot of usage scenarios, I just decided to keep the code in its most flexible format!
You can also hide plot from legend in another way. Here's the sample:
figure(1)
hold on
x=1:10;
y1=x;
y2=x.^2/10;
y3=x.^3/100;
plot(x,y1);
plot(x,y2,'HandleVisibility','off');
plot(x,y3);
legend('x','x^3')
You just need to put 'HandleVisibility', 'off' to your plot that you don't want to show up in legend. That's how result looks like:
HandleVisibility is a line property so it might now work if you create plot in some other way. But for most use cases its enough and it is much simpler.

Different color line with plotpc in matlab

I am plotting two plotpc charts (i want to see decision boundaries from perceptron and from Bayesian net) and I need them to have different color.
plotpv(P,T);
hold all;
plotpc(net.IW{1,1},0,'r');
plotpc([w1(maxind(1)), w2(maxind(2))],0,'g');
title('Decision boundaries');
However all my trials ended up with failure and I always get same colors like this:
Thank you for help.
You need to assign the output of plotpc (line handle) to some variable, which you can then use to alter line appearance, e.g.
hPlot = plotpc(net.IW{1,1},0);
set(hPlot, 'Color', 'r');
Also, I don't think plotpc accepts a color as a third argument - you should get a warning when you do that.

Using multiple colormaps on same axis with ezsurf

I'm trying to plot several symbolic functions in the same subplot. Using ezsurf makes it easy to plot symbolic functions, but it doesn't seem to be very configurable.
I can't colour the individual graphs (see the figure below). They all automatically take the normal jet colourmap. How can they use different colormaps?
edit: Here's the new code I'm running using freezeColors as suggested in the answer:
subplot(2,2,4)
for i = 1:numClasses
f(i) = M_k(i)/sum(M_k);
freezeColors
hold on
ezsurfc(x,y,f(i), [0 max(mean(1:numClasses))*2 0 max(mean(1:numClasses))*2],l)
switch i
case 1
colormap(copper)
case 2
colormap(bone)
case 3
colormap(spring)
case 4
colormap(hsv)
otherwise
colormap(jet)
end
hold off
unfreezeColors
alpha(0.7)
end
And this produces the image shown below:
Why are the colormaps still not different?
Since changing the color map of one axes in a figure via colormap changes it for all axes in the figure, you need to use a workaround to get different color maps in your individual subplots. The MathWorks article "Using multiple colormaps in a single figure" lists three methods:
Combine multiple colormaps into one, and use different portions of the concatenated map for different axes (this only works for images)
Use subimage if you have the Image Processing Toolbox (again, only for images)
The freezeColors File Exchange submission, which can hold any plots colormap.
The basic usage of freezeColors is similar to hold. For plots on different axes:
subplot(1,2,1)
ezsurf('sqrt(x^2 + y^2)')
colormap(jet)
freezeColors % submission by John Iversen
subplot(1,2,2)
contour(peaks,30)
colormap(copper)
For plots on the same axes:
surf(peaks) % jet
freezeColors
hold on
mesh(peaks')
colormap(copper)
Output:
NOTE: You have to call freezeColors repeatedly after each plot (surf, mesh, etc.).
NOTE 2: Do not use unfreezeColors (e.g. in a plotting loop) unless you want to revert to using the same color maps. This fixed the second question added in the edit to the question.

MATLAB - How to zoom subplots together?

I have multiple subplots in one figure. The X axis of each plot is the same variable (time). The Y axis on each plot is different (both in what it represents and the magnitude of the data).
I would like a way to zoom in on the time scale on all plots simultaneously. Ideally by using the rectangle zoom tool on one of the plots, and having the other plots change their X limits accordingly. The Y limits should remained unchanged for all of this. Auto fitting the data to fill the plot in the Y direction is acceptable.
(This question is almost identical to Stack Overflow question one Matplotlib/Pyplot: How to zoom subplots together? (except for MATLAB))
Use the built-in linkaxes function as follows:
linkaxes([hAxes1,hAxes2,hAxes3], 'x');
For more advanced linking (not just the x or y axes), use the built-in linkprop function
Use linkaxes as Yair and Amro already suggested. Following is a quick example for your case
ha(1) = subplot(2,1,1); % get the axes handle when you create the subplot
plot([1:10]); % Plot random stuff here as an example
ha(2) = subplot(2,1,2); % get the axes handle when you create the subplot
plot([1:10]+10); % Plot random stuff here as an example
linkaxes(ha, 'x'); % Link all axes in x
You should be able to zoom in all the subplots simultaneously
If there are many subplots, and collecting their axes handle one by one does not seem a clever way to do the job, you can find all the axes handle in the given figure handle by the following commands
figure_handle = figure;
subplot(2,1,1);
plot([1:10]);
subplot(2,1,2);
plot([1:10]+10);
% find all axes handle of type 'axes' and empty tag
all_ha = findobj( figure_handle, 'type', 'axes', 'tag', '' );
linkaxes( all_ha, 'x' );
The first line finds all the objects under figure_handle of type "axes" and empty tag (''). The condition of the empty tag is to exclude the axe handles of legends, whose tag will be legend.
There might be other axes objects in your figure if it's more than just a simple plot. In such case, you need to add more conditions to identify the axes handles of the plots you are interested in.
To link a pair of figures with linkaxes use:
figure;imagesc(data1);
f1h=findobj(gcf,,’type’,’axes’)
figure;imagesc(data2);
f2h=findobj(gcf,,’type’,’axes’)
linkaxes([f1h,f2h],’xy’)

How to create a new figure in MATLAB?

Usually when I plot in MATLAB, it always draws on the same figure. How do I make it draw in a new figure?
I know it is pretty elementary, but I'm not finding it using Google Search.
figure;
plot(something);
or
figure(2);
plot(something);
...
figure(3);
plot(something else);
...
etc.
While doing "figure(1), figure(2),..." will solve the problem in most cases, it will not solve them in all cases. Suppose you have a bunch of MATLAB figures on your desktop and how many you have open varies from time to time before you run your code. Using the answers provided, you will overwrite these figures, which you may not want. The easy workaround is to just use the command "figure" before you plot.
Example: you have five figures on your desktop from a previous script you ran and you use
figure(1);
plot(...)
figure(2);
plot(...)
You just plotted over the figures on your desktop. However the code
figure;
plot(...)
figure;
plot(...)
just created figures 6 and 7 with your desired plots and left your previous plots 1-5 alone.
The other thing to be careful about, is to use the clf (clear figure) command when you are starting a fresh plot. Otherwise you may be plotting on a pre-existing figure (not possible with the figure command by itself, but if you do figure(2) there may already be a figure #2), with more than one axis, or an axis that is placed kinda funny. Use clf to ensure that you're starting from scratch:
figure(N);
clf;
plot(something);
...
As has already been said: figure will create a new figure for your next plots. While calling figure you can also configure it. Example:
figHandle = figure('Name', 'Name of Figure', 'OuterPosition',[1, 1, scrsz(3), scrsz(4)]);
The example sets the name for the window and the outer size of it in relation to the used screen.
Here figHandle is the handle to the resulting figure and can be used later to change appearance and content. Examples:
Dot notation:
figHandle.PaperOrientation = 'portrait';
figHandle.PaperUnits = 'centimeters';
Old Style:
set(figHandle, 'PaperOrientation', 'portrait', 'PaperUnits', 'centimeters');
Using the handle with dot notation or set, options for printing are configured here.
By keeping the handles for the figures with distinc names you can interact with multiple active figures. To set a existing figure as your active, call figure(figHandle). New plots will go there now.
Another common option is when you do want multiple plots in a single window
f = figure;
hold on
plot(x1,y1)
plot(x2,y2)
...
plots multiple data sets on the same (new) figure.
As simple as this-
figure, plot(yourfigure);