multiple colours on matlab histogram - matlab

Hi I am trying to get multiple colours on a matlab histogram - i think the following should do it:
figure
hist(ligand,50)
h=findobj(gca,'Type','patch');
set(h,'FaceColor',[0 .5 .5],'EdgeColor','w')
hold on;
hist(potassium,50)
g=findobj(gca,'Type','patch');
set(g,'FaceColor',[0 1 1],'EdgeColor','w')
hold on;
hist(rectifier,50)
title('Alignment to AFP1')
xlabel('Score'); ylabel('Number of Sequences')
hold off;
where the first colour is [0 .5 .5], the second [0 1 1] and the third is the default colour. However even though I have specified two separate colours for the first two using two handles, h and g - both are the same colour, using the g handle.
What am I doing wrong?
edit - this is for Luis Mendos's suggestion - I am getting an "index exceeds matrix dimensions" with the following
figure
hist(ligand,50)
g=findobj(gca,'Type','patch');
set(g(1),'FaceColor',[0 .5 .5],'EdgeColor','w')
hold on;
hist(potassium,50)
set(g(2),'FaceColor',[0 1 1],'EdgeColor','w')
hist(rectifier,50)
title('Alignment to AFP1')
xlabel('Score'); ylabel('Number of Sequences')
hold off;
Thanks.

The problem is that g is a two-element vector, because it includes the two histograms that have already been plotted. Remove the lines with h (lines 3 and 4) and replace the line set(g,...) by
set(g(1),'FaceColor',[0 .5 .5],'EdgeColor','w')
set(g(2),'FaceColor',[0 1 1],'EdgeColor','w')

Related

Matlab figure problems: half scatter points, black lines inside a red region

Consider the following figure in Matlab (matrices here)
load matrices
%Rb, vertices_deg, vertices_comp
close all
patch([0 0 1],[0 1 0],[1 0 0],[0.8 0.8 0.8]);
axis equal
axis([0 1 0 1 0 1])
view(120,30)
hold on
T = delaunayTriangulation(Rb.');
K = convexHull(T);
patch('Faces',K,'Vertices',T.Points,'FaceColor','k','edgecolor','k');
hold on
scatter3(vertices_deg(:,1), vertices_deg(:,2) , vertices_deg(:,3),100,'o','filled','b')
hold on
patch(vertices_comp(:,1), vertices_comp(:,2) , vertices_comp(:,3),'red')
hold off
xlim([0 1])
ylim([0 1])
zlim([0,1])
box on
set(gca, 'ytick',0:0.2:1,'xtick',0:0.2:1,'ztick',0:0.2:1,'FontSize',13)
I would like to save this figure in a way such that:
within the red region, I do not get the black lines that I can see in the Matlab output
the blu scatter point is a full circle (and not half circle, as it appears in the Matlab output)
I tried two ways of saving the figure
saveas(gcf,'3.jpg')
print(gcf, '3.jpg', '-dpng', '-r300', '-painters')
None of these two gives me what I want. Could you help?
This is what I get with PRINT
This is what I get with SAVEAS
And here a screenshot of the Matlab window
The problem you are seeing is that the patches are plotted at the exact same plane, which causes this render effect. This is called Z-fighting.
An easy fix is to add some small offset to some of the planes that are drawn in front of the others. You can tweak this value till the effect is gone, and the error from the indented place is minimal.
load matrices
close all
patch([0 0 1],[0 1 0],[1 0 0],[0.8 0.8 0.8]);
axis equal
axis([0 1 0 1 0 1])
view(120,30)
hold on
T = delaunayTriangulation(Rb.');
K = convexHull(T);
d_patch = 0.001;
d_z = 0.01;
patch('Faces',K,'Vertices',T.Points + d_patch,'FaceColor','k','edgecolor','k');
patch(vertices_comp(:,1), vertices_comp(:,2) , vertices_comp(:,3)+d_z,'red')
scatter3(vertices_deg(:,1), vertices_deg(:,2) , vertices_deg(:,3),100,'o','filled','r')
scatter3(vertices_deg(:,1), vertices_deg(:,2) , vertices_deg(:,3)+2*d_z,100,'o','filled','b')
xlim([0 1])
ylim([0 1])
zlim([0,1])
box on
set(gca, 'ytick',0:0.2:1,'xtick',0:0.2:1,'ztick',0:0.2:1,'FontSize',13)
saveas(gcf,'3saveas.png')
print(gcf, '3print.png', '-dpng', '-r300', '-painters')
You can do the same for the blue dot, which is partially drawn into the plane. Just give it a little offset, and it appears as a full dot again. I plotted the dot in red and blue, so you can see the offset in location.

Two point series on the same graph on MatLab

I want to draw two separated lines, but I get the two lines on two points. How do I draw them separated?
When you implement the code, the green line should start from point 3 and end in point 4 on the x-tick. But, it starts again from point 1 and end in point2.
%% My question code:
a=3; %point1
b=4; %point2
c=6; %point3
d=7; %point4
plot([a b], 'k- *');
hold on;
plot([c d], 'g- *');
hold off
set(gca, 'XTick', 1:4, 'XTickLabel', {'point1', 'point2', 'point3','point4'})
axis([0 10 0 10]);
When you hold the current plot and plot a new one. x axis values are taken as if it is the first plot. To avoid the confusion, specify values for both x and y axes.
So change your plot commands like this:
plot([1,2], [a,b], 'k- *');
hold on;
plot([3,4], [c,d], 'g- *');
hold off;
or combine two plot commands into a single one like this:
plot([1,2], [a,b], 'k- *', [3,4], [c,d], 'g- *');

MATLAB plotting error arc on cirular rose plot

I used the answer on Plot vector (or arc) onto a rose plot. MATLAB to plot my rose diagram but my plot looks like this:
My code:
circ_plot(BB,'hist',[],20,true,true,'linewidth',2,'color','r')
hold on
plot([0 cos(mean)*a], [0 sin(mean)*a], 'r')
%// Plot error as many shaded triangles that compose a circular wedge
t = linspace(-Var/2+mean,Var/2+mean,100); %// increase "100" if needed
for k = 1:numel(t)-1
h = patch([0 cos(t(k))*a cos(t(k+1))*a 0], ...
[0 sin(t(k))*a sin(t(k+1))*a 0], [.5 0 0], 'edgecolor', 'none');
%// change color [.5 0 0] to something else if desired. Note also alpha
set(h,'Facealpha',.3) %// make transparent
end
%// Place rose on top by rearranging order of axis children
ch = get(gca,'children');
set(gca,'children',[ch(2:end); ch(1)]);
Any ideas how I get my error shaded area do follow the circumference of the circle rather than being a triangle?
Thanks
I don't know why, but today its works fine. I didn't change anything. Thanks for your reply and offer of help. unfortunately due to the sensitive nature of my data I am unable to post.
thanks again.

Plotting subplots in a figure automatically for each column of matrix

For example let's say I have a following matrix (<9x6 double>) with a colheaders(<1x6 cell>).
Matrix =
226.7431 14.7437 14.9417 14.1000 14.5000 66.0590
226.7500 14.6582 14.8250 NaN 14.2000 66.7740
226.7569 14.3590 14.6067 NaN 13.9000 68.4897
226.7639 14.2702 14.5717 13.4000 13.8000 68.2487
226.7708 14.2555 14.6000 NaN 14.0000 NaN
226.7778 14.1605 14.5967 NaN 13.9000 NaN
226.7847 14.0320 14.4567 12.9000 13.6000 68.8272
226.7917 13.8422 14.2733 NaN 13.4000 69.6392
226.7986 13.6585 14.1169 NaN 13.1000 69.8048
I want to plot first column of matrix on x-axis and the rest on y-axis in a matlab figure with subplots (let's say 3 in one figure). Manually I can do something like this a figure and so on.
figure
subplot(3,1,1)
plot(Matrix(:,1),Matrix(:,2),'ro'); grid on; box on; xlabel('A');ylabel('B')
subplot(3,1,2)
plot(Matrix(:,1),Matrix(:,3),'bo'); grid on; box on; xlabel('A');ylabel('C')
subplot(3,1,3)
plot(Matrix(:,1),Matrix(:,4),'go'); grid on; box on; xlabel('A');ylabel('D')
and so on.....
......
......
Now here start a tricky part in which I required help from experts like you guys. I do not want to do manual plotting for my matrix as it consists on 200 columns. So what I want to do a automatic plotting of matrix, so that it plot every column of matrix in subplots. But 200 subplot can not come in one figures, so it start automatically a new figure after subplots limit(let's say 3). Beside I also need to define 'xlabel, ylabel,legend' automatically with a header file 'colheaders'. Is it possible?
x = rand(10, 200);
myYLabel = char(64+randi(26, 200, 1));
nrows = 3;
ncols = 2;
for ii = 1:size(x, 2)
if nrows*ncols-mod(-ii, nrows*ncols) == 1
figure;
end
subplot(nrows, ncols, nrows*ncols-mod(-ii, nrows*ncols));
plot(x(:, ii));
ylabel(myYLabel(ii, :));
end
It seems to be an easy for-loop task.
I assume you know before how much subplots you want in each figure (let's say 3).
A = yourdatamatrix;
header = [{'A'}, {'B'}, {'C'}, {'D'}, {'E'}, {'F'}];
n = 6 %number of columns
i_figure = 1;
for ii=1:3:n-3
figure(ii)
subplot(3,1,1)
plot(A(:,1),A(:,ii+1),'ro'); grid on; box on; xlabel(header(1));ylabel(header(ii+1))
subplot(3,1,2)
plot(A(:,1),A(:,ii+2),'bo'); grid on; box on; xlabel(header(1));ylabel(header(ii+2))
subplot(3,1,3)
plot(A(:,1),A(:,ii+3),'go'); grid on; box on; xlabel(header(1));ylabel(header(ii+3))
end
I assume also you don't care about your figure number, otherwise just implement another counter.
Also be aware that the number of your columns+1 is divisible by 3.

Plot a data series beneath another one

When you plot things in Matlab, the most recently plotted data series is placed on top of whatever's already there. For example:
figure; hold on
plot(sin(linspace(0,pi)),'linewidth',4,'color',[0 0 1])
plot(cos(linspace(0,pi)),'linewidth',4,'color',[1 0 0])
Here, the red line is shown on top of the blue line (where they intersect). Is there any way to set "how deep" a line is drawn, so that you can plot things beneath what's already there?
Use the uistack command. For example:
h1 = plot(1:10, 'b');
hold on;
h2 = plot(1:10, 'r');
will plot two lines with the red line plotted on top of the blue line. If you then do:
uistack(h1);
the blue line will be brought to the front.
You can also accomplish this by setting the order of the children vector of the current axes. If you do the following:
figure; hold on
h1 = plot(sin(linspace(0,pi)),'linewidth',4,'color',[0 0 1]);
h2 = plot(cos(linspace(0,pi)),'linewidth',4,'color',[1 0 0]);
h = get(gca, 'Children');
you will see that h is a vector that contains h1 and h2. The graphical stacking order is represented by the order of the handles in h. In this example, to reverse the stacking order you could do:
h = flipud(h);
set(gca, 'Children', h);