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

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

Related

How to adjust the zcolor scale in a scatter plot in 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

Color the same line using two different colors

I have to plot a linear function and color it in a way that all negative values are red and the positive values are blue.
This is just an example of what I want to ask.
Generalizing, is it possible to color specific intervals of a function in a different color than that of the rest of the function, without having to do different plots?
No. Create a red coloured plot for the negative values, use hold on and then a blue coloured plot for the positive values.
x = [-10:0.1:10].'; %//range
x1 = x(x<=0); %//negative values and zero
x2 = x(x>=0); %//positive values and zero
figure; %//open figure
hold on %// plot things in the same figure
plot(x1,x1,'r') %//plot negative values
plot(x2,x2,'b') %//plot positive values

"Density" plots in Matlab, but not in the sense of density of data points

I would like to plot a sort of "density map" in Matlab, but have not found the right tool yet.
I have "continuous" data with x between (x_min and x_max), and y between (y_min and y_max). At each of these pairs of points (x_i,y_i), there is associated to it a value between 0 and 1.
I would like to plot this information in a 2d graph, such that in each small square containing (x_i,y_i) the plot shades the square black for the value 0, white for the value 1, and the appropriate shade of gray for intermediate values.
Can this be done easily in Matlab?
http://www.mathworks.com/help/images/ref/mat2gray.html seems to do exactly what I need.
If the data is in a matrix A, you can just use
image(255*A); colormap gray(256); axis image;
I'm not sure what you mean by continuous (uniformly spaced?), so my answer won't make too many assumptions other than that there is a reason why you mention the coordinates (if just a regular mesh, then just image or imagesc). So, only assuming your x and y coordinates are possibly non-uniformly spaced, but at least monotonically increasing samples, try surf with view(2):
surf(X,Y,data)
view(2)
colormap gray
By default surf sets the FaceColor property with the 'flat' option:
flat — The values of CData determine the color for each face of the surface. The color data at the first vertex determine the color of the entire face.
In other words, the value will determine the shade.
Assuming your data is in data and your x and y coordinates are in x and y, here is how to do it:
imagesc(x, y, data) % to create a heat map
colormap(gray) % for gray levels
caxis([0 1]) % to set 0 to black and 1 to white
axis xy % if you want the y axis to point up
colorbar % to display the colorbar

Matlab Scatter Plot - Set consistent color gradient

I'm making a whole bunch of separate scatter plots. Each one represents a timestep, so it would be nice to have the color gradient consistent from plot to plot. The default color gradient for a scatter plot depends on the range of values in the plot. Is there a way for me to define the max and min values of the range (so the max and min values in all of my plots combined), and use that as the gradient range for each individual plot?
Right now I just have:
h = scatter(ModelInfo.X(:,1),ModelInfo.X(:,2),50,ModelInfo.y,'filled')
where ModelInfo.y is the "value" assigned to each (X1,X2) pair. I would like to create a gradient from ModelInfo.y=0 to 30.
You want to use the CAXIS command to set the limits on the color bar.
After every new plot, call
caxis([0 30])
This way, the colormap maps from 0 to 30. Values below 0 are mapped to the first, values above 30 to the last color of the colormap, respectively.
Not near Matlab at the moment but you should be able to set the color scale using caxis
caxis([minVal, maxVal]);
where minVal maxVal are the limits to specified minimum and maximum values. Data values less than minVal or greater than maxVal map to minVal and maxVal , respectively. Values between minValand maxVallinearly map to the current colormap.

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/