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

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

Related

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:

Removing the edge line on a contour plot

I used Matlab to create a polar coordinate and converted it into Cartesian coordinate.
[th,r] = meshgrid((0:0.5:360)*pi/180,0:.02:1);
[X,Y] = pol2cart(th,r);
I get data on this mesh and produce a contourf plot over it.
My problem is that I get a center line in my contourf plot which I would like to remove, could some help me with this
Thank you
If I extend a little bit your example to get something I can plot, I do reproduce the problem:
[th,r] = meshgrid((0:0.5:360)*pi/180,0:.02:1);
[X,Y] = pol2cart(th,r);
Z = sqrt( X.^2 + Y.^2 ) ;
isoLevel = 0:0.1:10 ;
[C ,hc] = contourf(X,Y,Z,isoLevel) ;
The black line at the interface is because the function contourf create patch objects and these objects tend to "close" themselves (they will create a line between the first and last point defined in their profile).
This is easier to observe if you do not complete the definition of your profile over 360 degrees. The picture on the right shows you the same example but with the grid only from 0:350 and with the LineStyle set to :.
As you can see, it is difficult to control how Matlab will actually render this specific profile limit. There are ways to control specific edges of patch objects but in this case it would involve retrieving the handle of each patch object (10 in my case but many more in more complex cases), locating the edge you want to control and basically redefining the patch (each of them). You'd be better off drawing the patches from scratch yourself.
Fortunately, there is a simple ways out of that: Get rid of all the patch edge lines...
but then you may miss your isolines! No problem, just plot them on top of the patches !
You get all your colored patches (with no border) and a set of (iso)lines over which you have full control.
Two easy way to get you patch without lines (i) set the shading to shading flat, or (ii) specify 'EdgeColor','none' in the parameter of the contourf function.
To get your isolines on top, use the sister contour function.
So using the same X,Y and Z data than previously:
isoLevel = 0:0.1:10 ;
[C ,hc] = contourf(X,Y,Z,isoLevel,'EdgeColor','none') ; %// set of patches without border
% shading flat %// use that if you didn't specify ('EdgeColor','none') above
hold on
[C2 ,hc2] = contour(X,Y,Z,isoLevel,'LineColor','k') ; %// now get your isolines
will render:
It's a good idea to store the handle hc2 in case you want to modify your isolines properties (color, style, thicknes etc ...).
Also, specifying the isoline levels is recommended. This way you can make sure both contour and contourf will use the same set of isovalues. It could probably work without this (because the underlying dataset is the same), but personally I always prefer to be explicit and not rely on background calculations.

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

Set legend from looped data points matlab

I would like to scatter plot different sets of data points. Each point should have a different markerfacecolor, but within a given set, they would all have the same markeredgecolor.
I got this to work by looping over single scatter points individually, because markerfacecolor seems to only be able to take scalar points. The way I do it is that I loop through my data, and look for the appropriate color.
This works fine because I can define each point separately, but it becomes a problem when trying to set up the legend. It tries to list all the different points, but what I'd like is just an empty circle (markerfacecolor white or transparent), with each set having their specific markeredgecolor.
I hope this is clear enough. Thank you.
Mike
I've had luck using patches, setting 'FaceColor', 'EdgeColor' to 'none', 'MarkerEdgeColor' to 'flat' and setting the 'FaceVertexCData' to a Nx3 matrix where each row is a color corresponding the the points specified by you Nx1 'XData', 'YData' and 'ZData'.
h = patch('parent',gca,...
'XData',x,...
'YData',y,...
'ZData',z,...
'FaceColor','none',...
'EdgeColor','none',...
'MarkerFaceColor',faceColor,...
'MarkerEdgeColor','flat',... This is what sets the Edge of each marker
'FaceVertexCData',C) % C is a Nx3
I don't have access to Matlab at the moment, and Octave does not seem to have exactly the same functionality. Check out the Matlab patch properties documentation for more information.

about plotting two images in pseudocolor figures

I am using matlab to visualize my data in pseudocolor figures (pcolor). It works pretty good to show the data in pcolor but now I get one more data set. If I plot each data set in a separate pcolor plot, everything is fine. But now, I combine the two data such that
NEW_DATA = [OLDDATA1, OLDATA2]
if I pcolor the NEW_DATA instead, the color get messed up. Is that any way I can show the combined data without losing any detail or color information. In my case, if you plot pcolor(NEW_DATA), the general shape was maintained (just like [pcolor(OLDDATA1), pcolor(OLDATA2)]) but some detail become fuzzy
In pcolor plots, the vertex colors are scaled into the color map. You can set the scaling using the 'clim' property of the axes. I'm guessing what is happening here is that the scaling is different between the two OLDDATA and the combined NEWDATA.
Ultimately you need to decide which scaling to use; if you like the original from OLDDATA1, you could do the following:
pcolor(OLDDATA1);
orig_clim = get(gca,'clim'); %# just to get the color limits
pcolor(NEWDATA);
set(gca, 'clim', orig_clim);
If you like the original scaling of both OLDDATA plots and there isn't a scaling that works for both, you can manually create a figure with two axes placed adjacent to each other (rather than relying on subplot). Each axes object can have its own clim that way, but the two plots will appear contiguous.
figure;
h1 = axes('units','normalized','position',[.05 .05 .45 .45]);
h2 = axes('units','normalized','position',[.5 .05 .45 .45]);
pcolor(h1, OLDDATA1);
pcolor(h2, OLDDATA2);
set(h2, 'ytick', []); %# turn off ytick (if it was on)