plot two histograms (using the same y-axis) and a line plot (using a different y-axis) on the same figure - matlab

How can I plot two histograms (using the same y-axis) and a line plot (using a different y-axis) on the same figure? I am using Matlab 2014b. I am aware of this but it seems to only work for bar plots?
This is my histogram code:
A = [1 2 2 2 3 4 5 5 5 5 5 5 5 5 5 6 6 6 7 7];
B = [6 6 6 7 7 7 7 7 7 7 8 8 8 9 9 10 10];
hist(A,7);
hold on
hist(B,7);
h = findobj(gca,'Type','patch');
set(h(1),'FaceColor','b','EdgeColor','b','facealpha',0.2)
set(h(2),'FaceColor','r','EdgeColor','r','facealpha',0.2)
xlabel('Day','fontsize',14)
ylabel('Frequency','fontsize',14)
xlim([1 10])
Now say I have these data:
Day = [1 2 3 4 5 6 7 8 9 10];
Prevalence = [3 2 4 8 5 6 7 8 9 5];
I want to plot these data (plot(Day,Prevalence)) using the right y-axis.
Thanks.

I think this workaround will do what you want.
Basically create a new axes at the same position than the one in which the histograms are plot, however set its color property to 'none' and the YAxisLocation to the right. You can then assign the new axes the properties you want.
Code:
clear
clc
%// ====================
%// Your code
A = [1 2 2 2 3 4 5 5 5 5 5 5 5 5 5 6 6 6 7 7];
B = [6 6 6 7 7 7 7 7 7 7 8 8 8 9 9 10 10];
hist(A,7);
hold on
hist(B,7);
h = findobj(gca,'Type','patch');
set(h(1),'FaceColor','b','EdgeColor','b','facealpha',0.2)
set(h(2),'FaceColor','r','EdgeColor','r','facealpha',0.2)
xlabel('Day','fontsize',14)
ylabel('Frequency','fontsize',14)
xlim([1 10])
%// ====================
Day = [1 2 3 4 5 6 7 8 9 10];
Prevalence = [3 2 4 8 5 6 7 8 9 5];
%// Get the current axes position to place the new one.
AxesPos = get(gca,'Position');
hold on
hax2 = axes('Position',AxesPos);
%// Plot the data
plot(Day,Prevalence,'--k','LineWidth',4,'Parent',hax2)
%// Set properties of the axes.
set(hax2,'Color','none','YAxisLocation','right','XTick',[],'XTickLabel','','YLim',[0 15])
ylabel('Prevalence','FontSize',16)
%// Rotate the label to correct orientation
LabelPos = get(get(hax2,'YLabel'),'Position');
set(get(hax2,'YLabel'),'Position',[LabelPos(1)+.2 LabelPos(2) LabelPos(3)],'Rotation',-90)
Output:
Note that it's far from perfect ...for example the left border of the first axes is not visible...that could be fixed by playing around with the position of the new axes. Hopefully it does the job for you!

Related

matlab plot different colors for different change

In https://www.amcharts.com/demos/line-different-colors-ups-downs/,
it describes plotting different colors for up and downs. How can I do the same for matlab? I attach an example
plot([1 2 3 4 5 6 7 8 9 10], [5 5 7 5 2 5 5 8 9 2])
which involves no change as well. I want to have yellow for ups and blue for downs, and red for no change.
In the figure two different colors are chosen. You may follow something like this:
x = [1 2 3 4 5 6 7 8 9 10] ;
y = [5 5 7 5 2 5 5 8 9 2] ;
figure
hold on
for i = 1:length(x)-1
m = (y(i)-y(i+1))/(x(i)-x(i+1)) ;
if sign(m)==0
plot(x(i:i+1),y(i:i+1),'r') ;
elseif sign(m)==-1
plot(x(i:i+1),y(i:i+1),'b') ;
elseif sign(m)==1
plot(x(i:i+1),y(i:i+1),'y') ;
end
end

Matlab adjusting heat map axes and colors

I'm trying to create a heat map which is something I'm not very familiar with. I have a large matrix of the form:
One=
[0 2 4 6 8
2 1 3 5 6
4 5 8 3 1
6 2 7 4 8
8 3 9 5 4]
And I want to create a heat map such that the topmost row and the leftmost column are the axes.
So far I've managed this:
figure(1)
Plot = One;
colormap('hot');
imagesc(Plot);
I've also noticed that in the 'hot' colormap, the small numbers are very dark and the large numbers are white. Is there a way to reverse that?
Here a good start:
One = ...
[0 2 4 6 8
2 1 3 5 6
4 5 8 3 1
6 2 7 4 8
8 3 9 5 4];
figure();
imagesc(One(1,:), One(:,1), One(2:end,2:end));
get(gca(), 'ydir', 'normal')
colormap(flipud(hot()));
colorbar();
Notice that the x & y axis are larger than the data, so perhaps one needs to exclude One(1,1):
figure();
imagesc(One(1,2:end), One(2:end,1), One(2:end,2:end));
get(gca(), 'ydir', 'normal')
colormap(flipud(hot()));
colorbar();
Generate the colormap with the hot function and flip it upside down with flipud:
colormap(flipud(hot))
By default this produces 64 colors. If you want to specify a different number, say 128, use
colormap(flipud(hot(128)))

Quiver from a single matrix

I have a matrix (In Matlab) which I need to plot with a quiver plot but I don't know how to turn it into a "quiver plot-able form". Are there any commands to change it? I saw some example on MathWorks Homepage using the peaks function but I couldn't get it to work.
The matrix I have is pretty huge with lots of NaN's so I created an smaller version of it.
Temperature = [ 1 2 2 2 3 4 6 7 ;
1 2 3 4 4 5 6 7 ;
2 3 4 NaN NaN 6 8 9 ;
3 4 5 NaN NaN 7 8 9 ;
4 4 6 6 7 8 10 11;
4 5 7 7 8 9 11 12];
contour(Temperature)
%quiver(Temperature)
In the comments you find the example code from the documentation which I used. Besides knowing that the dimensions are ordered [Y,X,Z] to get a matching meshgrid it's mostly copy&paste
% figure
figure
% [X,Y] = meshgrid(-2:.2:2);
Temperature = [ 1 2 2 2 3 4 6 7 ;
1 2 3 4 4 5 6 7 ;
2 3 4 NaN NaN 6 8 9 ;
3 4 5 NaN NaN 7 8 9 ;
4 4 6 6 7 8 10 11;
4 5 7 7 8 9 11 12];
[X,Y] = meshgrid(1:size(Temperature,2),1:size(Temperature,1));
% Z = X.*exp(-X.^2 - Y.^2);
Z=Temperature;
% [DX,DY] = gradient(Z,.2,.2);
[DX,DY] = gradient(Z,1,1);
% contour(X,Y,Z)
contour(X,Y,Z)
% hold on
hold on
% quiver(X,Y,DX,DY)
quiverscaling=3;
quiver(X,Y,DX,DY,quiverscaling)
% colormap hsv
colormap hsv
% hold off
hold off

matlab colormap with three columns

I want to draw a color map with three columns in matlab.
I can draw with plot3 like below,
x = [1 1 1 1 2 2 2 2 4 4 4 4 5 5 5 5 9 9 9 9];
y = [2 3 4 5 5 6 7 8 4 5 6 7 1 2 3 4 7 8 9 10];
z = [1 3 2 4 5 6 7 3 9 8 8 9 2 4 3 5 1 2 3 1];
plot3(x, y, z, 'o')
But how can I draw 2D color map with three columns?
Option 1:
If I understand you correctly you want to draw a 2D array (say m(x,y)) where the color is given by z. this is how:
m=zeros(max(x),max(y)); % preallocate m according to values of x,y
m(sub2ind(size(m),x,y))=z; % assign z-values to the x,y coordinates
imagesc(m) % plot
colormap(pink(max(z))); % set colormap with the dynamic range of z.
% you can replace it with jet or whatever...
colorbar % add a colorbar
Option 2:
you really just want to create am RGB colormap from x,y,z:
cmap=[x(:) y(:) z(:)]./max([x(:);y(:);z(:)]);
imagesc(peaks(100));
colormap(cmap);

Plot 3d surface plot in matlab/freemat

I would like to ask about 3d surface plotting. As I am new to it, I was just trying out. Basically, I have 3 parameters, x, y ,z which I have the values from experimental datas and I would like to plot them out. As such, i tried,
x= [6 7 8 9 10 11 12 1]
x =
6 7 8 9 10 11 12 1
--> y=[2 3 4 5 6 1 6 8]
y =
2 3 4 5 6 1 6 8
--> z= [3 4 5 6 7 8 9 10]
z =
3 4 5 6 7 8 9 10
meshgrid(x,y,z)
surf(x,y,z)
The plot window did come out but there was no graph. Is my method wrong?
Thanks!
It sounds like you need to start with plot3, as you're just describing a set of points in 3D, rather than points on a mesh or surface. See if that does what you want.
x = [6 7 8 9 10 11 12 1];
y = [2 3 4 5 6 1 6 8];
z = [3 4 5 6 7 8 9 10];
plot3(x, y, z, '.');
This is how I would plot a surface :
%define the data
x=[6 7 8 9 10 11 12 1 6 7 8 9 10 11 12 1];
y=[2 3 4 5 6 1 6 8 2 3 4 5 6 1 6 8];
z=[3 4 5 6 7 8 9 10 3 4 5 6 7 8 9 10];
%Create 3D surface
[X,Y]=meshgrid(x,y);
Z=griddata(x,y,z,X,Y);
%Plot the surface
surface(X,Y,Z);
shading interp %makes it look sexy
%xlim([])
%ylim([])
Sometimes I use axis limets to make the plot look nicer (eliminates the unneeded white area's); for this set of data I could use xlim([6 11]) and ylim([2 6]).