decouple colorbar from figure matlab - 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);

Related

make different subplot have the same range of colors on matlab

I'm trying to plot several data on subplots, but each one have its own range of color (colorbar) and I want all to be the same. I can change the labels of the colorbar but if ranges are too different it would mostly show only one color. Is there a way to make matlab think that its all just one plot and then decide the colorbar scale accordingly?
To change colorbar label its quite easy:
allaxes = findall(f, 'type', 'axes');%f is the figure handle
for i=1:length(allaxes)
cc=get(allaxes(i),'Colorbar');
mm(i,:)=get(cc,'Limits');
end
limits=[min(min(mm)) max(max(mm))];
for i=1:length(allaxes)
cc=get(allaxes(i),'Colorbar');
set(cc,'Limits',limits);
end
Here there is an example (look for the one on the right):

Matlab multiple stacked plots

I've never seen a plot like the following (the plot in (a) ). Is it even possible?
According to the profile page of #Ander Biguri
Matlab can even make your dinner, if you know how to use it.
Which answers the question, if this is even possible ;-)
All we need is basic knowledge of the axes command - the rest is just tweaking to make it look nice. Let's have a look at it:
We'll start off by creating some sample data:
t = 100:220;
x1 = -(10*(t-130)).^2;
x2 = -(10*(t-150)).^2;
x3 = -(10*(t-170)).^2;
Then we'll create an initial figure with a white background
fig = figure(1);
set(fig,'Color','w');
Now we can create a new axes object and plot x1 on it:
ax(1) = axes('Position',[0.1,0.1,0.6,0.6]);
plot(ax(1),t,x1+10^4*rand(size(x1)),'-k',t,x1,'-r');
We'll remove the box around the axes, so only the x- and y-axes remain. Further we resize the plot, so we'll have enough space for the other two plots. We also set the color to none, i.e. transparent.
set(ax(1),'Color','none');
set(ax(1),'Box','off');
set(ax(1),'Position',[0.1,0.1,0.6,0.6]);
Now we need to create the second graph. We'll just create another axes object at a position which we like:
ax(2) = axes('Position',[0.2,0.2,0.6,0.6]);
plot(ax(2),t,x2+10^4*rand(size(x2)),'-k',t,x2,'-r');
set(ax(2),'Color','none');
set(ax(2),'Box','off');
and so on:
ax(3) = axes('Position',[0.3,0.3,0.6,0.6]);
plot(ax(3),t,x3+10^4*rand(size(x3)),'-k',t,x3,'-r');
set(ax(3),'Color','none');
set(ax(3),'Box','off');
And simple as that, we get something that doesn't even look that bad:
Using multiple waterfall plots, as Horchler suggested:
%// create some sample data
t=10:20:110;
x=0:1:200;
Y=bsxfun(#(x,t) normpdf(x,t,20),x,t.'); %//' fix the code formatting on SO!!
%// Make a colormap to to set the colour of the lines
colormap([1 0 0;0 0 0]);caxis=[0 1];
%// Plot the first set of lines (red ones)
h1=waterfall(x,t,Y,zeros(size(Y)));
set(h1,'FaceColor','none','LineWidth',2) %// tweak the properties
hold on
%// Plot the second set of lines (black lines), just the red lines with some noise
h2=waterfall(x,t,Y+0.002*(rand(size(Y))-0.5),ones(size(Y)));
set(h2,'LineWidth',2)
hold off
view([16 28])
we can get this:

Setting alpha of colorbar in MATLAB R2014b

I have a colorplot (from imagesc) with an alpha map. I'd like the colorbar to reflect the alpha (notice that in the image below the colormaps are the same). I found solutions online but none seem to work in R2014b.
Code is here:
subplot(2,1,1)
A = imagesc(meshgrid(0:10,0:5));
alpha(A,1)
colorbar
subplot(2,1,2)
B = imagesc(meshgrid(0:10,0:5));
alpha(B,.7)
colorbar
James
You could add a textbox with alpha on top of the colorbar. This works for later versions of MATLAB.
cb=colorbar
annotation('textbox',...
cb.Position,...
'FitBoxToText','off',...
'FaceAlpha',0.5,...
'EdgeColor',[1 1 1],...
'BackgroundColor',[1 1 1]);
In pre-R2014b MATLAB, the colorbar is itself an axis containing an image for which you can set alpha:
hb = findobj(gcf,'Type','axes','Tag','Colorbar');
hi = findobj(hb,'Type','image');
alpha(hi,0.7)
Instead of gcf, use the handles of the individual subplots.
Or save its handle when you make it:
hb = colorbar;
From R2014b on, the colorbar is creating using the new handle graphics system, where there is no longer a child image to modify. The colorbar is created internally with colorbarHGUsingMATLABClasses, which is an obfuscated .p file, so it's no clear how it's constructed.

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 plot graphs above each other in Matlab?

I want to plot two (or more) graphs in Matlab. I want them to be aligned one above the second one above the third and so on. However subplot is not the case for me. I also don't want them both to be on the same graph in different colors, just to be one above the other. Is it possible? I'm searching for such a command but without any success.
Thanks in advance for hints!
If what you mean by "one above the other" is one "on top of another" then I think what You need to use is the axes command. Having said that, in order to have your plot be readable, I think more than two scales in the same plot makes it pretty hard to read.
This Matlab's help page shows you how to do it, the gist of it is shown in the following script:
y2 = 1000.*rand(100,1);
x2 = 0:99;
% Plot the first data set
hl1 = line(x1,y1,'Color','r');
% Get the axes and configure it
ax1 = gca;
set(ax1,'XColor','r','YColor','r')
%Create the new axes
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k');
% Plot the second data set with the new axes
hl2 = line(x2,y2,'Color','k','Parent',ax2);
The above script creates the following plot with one set of axes in red and another in black.
Hope this helps.