How to adjust the zcolor scale in a scatter plot in Matlab? - matlab

I have a data set, contained in three vectors say xx, yy and zz. I want to plot yy vs xx with the marker color face according to zz, so I use the scatter function such as:
scatter(xx,yy,50,zz,'s','filled')
Unfortunately zz has some very extreme values, so I cannot see any difference in the marker face color: all the dots are dark blue!
Is there a possibility to solve this issue? I was thinking of a possibility to impose a lower and an upper value for the color scale, so that any dot with a zz value out of the authorized range would be grey (or of the color of the closest bound)...?
Thank you for your help!

You can try changing the CLim property of the axes.
This example uses the MatLab example data seamount an changes the colorscale range
from the original [-4250 -490]
to the new [-1000 -100]
Default color scale
load seamount
figure
scatter(x,y,5,z)
colorbar
Modified color scale
figure
scatter(x,y,5,z)
set(gca,'clim',[-1000 -100])
colorbar
Default color scale
Nodified color scale

Related

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

How can I plot the absolute value of a quantity with colorbar?

I am trying to figure out how to use colorbar to show the magnitude rather than the value. Currently colorbar has a range from [-x x] where any negative values are color blue-ish. I want the colorbar to ignore the sign values when determining what color to paint the graph.
I tried setting the range to [0 x] in CLim but that just paints anything that is negative blue.
An example would be plotting a sphere. If you plot a sphere, it would be centered
at the origin and the color would only reflect the value of the z-axis. However, I want the colorbar to show the magnitude of the distance from the center. So, in this case, the sphere should be a solid color representing the radius.
Any ideas?
You must take the magnitude of the data you are plotting (e.g. using the abs(data) function) prior to making a figure with it. Then, you can set the colorbar scale using the caxis([min max]) function.
Example:
rawData=repmat([-10:1:10],10,1);
figure(1),imagesc(rawData),caxis([0 10]) % All raw values below 0 are plotted as blue
magnData=abs(rawData) % Take absolute value of raw data
figure(2),imagesc(magnData),caxis([0 10]) % Raw negative values are now plotted in the same color as positive raw values (i.e. ignoring the sign, per the solution you requested).
Your solution (CLim) didn't work because setting the color range [0 x] will associate color of all values lower than 0 to the value of 0 (blue, in the case of the "jet" colormap).

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

I have a 2D plot in Matlab where I want to specify the RGB value for each point to vary to color

I am trying to plot a 2D line in Matlab with a color that varies based on an RGB code that I assign to each point. The code below works well for a given colormap ('col' values defining the color), but I am trying to keep tighter control over the color assignment, so that values always render the same color across multiple charts.
surface([x;x],[y;y],[z;z],[col;col],...
'facecol','no',...
'edgecol','interp',...
'linew',2);
The question is how do you decide which colour does each point have? defining the colormap is easy (shown below).
col=colormap(hot(128)) %% or other colour style like hsv or jet
if you have a variable (lets called it V) that for each point has a certain value and you want the colours to change based on that:
first define the values for the extremes in the colormap:
min_col=0; %%%can be the minimum of V
max_col=1; %%%can be the maximum of V
Then interpolate to your data
new_col=interp1(linspace(min_col,max_col,length(col)),col, V(:))

How do you draw different surfaces with the same color scale in MATLAB?

I'm trying to represent several surface plots* for which the scale differs a bit. Each surface plot is drawn in a separate subplot and/or figure.
Right now, I'm using the default color mapping, which automatically scales the whole range of the color map to my figure, i.e. the maximum of my surface is always red (in 'jet' color mode) regardless of the magnitude of this maximum.
I'd like the colormap to be consistent between the figures instead of spread between the min and max of each individual graph. That way, readers could appreciate the difference in scale of the surfaces just by looking at the color map.
Any idea on how to do this?
**Actually, in case it makes a difference, I'm plotting results of a surface fitting operation using the plot command as follows:*
[myfit, gof] = fit( ... );
plot(fit)
You should use the caxis function. For example, if one surface has a height from 0 to 5 and the other has a height from 0 to 10, doing the following for both plots:
caxis([0 10]);
will force them both to use the same color scale as the plot that covers the larger range. You can also call caxis with an axes handle as the first argument:
caxis(hAxes, [0 10]); % Sets the color scaling for hAxes
If not specified, caxis adjusts the color scaling of the axes that is current.
I recently answered this question in video form on my blog:
http://blogs.mathworks.com/videos/2009/03/27/setting-the-colormap-to-be-consistent-across-axes/