How to plot contours with selected colors and formatted labels - matlab

I'm trying to plot contour using my computed data with limited contour labels and and colors as given in the top panel of this image:
But I ended up with a slightly different plot (see the plot in the bottom of the above image).
I want to modify my plot with the following three specifications
Restrict contour labels in 2 or 3 decimal places
Remove plot labels in the area where the contours are too close to each other.
Plot with two colors as in the first image
Here is my code:
f=load('fort.15');
ngridx=180;
ngridy=180;
x=f(:,3);
y=f(:,4);
z=f(:,5);
xlin=linspace(min(x),max(x),ngridx);
ylin=linspace(min(y),max(y),ngridy);
[X,Y]=meshgrid(xlin,ylin);
Z=griddata(x,y,z,X,Y,'linear');
[c,h] = contour(X,Y,Z,20);
set(h,'LineWidth',2,'LineColor',rgb('SteelBlue'),'ShowText','on',...
'LabelSpacing',800 )
axis([0 6 -5 7])
I'm not an expert in Matlab. Please help me get the right plot.
I'm attaching my data file here.

Well, I got only 2 of 3. Deine the level in which the color has to change (here scl) and you good to go:
scl = 6.5; % switch color level;
[c1,h1] = contour(X,Y,Z,scl:max(Z(:)),'Color','r');
hold on
[c2,h2] = contour(X,Y,Z,min(Z(:)):scl,'Color','b');
clabel(c2,h2);
axis([0 6 -5 7])
The idea here is to builed your plot from two contour objects, using hold on command. the vector scl:max(Z(:)) define the levvels to show in the first contour, and the get the red color and no lables. And a similar logic works for the secound contour.
If you want to lable some red contours, or remove lables from the blue ones, you need to replace h2 in the clabel function with a vector of the levels you want to lable. If you will be mo specific in the comments I'll update my answer.
Changing the formatting of the lables, is probably possible somehow, but it's really not trivial, so I left it by now.

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

How to setup step colorbar in matlab?

I want to change the default color bar (jet color) generated by Matlab, especially the step of the color (just like the figure below). How to do that?
Here's my code
[hC hC] = contourf(interp2(sal,2,'spline'),[0:0.5:5]);
set(hC,'LineStyle','none','YTick',0:4);
colorbar;
If you are looking to reduce the number of colors in the contour plot and colorbar then you can set a new colormap with a reduce color set.
%Get 10 colors from jet
numColors = 10;
colormap(jet(numColors))
data = peaks;
contourf(data)
% Optionally you can set yTicks in conjunction with the number of items in your colormap to line up
colorbar('YTick',linspace(min(data(:)),max(data(:)),numColors+1))
EDIT:
If you want more control over where the contour lines are drawn then use the function in this form countourf(data,v) where v is an monotonically increasing vector of your desired contour levels. Example:
contourf(data,linspace(-7,8,numColors))
c = colorbar('YTick',linspace(-7,8,numColors+1));
The will draw 10 contour lines at -7, -5.33, -3.66 ... 8. Replace -7 and 8 with whatever you wish ex. min/max of data or whatever makes sense for your application
You can adjust the color bar properties by using:
c=colorbar;
c.Ticks=[vector of tick locations]
or alternately you could try
c.Limits=[min max]
See the MATLAB documentation for colorbar properties: http://www.mathworks.com/help/matlab/ref/colorbar-properties.html?refresh=true
this explains color bar customization in more detail

plotting a text file with 4 columns in matlab

I want to plot a text file with 4 columns that first column in longitude,second in latitude, third is depth and forth is amount of displacement in each point.(it's related to a fualt)
-114.903874 41.207504 1.446784 2.323745
I want a plot to show the amount of displacement in each point (like images that we plot with imagesc),unfortunately "imagesc" command doesn't work for it.
how can I plot it?
Thanks for your attention
A simple way would be to use scatter3 and assign your displacements to be the colours. Note that you have to supply a size for this to work - I'm using [] (empty matrix) which will set it to default. If your four sets of values are four vectors of the same size, then it's just something like:
scatter3(lat,lon,depth,[],displacement, 'filled')
Values in displacement will be linearly mapped to the current colormap. 'filled' gives you filled markers rather than open ones (default marker is a circle but can be changed).
You can plot each point using plot3(longitude,latitude,depth). You can color each point according to the displacement in a for loop. The easiest way to do this is create a colormap, e.g. using jet and chosing the color according to the displacement.
figure;
hold on;
cmap = jet(256);
dispRange = [min(displacement),max(displacement)];
for k=1:size(longitude,2)
c = cmap(1+round(size(cmap,1)*(displacement(k)-dispRange(1))/dispRange(2)),:);
plot3(longitude(k),latitude(k),depth(k),'o', ...
'MarkerEdgeColor',c,'MarkerFaceColor',c);
end

How can we fill different levels of contour with a color in matlab

I have a figure which consists of different levels of a contour plotted using the hold on function. I want to fill the space between the levels of contour with a color. Could you please assist me how can I do that. I have already tried the contourf function. The figure consists of different red colored levels of a contour, what I want is a solid color filled between these contour levels.
I am not sure if I got exactly what you wanted but here goes a possible solution to your problem.
If you plotted using the hold on function I would therefore assume you have each contour in a different variable. If so you can use logic to check whether a contour is higher or lower relatively to another. If your problem is that you do not have the same x axis for each so that you can use logic just interpolate for each contour (for example between -0.8 and 0.8)
I will give an example of what I am saying. See if this helps.
%simulated contours
x=linspace(0,pi,100);
y = sin(x);
y2 = sin(x)*2;
y3 = sin(x)*3;
figure, hold on,
plot(x,y),
plot(x,y2),
plot(x,y3),
%fill contours
[X,Y]=meshgrid(x,0:3/100:3);
zzz=(Y<repmat(y,size(Y,1),1))+(Y<repmat(y2,size(Y,1),1))+(Y<repmat(y3,size(Y,1),1));
figure,imagesc(zzz)
set(gca,'YDir','normal')

Matlab - plot two pcolors on top of each other, with different colormaps

I'm plotting two pcolors on top of each other (using the m_map algorithm m_pcolor). The reason for this is that the second pcolor has transparency in it, and so shows the pcolor underneath. The first plot consists of just ones and zeroes, and I would like it to be just black and white. I would like the second to use the colormap jet, but I can't work out how to set one colormap without changing the other. My code at the moment is:
h1 = m_pcolor(Lon', Lat', black_background);
hold on;
h = m_pcolor(Lon', Lat', input_matrix);
Thanks in advance,
Adam
For this limited application, the easiest way is probably to append a row of zeros onto the colormap, deal with scaling (the clim property) yourself so that each plot takes advantage of the appropriate part of the colormap.
cm=colormap('jet'); %# Nx3
cm = [cm; 0 0 0]; %# append black row: (N+1)x3
h1 = m_pcolor(Lon',Lat',black_background);
set(h1,'clim',[length(colormap),length(colormap)])
hold on
h2 = m_pcolor(Lon', Lat', input_matrix);
set(h2,'clim',[length(colormap)-1, length(colormap)-1])
This should get you close, but I haven't tested it since I'm not on my matlab machine.
Another option is freezeColors from the file exchange (but this may only work for different axes within the same figure window, I'm not sure about different plots in the same axes object).