How can we fill different levels of contour with a color in matlab - 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')

Related

Matlab or Origin - Combining two sets of 3D data in one contour plot

I have two sets of 3D data with XYZ coordinates. I would like to know if there is a program that can combine the two, such that:
One set of data is represented by the colours of the plot, and the other set of data is represented by the height (in 3D) of the plot.
I am familiar with both Matlab and Origin.
Can be done with surf(Z,C).
a = randi(20,20,20);
b = randi(20,20,20);
figure;
subplot(2,2,1);
surf(a);
title('Height');
subplot(2,2,2);
surf(b);
title('Color');
subplot(2,2,[3,4]);
surf(a,b);
title('Mixed');
Not the best representations but you can see one matrix yields height and one yields color.
Color of mixed plot comes from right plot
Height of mixed plot comes from left plot
It is easy if you use scatter3 function.
w=100;
x1=rand(1,w);
y1=rand(1,w);
z1=rand(1,w)*100;
z2=ceil(rand(1,w)*255);
figure
h=scatter3(x1,y1,z1,ones(1,w)*50,z2,'filled');

How to plot contours with selected colors and formatted labels

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.

Matlab: How can I control the color of a streamtube plot?

I am currently trying to plot 3D streamtubes. I want the tubes to be colored corresponding to their respective velocity (e.g. slow = blue, fast = red).
To be more exact, I have three 3D-matrices containing the velocity in x, y and z direction. The color of the streamtubes should be sqrt(vx^2+vy^2+vz^2). When using streamtube(x,y,z,vx,vy,vz,sx,sy,sz) the tubes are colored according to their z-coordinate which is useless because it's a 3D plot anyway.
Well this wasn't easy (it ought to be a builtin option), but by modifying the CData of each tube (they are each their own graphics object), you can achieve the desired result. Here's an example
load wind
[sx,sy,sz] = meshgrid(80,20:10:50,0:5:15);
h=streamtube(x,y,z,u,v,w,sx,sy,sz);
drawnow
view(3)
axis tight
shading interp;
This gives this picture:
but then doing this:
vel=sqrt(u.^2+v.^2+w.^2); %// calculate velocities
for i=1:length(h)
%// Modify the colour data of each tube
set(h(i),'CData',interp3(x,y,z,vel,get(h(i),'XData')...
,get(h(i),'YData'),get(h(i),'ZData'),'spline'))
end
drawnow
view(3)
axis tight
shading interp;
gives this result
NOTES:
1) I don't know if this is fully correct, I don't know how to test it
2) You have to interpolate the velocity data from the points where it's known onto the vertices of the streamtubes
3) I found the spline interpolation option to work best, but the other options might work better in other cases

Improve surface plot visualisation of scatter points

I want to visualize 4 vectors of scattered data with a surface plot. 3 vectors should be the coordinates. In addition the 4th vector should represent a surface color.
My first approach was to plot this data (xk,yk,zk,ck) using
scatHand = scatter3(xk,yk,zk,'*');
set(scatHand, 'CData', ck);
caxis([min(ck), max(ck)])
As a result I get scattered points of different color. As these points lie on the surface of a hemisphere it ist possible to get colored faces instead of just points. I replace the scattered points by a surface using griddata to first build an approximation
xk2=sort(unique(xk));
yk2=sort(unique(yk));
[xxk, yyk]=meshgrid(xk2, yk2);
zzk=griddata(xk,yk,zk,xxk,yyk,'cubic');
cck=griddata(xk,yk,clr,xxk,yyk,'cubic');
surf(xxk,yyk,zzk,cck);
shading flat;
This is already nearly what I want except that the bottom of the hemisphere is ragged. Of course if I increase the interpolation point numbers it gets better but than the handling of the plot gets also slow. So I wonder if there is an easy way to force the interpolation function to do a clear break. In addition it seems that the ragged border is because the value of zzk gets 'NaN' outside the circle the hemisphere shares with the z=0-plane.
The red points at the top are the first several entries of the original scattered data.
You can set the ZLim option to slice the plotted values within a certain range.
set(gca, 'Zlim', [min_value max_value])

Making an accurate colorbar for a simple plot

I am trying to make a simple plot (for this example doing a plot of y=x^2 will suffice) where I want to set the colors of the points based on their magnitude given some colormap.
Following along my simple example say I had:
x = 1:10;
y = x.^2;
Use gscatter(x,y,jet(10)); legend hide; colorbar which produces a plot with the points colored but the colorbar does not agree with the colored values. (Can't post picture as this is my first post). Using a caxis([1,100]) command gives the right range but the colors are still off.
So I have two questions:
(1) How can I fix the colors to fit to a colorbar given a range? In my real data, I am looking at values that range from -50 to 50 in some instances and have many more data points.
(2) I want to create a different plot with the same points (but on different axes) and I want the colors of each point on this new plot to have the same colors as their counterparts in the previous plot. How can I, programmatically, extract the color from each point so I can plot it on two different sets of axes?
I would just move the points into a matrix and do an imagesc() command but they aren't spaced as integers or equally so simple scaling wouldn't work either.
Thanks for any help!
Regarding you first question, you need to interpolate the y values into a linear index to the colormap. Something like:
x = 1:10;
y = x.^4;
csize = 128;
cmap = jet(csize);
ind = interp1(linspace(min(y),max(y),csize),1:csize,y,'nearest');
scatter(x,y,14,cmap(ind,:),'filled')
colorbar
caxis([min(y) max(y)])
Using interp1 in this case is an overkill; you could calculate it directly. However, I think in this way it is clearer.
I think it also answers your 2nd question, since you have the index of the color of each data point, so you can use it again in the same way.