Matlab: How to set color of legend in a scatter plot where each data point gets a different color? - matlab

Here is the sample code:
x = rand(100,1);
y = rand(100,1);
score = rand(100,1);
figure;
colormap(flipud(bone));
caxis([0 1])
axis([0 1 0 1])
scatter(x,y,50,score,'+','LineWidth',2);
legend('scores');
I'm using the reversed 'bone' colormap which assigns pure white to score value 0 and pure black to score value 1. However, the legend seems to be automatically assigned the score 0, and so if you run the code the legend color is pure white and not visible.
Is there any ways to fix that? Thanks.

If you only want to plot a black + without showing the color range of the data (as with color bar) you can create a dummy legend for that. Here is how you do that:
% plot some dummy data for the legend:
scatter(nan,nan,[],1,'+','LineWidth',2)
hold on
% plot your data:
scatter(x,y,50,score,'+','LineWidth',2);
hold off
% add the legend only for the first (dummy) data:
leg = legend('scores');
The result:

What you are looking for is a colorbar. This shows a bar with color gradient for all values in the colormap used.
Replace legend('scores'); line with colorbar.
P.S. legend is meant for identifying multiple plots in the same figure.

Related

MATLAB: Specify a fixed color for zero in imagesc [duplicate]

I have a wide range of values and while plotting as a scatter(x,y,z), the colorbar showing the z axis shows a wide range of values, now I am not interested in the lower range values. Is there any method to change the range in color bar.
I have the following part of my code to plot, I also intend to plot the log plot. For eg. I want to set the range in my log plot to 14 to the maximum value.
I want some values not to be displayed at all. so that the color bar has a limited range, say from 14 to maximum. At present it is showing from 9 to maximum in the log plot.
scatter(x(1:end-1), y(1:end-1), 5, gnd);
title('G plot (m^-^2)');
colorbar('eastoutside');
xlabel(' X-axis (microns)');
ylabel('Y-axis (microns)');
figure;
log_g=log10(gnd);
scatter(x(1:end-1), y(1:end-1), 5,log_g);
colorbar('eastoutside');
xlabel(' X-axis (microns)');
ylabel('Y-axis (microns)');
title('G Density, log plot (m^-^2)');
I believe that caxis is the command you're looking for. Usage:
caxis([minValue maxValue])
Using caxis like this, all values outside the range [minValue maxValue] will be coloured with the lowest or highest value in the colormap, respectively.
Since colorbar and friends use colormap, you'll have to adjust the current colormap if you want to adjust the number of colors used. Do this like so:
%# get current colormap
map = colormap;
%# adjust for number of colors you want
rows = uint16(linspace(1, size(map,1), NUM_COLORS)) ;
map = map(rows, :);
%# and apply the new colormap
colormap(map);
Of course, combining this with caxis is the most powerful.
If you don't want to show some values outside of range, that's not a job for colorbar or caxis, that's up to you -- you'll have to adjust the data that's plotted so that all values you don't want plotted are NaN. Doing so will make Matlab understand that you don't want to plot these data:
data( indices_to_data_not_to_plot ) = NaN;
surf(x,y,data); %# or whatever you're using
How about this?
% don’t know why, but apparently your x and y are one value too long?
x = x(1:end-1); y = y(1:end-1);
% only plot values of 14 or higher
scatter(x(gnd>=14), y(gnd>=14), 5, gnd(gnd>=14);
Try this:
cmap = colormap; % get current colormap
cmap=cmap([min max],:); % set your range here
colormap(cmap); % apply new colormap
colorbar();

"Transition-FaceColor" in Matlab

I want to color a rectangle in Matlab with a "transition facecolor" (I do not know the right term) meaning for instance a transition from hell blue to dark blue; you could also interpret it as a shadowing (here you can see an example:
http://il1.picdn.net/shutterstock/videos/620653/thumb/1.jpg?i10c=img.resize(height:160)
I could imagine to achieve it by using a colormap, but I do not know how to apply it on a text annotation like a rectangle.
Is it possible to modify Matlab's standard (monochromatic) colors in such a way? And if so, does someone have a basic framework for it?
You can create the rectangle using a patch which allows having interpolated face colors.
Then, in order to have the face color ranging from dark blue to bright blue you have to define your own "blue" colormap.
The colormap should be defined as a (N x 3) RGB array: in your case you have to set to 0 the first two columns (corresponding to red and green and have the values of the third column (the blue) ranging from (start_blue,end_blue) where start_blue is the darkest blue level you want, end_blue the brightest (both have to be between 0 and 1).
% Define the rectangle: lower left x, lower left y, width, height
x_rect=1;
y_rect=1;
width=10;
height=5;
% Define the patch vertices and faces
verts=[x_rect y_rect;x_rect y_rect+height; ...
x_rect+width y_rect+height;x_rect+width y_rect];
faces=[1 2 3 4];
% Define the color: the higher the brighter
col=[0; 0; 4; 4];
figure
% Create the new blue colormap
b=0.7:.01:1;
cm1=[zeros(length(b),2) b']
% Set the new colormap
colormap(cm1)
% Plot the patch
patch('Faces',faces,'Vertices',verts,'FaceVertexCData',col,'FaceColor','interp');
As an alternative, you can create the rectangle as a surf, then, define your own colormap as above.
% Define the rectangle:
x_rect=1;
y_rect=1;
width=10;
height=5;
% Build a patch
xp=[x_rect:x_rect+width];
yp=[y_rect:y_rect+height];
% Get the number of points
n_xp=length(xp);
n_yp=length(yp);
% Create the grid
[X,Y]=meshgrid(xp,yp);
% Define the z values
Z=ones(size(X));
% Create the color matrix as uniformly increasing
C=repmat(linspace(1,10,n_xp),n_yp,1)
% Create the new blue colormap
start_blue=0.5;
end_blue=1;
b=start_blue:.01:end_blue;
cm1=[zeros(length(b),2) b']
% Set the new colormap
colormap(cm1)
% Plot the rectangle as a "surf"
surf(X,Y,Z,C)
shading interp
xlabel('X Axis')
ylabel('Y Axis')
view([0 90])
xlim([0 13])
ylim([0 9])
daspect([1 1 1])
Hope this helps.
Qapla'

Matlab Area Plot: Adjusting box style and tick marks

I am trying to create area plots with 2 y-axis using plotyy by using 'area' function. I don't want to have any tick marks or labels or titles but I just want the outside box. I would also like to save just the plot (not the entire figure window) as a png file.
When I turn off the x and y-axis ticks and labels, the box looks thin on the bottom and left, thick on the top and right. What am I doing wrong?
Also, if I try to make the box 'LineWidth' fat, I see two tick marks at the bottom left and bottom right - which ruin the plot and I am unable to remove. Can someone help?
My test code is below:
x = 1:100;
y1 = rand(size(x));
y2 = 100*rand(size(x));
fig_handle = figure('units','inches','position',[1 1 9 3]);
[a,p1,p2] = plotyy(x,y1,x,y2,'area');
c1 = get(p1,'child');
c2 = get(p2,'child');
set(c1,'facea',0.5,'FaceColor','b','EdgeColor',[0 0 0]);
set(c2,'facea',0.5,'FaceColor','r','EdgeColor',[0 0 0]);
set(c1,'Line','None');
set(c2,'Line','None');
set(a,'Layer','top')
set(a,'XTick',[]);
set(a(1),'YTick',[]);
set(a(2),'YTick',[]);
set(a,'TickDir','in')
set(a,'LineWidth',5);
Also, notice how the left Y-axis has red area on it, while the right Y-axis doesn't. Is this fix-able?
Any help would be appreciated! Also, I am new to StackOverflow - so, if these are too many questions in one post, please pardon me and I will post them as separate requests/questions.
Here is a workaround for the red appearing on the left Y-axis.
Since the axes line is quite thick, the data displayed close to it is drawn over it. To avoid this, you can slightly shift the x limits of the axes to make more room for the data. Do so by changing the XLim property of either axes since they share the same x limit:
XL = get(a,'Xlim');
xl = XL{1}; %// here XL{1} and XL{2} are the same...[1 100]
set(a(:),'Xlim',[xl(1)-.5 xl(2)+.5])
As for the annoying tick marks at the bottom of the plot, I must say that I don't know how to remove them while keeping the axes visible.
As an alternative solution to plotyy, here is a way to obtain a good result (I think) without plotyy. The trick is to superimpose 2 axes and make both of them not visible, then set the figure's color to white and add a black rectangle surrounding the plot.
Here is the code:
clear
clc
close all
x = 1:100;
y1 = rand(size(x));
y2 = 100*rand(size(x));
H1 = area(x,y1);
%// Set the figure color to white
set(gcf,'Color','w')
%// Plot data and set different properties for each axes
A1 = gca;
A2 = axes('Position',get(A1,'Position'));
H2 = area(x,y2);
set(A2,'YAxisLocation','right','Color','none','XTickLabel',[]);
set(A2,'XLim',get(A1,'XLim'),'XTick',[],'YTick',[]);
set(A1,'XTick',[],'YTick',[]);
%// Make both axes not visible.
set(A2,'Visible','off')
set(A1,'Visible','off')
axes(A1)
%// Get axes limits
XL = get(gca,'XLim');
YL= get(gca,'YLim');
%// Create rectangle as a bounding box
rectangle('Position',[XL(1) YL(1) XL(2)-1 YL(2)],'LineWidth',5)
%//===
%// Change the data color/properties
hP = findobj('Type','patch');
set(hP(1),'FaceColor','b','FaceAlpha',.5,'EdgeColor',[0 0 0],'line','none')
set(hP(2),'FaceColor','r','FaceAlpha',.5,'EdgeColor',[0 0 0],'line','none')
And the output:
It's not perfect but hope that helps!

How to show color gradient on scatter plot in matlab?

I have a scatter plot that will overlay several sets of data. Each set of data currently displays as the next color in the default colormap. This is my code right now:
figure
hold on
for i=1:10
scatter(RunRawArea(i,:), RunRawNetLength(i,:));
end
hold off
What i would like is to color code each set of data (indexed by i) to be the next color on a gradient. For example, the data for i=1 would be blue, i=5 would be purple, and i=10 would be red.
How could I do this?
You should add another parameter to scatter - called CData
https://www.mathworks.com/help/matlab/ref/scatter.html
Description: scatter(x,y) creates a scatter plot with circles
at the locations specified by the vectors x and y. This type of graph
is also known as a bubble plot.
In your example:
figure
hold on
colorVec = linspace(1,0, size(RunRawNetLength,1));
colorVec = transpose(colorVec);
colorVec = repmat(colorVec,[1 3]);
for i=1:10
scatter(RunRawArea(i,:), RunRawNetLength(i,:),'CData', colorVec );
end
hold off
Method 1:
You can vectorize your data, so you don't need a loop and than add a color by series:
% specify your color map:
colorCode = lines(size(RunRawNetLength,1)); % or any other colormap...
% define the correct color for each series:
coloVev = repmat(colorCode,size(RunRawNetLength,1),1);
% plot it all at once without a loop:
scatter(RunRawArea(:),RunRawNetLength(:),[],coloVev)
Method 2:
If you have the Statistics and Machine Learning Toolbox you can also do this with gscatter:
% define the series of data:
group = repmat(1:size(RunRawNetLength,1),1,size(RunRawNetLength,1));
% plot it all at once without a loop:
gscatter(RunRawArea(:),RunRawNetLength(:),group,colorCode);
if your colormap has not enough colors for all the series, then the function loops over and start from the first one.
Result:
In both cases the result for some random data will be (the main difference is that gscatter fill the data points and add the legend by default):
Note that I used the colormap lines which has only 7 entries, so in this example with 10 series it repeats itself.

Matlab Ploting with different color for iso-surface

I was trying use the code shown below to plot in such a way that each iso-surface will be different in color and there will be a color bar at the right. I made a ss(k) color matrix for different colors. Number of iso-surfaces is 10 but I have only 8 colors. That's why I wrote ss(9)='r' and ss(10)='r'.
I need a solution to plot the iso-surface with different color and bar at the right side.
ss=['y','m','c','r','g','b','w','k','r','r']
k=1;
for i=.1:.1:1
p=patch(isosurface(x,y,z,v,i));
isonormals(x,y,z,v,p)
hold on;
set(p,'FaceColor',ss(k),'EdgeColor','none');
daspect([1,1,1])
view(3); axis tight
camlight
lighting gouraud
k=k+1;
end
Another possibility is to draw the patches with direct color-mapping (by setting the property 'CDataMapping'='direct'), while assigning the 'CData' of each patch to an index in the colormap of your choice. This is in fact recommended for maximum graphics performance.
Consider the following example:
%# volumetric data, and iso-levels we want to visualize
[x,y,z,v] = flow(25);
isovalues = linspace(-2.5,1.5,6);
num = numel(isovalues);
%# plot isosurfaces at each level, using direct color mapping
figure('Renderer','opengl')
p = zeros(num,1);
for i=1:num
p(i) = patch( isosurface(x,y,z,v,isovalues(i)) );
isonormals(x,y,z,v,p(i))
set(p(i), 'CData',i);
end
set(p, 'CDataMapping','direct', 'FaceColor','flat', 'EdgeColor','none')
%# define the colormap
clr = hsv(num);
colormap(clr)
%# legend of the isolevels
%#legend(p, num2str(isovalues(:)), ...
%# 'Location','North', 'Orientation','horizontal')
%# fix the colorbar to show iso-levels and their corresponding color
caxis([0 num])
colorbar('YTick',(1:num)-0.5, 'YTickLabel',num2str(isovalues(:)))
%# tweak the plot and view
box on; grid on; axis tight; daspect([1 1 1])
view(3); camproj perspective
camlight; lighting gouraud; alpha(0.75);
rotate3d on
I also included (commented) code to display the legend, but I found it to be redundant, and a colorbar looks nicer.
Matlab usually plots different iso-surfaces in different colors automatically, so you don't need to care about that. What kind of bar do you need? A colorbar or a legend? Either way, it is just to use the colorbar or legend function..
%Create some nice data
[x y z] = meshgrid(1:5,1:5,1:5);
v = ones(5,5,5);
for i=1:5
v(:,:,i)=i;
end
v(1:5,3:5,2)=1
v(1:5,4:5,3)=2
%Plot data
for i=1:5
isosurface(x,y,z,v,i)
end
%Add legend and/or colorbar
legend('one','Two','Three','Four')
colorbar
Since the color bar encodes value->color, it is impossible to do what you ask for, unless there is no intersection in z-values between all pairs of surfaces. So the solution below assumes this is the case. If this is not the case, you can still achieve it by adding a constant value to each surface, so to separate the surfaces along the z axis, and eliminate any intersection.
The solution is based on constructing a colormap matrix of piecewise constant values, distributed similarly to the z values of your surfaces. So for example, if you have 3 surfaces, the first has z values between 1 and 10, the 2nd between 11 and 30, and the 3rd between 31 and 60, you should do something like this (I plot in 2D for simplicity)
r = [1 0 0];
g = [0 1 0];
b = [0 0 1];
cmap = [r(ones(10,1),:); g(ones(20,1),:); b(ones(30,1),:)];
z1 = 1:10;
z2 = 11:30;
z3 = 31:60;
figure; hold on
plot(z1,'color',r)
plot(z2,'color',g)
plot(z3,'color',b)
colorbar
colormap(cmap)
More complex colormaps (i.e, more colors) can be constructed with different mixtures of red, green, and blue (http://www.mathworks.com/help/techdoc/ref/colorspec.html)