Matlab: How to plot without color graduation? - matlab

My friend has made a script to handle a lot of data and plot various plots, however when he runs the script, the graphs have different colors according to the legend and when I do it, all graphs are similar with a color graduation from black to red! (see picture) Why does it differ and how do I get the graphs in different colors?
2D plot with color graduated graphs:-
figure
hold on
plot(sentar_7.created_at, sentar_7.acc_diff, '-');
plot(sentar_7.created_at, sentar_7.stand_toggle, '-');
plot(sentar_7.created_at, sentar_7.state, '-');
title('Sentar 7 acc')
xlabel('tid')
ylabel('dist [cm]')
legend('acc diff','stand toggle', 'state')
hold off

This is a known issue with certain Intel graphics drivers. One potential workaround is to use basic hardware rendering:
opengl hardwarebasic
You could also disable the AlignVertexCenters property of the line object:
h = findall(gca, 'type', 'line')
set(h, 'AlignVertexCenters', 'off')
The best solution may be to update your graphics drivers to the 4380 version.

Related

How to 'un' bold titles for MATLAB figures?

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').

Matlab - highlight a specific interval using patch

I would like to highlight a specific interval in my plot. I found a way using the patch object.
The problem is that my layout gets messy whenever i use it.
Here's an example of a plot:
x = 1:0.05:25;
plot(x,sin(x)+sin(100*x)/3);
xlabel('x axis');
ylabel('y axis');
title('\Omega \Delta test');
legend('sin \Sigma')
And to highlight a period:
yl = ylim;
patch([5 5 10 10],[yl(1) yl(2) yl(2) yl(1)],'k',...
'facecolor',[.4 .6 .4],'edgecolor',[.4 .6 .4],...
'facealpha',0.3,'edgealpha',0.3)
My results with and without the patch command:
Normal:
Messy:
Look at the fonts and the legend block. Any ideas on how to fix that?
Is there a better way to highlight an interval? I need to choose the color and set transparency.
Just one more question: Why do I have to use the third input (color) if it's not applied?
Thanks in advance!
Edit: This answer is only valid for Matlab versions before 2014b, as the incredibly useful erasemode property has been removed from all HG2 graphic objects on later Matlab versions.
I ran into this problem countless times and I had to learn to live with it. Most times I can accept the glitches of the OpenGL renderer if it buys me nice transparency effects, but in some cases it is just not acceptable.
I use patch objects to highlight intervals in many applications, usually over several curves. There is a trick you can use when transparency is not an option, it is the EraseMode property of the patch object. If you set the EraseMode property to 'xor' the patch will not be transparent, yet anything under the patch will xor the patch pixel colours so you can still see the curves under the patch.
This not being a transparency rendering, you can use the default painter renderer and avoid all the occasional glitches of the OpenGL.
So for example with your data:
hp = patch([5 5 10 10],[yl(1) yl(2) yl(2) yl(1)],'k',...
'facecolor','g','edgecolor','g',...
'erasemode','xor') ;
And the nice advantage of this trick is it works with monochrome display/prints. If you cannot use multiple colours, you can use that with only one colour (if you plan black & white printing for publication for example)
hpx = patch([5 5 10 10],[yl(1) yl(2) yl(2) yl(1)],'b',...
'facecolor','b','edgecolor','b',...
'erasemode','xor') ;
(Note: This answer only makes sense if you're using the OpenGL renderer.)
If that's the case, then this is one symptom of
a bug in MATLAB 6.5 (R13) and later versions in the way that axes are rendered in hardware accelerated OpenGL, when transparency is used
as mentioned here.
The simplest workaround is to force OpenGL to run in software mode by including the line:
opengl software
Apparently, this only works under Windows and Linux. For other symptoms of the problem and different approaches to solutions have a look at this related question.
Regarding your other questions:
Not sure if it's a better way, but it's another option: You could use an area-plot like this:
opengl software
figure
x = 1:0.05:25;
ax = plot(x,sin(x)+sin(100*x)/3);
hold on
basevalue = -2;
areaAx = area([5,10],[2,2], basevalue);
set(areaAx, 'FaceColor', [.4 .6 .4])
alpha = get(areaAx, 'Children');
%make area plot transparent
set(alpha, 'FaceAlpha', 0.5);
set(areaAx, 'EdgeColor', 'none');
xlabel('x axis');
ylabel('y axis');
title('\Omega \Delta test');
legend('sin \Sigma')
xlim('auto')
ylim('auto')
hold off
However, OpenGL renderer will also cause problems here, so it's not a fix, just another option.
And finally, I'm not sure what you mean by the color not being used. The 'facecolor' is used, that's why your plot is green. When you go through the area-option above step by step you'll see that you get a solid color first. Later on, transparency is added with the 'facealpha'-option.

decouple colorbar from figure matlab

Is it possible to change the color of the colorbar without changing the color of the figure?
figure_1 = figure;
j1= bar(rand(2,10),'stacked');
colormap(winter)
htx = colorbar('SouthOutside');
colormap(jet)
In particular I would like to have the object in the figures colored with the colormap winter and the colorbar following the colormap jet...is it possible to decouple the 2?
There are two FileExchange functions that I've used for this purpose with satisfactory results. freezeColors, which is mentioned in the link that #Benoit_11 gave in his comment will let you have two or more subplots in one figure with different colormaps, but the colorbars have to be difficult, and freezeColors ignores them. For that you need COLORMAP and COLORBAR utilities, and in particular cbfreeze which will fix the colorbar and keep it from changing.
So your code would look like this:
figure_1 = figure;
j1= bar(rand(2,10),'stacked');
colormap(winter)
freezeColors;
htx = colorbar('SouthOutside');
colormap(jet)
cbfreeze(htx);

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.

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);