d=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16]
bar_widh=0.2;
figure;
h = bar(d,bar_widh);
for i=1:2:4
for j=1:4
x=d(i,j)
y=d(i+1,j)
figure,plot(x,y);
end
i=i+1;
end
In this code I had plotted a bar chart and I want to extract values from bar chart. (Values are correctly extracted but only one point is plotted; not a line) But i'm getting wrong results.
My aim is to plot a line between
d(1,1) and d(2,1);d(3,1) and d(4,1);
d(1,2) and d(2,2);d(3,2) and d(4,2);
d(1,3) and d(2,3);d(3,3) and d(4,3);
d(1,4) and d(2,4);d(3,4) and d(4,4);
In first figure I need 2 lines(from 1 column); in second figure I need 2 lines(from 2 column); in third figure I need 2 lines(from 3 column) and in fourth figure I need 2 lines(from 4 column).
no.of figures=no.of columns
Version 2
I tried another version
d=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16]
bar_widh=0.2;
figure;
h = bar(d,bar_widh);
saveas(h,'testfigure.fig');
clear
close all
h=hgload('testfigure.fig');
ch=get(h,'Children');
l=get(ch,'Children');
x=get(l,'Xdata');
y=get(l,'Ydata');
and i'm getting error as
Error using get
Conversion to double from cell is not possible.
Error in Untitled5 (line 10)
l=get(ch,'Children');
d=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16];
bar_widh=0.2;
figure;
h = bar(d,bar_widh);
figure; hold on;
for i = 1:size(d,2)
x = [d(1,i) d(2,i)];
y = [d(3,i) d(4,i)];
plot(x,y);
end
To plot a line, you must make sure the parameters x and y in plot(x,y) are vectors than scalars. For example, to plot a line between P1 = [P1x, P1y] and P2 = [P2x, P2y], the paramenters should be:
x = [P1x, P2x]; y = [P1y, P2y];
Related
I have this type of data:
Data = [1:1:9; 1 2 3 4 5 6 7 8 9; 1 2 3 4 5 6 7 8 9 ;1 2 3 4 5 6 7 8 9;1 2 3 4 5 6 7 8 9];
Where the first entry is the zone of interest (9 zones total) and the remaining 4 array entries are how long something stayed in the zone (just arbitrary now for this question.
I want to plot how long each thing is in each zone on a picture of a map as a 3d "heatmap" style plot. The map is divided into a 3x3 grid like so:
1 2 3
4 5 6
7 8 9
How do I plot this? I have tried the contour and surf functions but I am not sure how I would map them to the grid. I would like to overlay the graph on a map picture as my final step but just getting the graph up and running would be great!
You can use imagescfor the 2D version or bar3 for the 3D version, in this example I created some subplots, but of course you are free to change this option.
Data = [1:1:9; 1 2 3 4 5 6 7 8 9; 1 2 3 4 5 6 7 8 9 ;1 2 3 4 5 6 7 8 9;1 2 3 4 5 6 7 8 9];
M = zeros(3,3);
2D
for ii = 2:size(Data,1)
subplot(2,2,ii-1)
M(1:end) = Data(ii,:);
imagesc(M)
colormap jet
shading flat %for an exact result
% shading interp %for a smooth result
end
figure
3D (even if I think that the 3D view is useless)
for ii = 2:size(Data,1)
subplot(2,2,ii-1)
M(1:end) = Data(ii,:);
h{ii} = bar3(M)
colorbar
for k = 1:length(h{ii})
zdata = h{ii}(k).ZData;
h{ii}(k).CData = zdata;
h{ii}(k).FaceColor = 'interp';
end
end
I'm not sure if this is the kind of plot that you want, but you could use Delaunay Triangulation to map your duration data as a surface over your grid points. The MatLab file exchange has a nice function for mapping something using this method.
Making Surface Plots from Scatter Data
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)))
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!
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);
I need to simple plot B vs. A as bar plot in MATLAB, but I don't want my x axis showing completely from 1 to 274. I only need to show the existing data point on my x axis, which can be done easily in Excel as in the image below. How can MATLAB do this?
A=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 20 25 27 29 37 40 42 43 48 73 204 242 274];
B=[30 15 5 9 5 6 3 3 2 1 4 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1];
You need to set both 'XTick' and 'XTickLabel' axes properties:
bar(B);
set(gca,'XTickLabel',A)
set(gca,'XTick',1:numel(A));
xlim([0 numel(A)+1]);
Here is an inelegant, but, nonetheless, working solution to your question:
x = [1,4, 6, 7]; % Your data
uni = unique(x)
yMax = length(find(x == mode(x))) + 1;
c = cell(1, length(uni));
c = strread(num2str(uni),'%s')
hist(1:length(uni));
axis([0 length(uni) 0 yMax])
set(gca, 'XTick', 1:length(uni));
set(gca, 'XTickLabel', c);
Basically, this plots the histogram as if the data were spread from 1 to the number of unique elements. Then, it sets the tick marks at each histogram value. Then, it labels each tick mark with the correct number.