9

I am trying to make a graph with two different contourf subplots that use completely different colormaps. However with the code I currently have (which creates a custom colormap for one of the subplots), the subplots come out with the same colormap. Any ideas how to fix this?

h = figure;
subplot(2,1,1)
title('GEFS 20 Member Mean Vorticity');
axesm('eqdcylin','maplonlimit',[-180 179],'maplatlimit',[0 90]);
%eqdcylin
contourm(gLat, gLon, squeeze(meanhx(x,:,:))', 16,'Color',[0.05 0.05 0.05],'LineWidth',2);
hold on
contourfm(gLat, gLon, squeeze(vmeanx(x,:,:))', 30, 'LineStyle', 'none'); 
shading flat;
lm=worldlo('POline');
  for i=1:length(lm);
  lm(i).otherproperty = {'color','m','LineWidth',1.5};
  end
displaym(lm);
gridm on;
tightmap;
set(h, 'Position', [1 1 2200 1100]);
colormap(b2r(-5*10^-5, 5*10^-5));
freezeColors;
cbfreeze(colorbar)




%caxis([-5*10^-5 5*10^-5])

colorbar;


subplot(2,1,2)
title('GEFS 20 Member Vorticity Variance');
axesm('eqdcylin','maplonlimit',[-180 179],'maplatlimit',[0 90]);
%eqdcylin
contourm(gLat, gLon, squeeze(meanhx(x,:,:))', 16,'Color',[0.05 0.05 0.05],'LineWidth',2);
hold on
contourfm(gLat, gLon, squeeze(vvarx(x,:,:))', 30, 'LineStyle', 'none'); 
shading flat;
lm=worldlo('POline');
  for i=1:length(lm);
  lm(i).otherproperty = {'color','m','LineWidth',1.5};
  end 
displaym(lm);
gridm on;
tightmap;
set(h, 'Position', [1 1 2200 1100]);

mycmap = [
0.9961    0.9961    0.9961;
0.6641    0.6641    0.9974;
0.3320    0.3320    0.9987;
     0         0    1.0000;
     0    0.2500    1.0000;
     0    0.5000    1.0000;
     0    0.7500    1.0000;
     0    1.0000    1.0000;
0.2000    1.0000    0.8000;
0.4000    1.0000    0.6000;
0.6000    1.0000    0.4000;
0.8000    1.0000    0.2000;
1.0000    1.0000         0;
1.0000    0.9333         0;
1.0000    0.8667         0;
1.0000    0.8000         0;
1.0000    0.7333         0;
1.0000    0.6667         0;
1.0000    0.6000         0;
1.0000    0.5333         0;
1.0000    0.4667         0;
1.0000    0.4000         0;
1.0000    0.3333         0;
1.0000    0.2667         0;
1.0000    0.2000         0;
1.0000    0.1333         0;
1.0000    0.0667         0;
1.0000         0         0;
0.9854         0         0;
0.9708         0         0;
0.9561         0         0;
0.9415         0         0;
0.9269         0         0;
0.9123         0         0;
0.8977         0         0;
0.8830         0         0;
0.8684         0         0;
0.8538         0         0;
0.8392         0         0;
0.8246         0         0;
0.8099         0         0;
0.7953         0         0;
0.7807         0         0;
0.7661         0         0;
0.7515         0         0;
0.7368         0         0;
0.7222         0         0;
0.7092         0         0;
0.6961         0         0;
0.6830         0         0;
0.6699         0         0;
0.6569         0         0;
0.6438         0         0;
0.6307         0         0;
0.6176         0         0;
0.6046         0         0;
0.5915         0         0;
0.5784         0         0;
0.5654         0         0;
0.5523         0         0;
0.5392         0         0;
0.5261         0         0;
0.5131         0         0;
0.5000         0         0;
];

colormap(mycmap);






freezeColors;
cbfreeze(colorbar);


set(gcf, 'renderer', 'zbuffer');
agold2121
  • 103
  • 1
  • 6
  • not minimal nor reproducible; did you go through the examples in freezeColors? – rozsasarpi Nov 12 '14 at 17:24
  • Sure did, how can I make this minimal and reproducible. – agold2121 Nov 12 '14 at 17:27
  • [This](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) is related to R, but equally valid for Matlab and any programming question. A google search would give you hundreds of hits.. Additionally take a look at SO's [help](http://stackoverflow.com/help/how-to-ask). – rozsasarpi Nov 12 '14 at 17:31
  • I have repeatedly searched google, and continue to come back with the same issue as there does not seem to be a solution that directly solves this problem. Otherwise I wouldn't be asking here. Additionally, I'm not sure what exactly it is that makes this not reproducible, there is nothing here that is not reproducible, and the code is simply a cluttered (with mapping lines) contourf plot. – agold2121 Nov 12 '14 at 17:38
  • It is not so simple to solve, have a look here for a solution: http://stackoverflow.com/a/39741044/2627163 and if you can make a sand-alone code that I can run, I will add 2 colormaps to it. – EBH Nov 05 '16 at 23:20

3 Answers3

3

EDIT: it seems, that the documentation is wrong! See comments!

By using the second argument of the colormap function, one should be able to assign a specific colormap to a specific (sub-)plot or axes, if you want to call it like that:

Referring to TMW: colormap:

Be aware that the first argument is the handle to the axes!

colormap(ax,map)

sets the colormap for the axes specified by ax. Each axes within a figure can have a unique colormap. After you set an axes colormap, changing the figure colormap does not affect the axes.

How to get the handle of the axes?:

when plotting with plot(x,y..) you get it as the return-value. Catch it with:

ax = plot(x,y..)

For other plot-functions, as you seem to use, you should find some info about that within the doku.

Lucius II.
  • 1,832
  • 12
  • 19
  • this is wrong. The question is about multiple colormaps in a subplot. The above only works when each plot is in a different figure. Even if you get the axis handle for a single subplot, setting its colormap will change the colormap of all other axis in the figure. – carandraug Aug 26 '15 at 20:15
  • gosh, it seems that you are right. I just got this form the documentation. Did I got this wrong?-> "Each axes within a figure can have a unique colormap" or is there something wrong with the documentation..? :o – Lucius II. Aug 27 '15 at 09:02
  • 1
    apparently yes. It's very easy to test, `data = randi (64, 10); hax1 = subplot (1, 2, 1); image (data); colormap (hax1, jet); hax2 = subplot (1, 2, 2); image (data); colormap (hax2, hot);` Mathwork's solution for this is an [absolute joke](http://www.mathworks.com/matlabcentral/answers/101346-how-do-i-use-multiple-colormaps-in-a-single-figure). You must create a colormap that is the concatenation of each subplot colormap and adjust the values on each accordingly. And if you want colorbars, you need to set their ranges manually too. – carandraug Aug 27 '15 at 11:58
2

If you upgrade to MATLAB 2017a, you can assign a colormap to each axes object using the syntax colormap(axesHandle, cMap)

user2831602
  • 138
  • 6
0

If you have the Image Processing toolbox, you can use the function subimage to display images with their respective colormaps:

X1=imread('http://upload.wikimedia.org/wikipedia/commons/5/5c/Burosch_Blue-Only_Test_pattern.jpg');
X2=imread('http://upload.wikimedia.org/wikipedia/commons/e/ea/Romsey_River_Test.jpg');
subplot(1,2,1), subimage(X1)
subplot(1,2,2), subimage(X2)

Result:

two images in one figure

Edit there is a more complete answer to this question here.

Community
  • 1
  • 1
Cape Code
  • 3,584
  • 3
  • 24
  • 45