Matlab Bar Graph containing 2 bars - matlab

I am trying to make bar with containing 2 bars. Below is the code to make a single bar graph from an excel file. However, idk how to make a bar graph containing 2 bars. I need to take data from 2 excel files. 1 is Locations vs NO2 and other one is Locations vs SO2. The locations are the same, but I need to compare NO2 and SO2. Can anyone help me out?
Airpollution = readtable ('Location vs No2.xlsx');
x = categorical(Airpollution{:,1});
y = Airpollution{:,2};
bar(x,y)
xlabel('Location');
ylabel('Concentration of No2');
title('Location vs Max concentration of No2');

I think I know how... I found this some days ago somewhere in google:
figure(1);
[dummy, t] = hist([one;two], numBin);
nx = hist(one, t); % Sort x into bins.
nx = transpose(nx/sum(nx));
ny = hist(two, t); % Sort y into bins.
ny = transpose(ny/sum(ny));
bar(t, [nx, ny]);

Related

matlab, plotting 2 vecctors as a list, how can I give them different line types and colours using this syntax

I have checked SO and ML help but cannot see a solution to this.
using this syntax
E = 10; % amplitude
sample_1_hz = 1000;
sample_2_hz = 11000;
fs = 10000; % sample rate in Hz
Samples = 100;
time_scale = (0:Samples-1)'/fs;
sig_1 = E*sin(2*pi*time_scale*sample_1_hz);
sig_2 = E*sin(2*pi*time_scale*sample_2_hz);
plot(time_scale,[sig_1 sig_2]);
grid('on');
xlabel('Time');
ylabel('Amplitude');
legend('1000 Hz', '11000 Hz');
how can I alter the lines to have different line styles and colors?
the two lines coincide in every point in time_scale in the example code you posted (plot their difference sig_1-sig_2 and see the values in the 1e-13), so the only way to see both of them is to make one "dashed" and the other "solid" so you can see one on top of the other , or add a marker to one of them. for example,
plot(time_scale, sig_1 ,'-',time_scale,sig_2,':o','LineWidth',2 );

How to plot a Diagonal Histogram in Matlab

Given scatter data, or a matrix, I would like to generate a nice plot such as the one shown below, with all 3 histograms and a colored matrix. I'm specifically interested in the diagonal histogram, which ideally, would correspond to the diagonals of a matrix:
Source figure: www.med.upenn.edu/mulab/jpst.html
The existing command scatterhist is not that powerful to generate this type of graph. Any ideas?
Thanks!
EDIT:
Following #Cris Luengo's hints, I came up with the following code which does some first work at the inclined histogram: WORK IN PROGRESS (HELP WELCOME)!!
b = [0 1 2 3 4 5 6 7 8 9 10];
h = [0.33477 0.40166 0.20134 0.053451 0.008112 0.000643 2.7e-05 0 0 0 0];
wid = 0.25; bb = sort([b-wid b-wid b+wid b+wid]);
kk = [zeros(numel(h),1) h(:) h(:) zeros(numel(h),1)];
kk = reshape(kk',[1,numel(kk)]);
pp=patch(bb,kk,'b');axis([-.5 5 0 .5])
set(gca,'CameraUpVector',[-1,.08,0]);axis square
EDIT 2: Using rotation
phi = pi/4;
R = [cos(phi),-sin(phi);sin(phi),cos(phi)];
rr = [bb' kk'] * R;
bb = rr(:,1); kk = rr(:,2);
patch(bb,kk,'b'); axis([-.5 3 -4 .5])
Here is a recipe to plot the diagonal histogram, if you can do that I’m sure you can figure out the rest too.
Compute the histogram, the bin counts are h, the bin centers are b.
Build a coordinate matrix, attaching the coordinates of a point on the x-axis at the left and right ends of the histogram:
coords = [b(:),h(:)];
coords = [coord;b(end),0;b(1),0];
Using patch you can now plot the histogram as follows:
patch(coords(1,:),coords(2,:));
To plot a rotated histogram you can simply multiply the coords matrix with a rotation matrix, before using patch:
phi = pi/4;
R = [cos(phi),-sin(phi);sin(phi),cos(phi)];
coords = R * coords;
You might need to shift the plot to place it at the right location w.r.t. the other elements.
I recommend that you place all these graphic elements in the same axes object; you can set the axes’ visibility to 'off' so that it works only as a canvas for the other elements.
It will be a bit of work to get everything placed as in the plot you show, but none of it is difficult. Use the low-level image, line,patch and text to place those types of elements, don’t try to use the higher-level plotting functions such as plot, they don’t provide any benefits over the low-level ones in this case.

How can I specify different points in the plot in matlab

I have generated a data set in matlab then some outliers embedding in the data. I would like to plot it and since I'm new in matlab I don't know how to specify the outliers from inliers by different sign or different color. The points which are outlyingness with respect to the x axis, y axis and both of them. This is the matlab codes for that;
pd = makedist('Normal');
rng(38)
a = random(pd,100,1);
b = datasample(1:100,40,'Replace',false);
pd1 = makedist('Normal','mu',10*sqrt(2),'sigma',0.1);
a(b)=random(pd1,40,1);
a=reshape(a,[50,2]);
plot(a(:,1),a(:,2),'O')
I would be appreciated if you could help me.
In this example I assumed that the points which distance along OX axis is greater than 3 are outliers and marked them red (whereas normal points are marked blue):
centroid = mean(a);
distx = a(:,1) - centroid(1);
disty = a(:,2) - centroid(2);
outliers_x = distx > 3;
plot(centroid(1), centroid(2), 'xk')
hold on
plot(a(outliers_x,1),a(outliers_x,2),'or')
plot(a(~outliers_x,1),a(~outliers_x,2),'ob')
hold off
Note that I've also displayed the centroid as a black "X" mark.
hold on/hold off are used to "stack" several plots (or images) together
You may want to read hold() reference. Also here you'll find which markers and colors are available.
To answer to my question I have written the following codes, in order to specify 4 groups of observations with different color.
pd = makedist('Normal');
rng(38)
a = random(pd,100,1);
b = datasample(1:100,40,'Replace',false);
pd1 = makedist('Normal','mu',10*sqrt(2),'sigma',0.1);
a(b)=random(pd1,40,1);
a=reshape(a,[50,2]);
hold all;
aa=(a >= 10 | a >= 10);
rep=repmat(0, 1, 50);
aaa=[rep',aa];
n=50;
for i=1:n; plot(a(i,1),a(i,2),'o','col',aaa(i,:));
end

Finding top point in two gaussians

So i have this graph with these two gaussians created in matlab
This Graph have been created using the following bit of matlab code
TimeTakenWDriver = textread('TimeTakenWDriver.txt');
TimeTakenWODriver = textread('TimeTakenWODriver.txt');
fig1 = figure;
h1 = histfit(TimeTakenWDriver);
std1 = std(TimeTakenWDriver);
std2 = std(TimeTakenWODriver);
mean1 = mean(TimeTakenWDriver)
mean2 = mean(TimeTakenWODriver)
delete(h1(1));
set(h1(2),'color','b');
hold on;
h2 = histfit(TimeTakenWODriver);
delete(h2(1));
set(h2(2),'color','r');
Now i want to find the two y coordinates that corresponds to the two means "the two top-points" ive searched around but cant get any of the solutions i find to work because of the way i create my graphs. Any ideas?
You can try getting the data directly from the plot:
C = get(get(gca, 'Children'), 'YData');
and then find the peak value using max:
max(C(:))
Note that if you have two or more plots on the same axes (as in your example), C would be a cell array, so you need to access each cell separately to get the peaks:
cellfun(#(x)max(x(:)), C)

Simple MATLAB Graph Plotting

This may be an obviously simple question, but i'm not sure how to do this.
I have 4 calculated values stored in 4 variables, each representing a condition. I want to simply display each of these in a graph with the condition/variable on the X axis and the values on the Y axis. I have tried the code below, but it just gives me a blank figure with values but no line.
figure(1)
T = TA;
S = SA;
U = UA;
O = OA;
plot(T,S,U,O, '--o')
shg
Thanks in advance.
Try this
figure(1)
T = 12;
S = 7;
U = 5;
O = 10;
plot([T,S,U,O], '--o');
set(gca,'XTick',[1,2,3,4]);
set(gca,'XTickLabel',{'T','S','U','O'})
shg
for me this gave
Check matlab help on plot and specifically linespecs to define the format you would like the data plotted in. For example, code below plots each variable in different color with line and symbol.
figure;
hold on;
plot(T,'-bo');
plot(S,'-g.');
plot(U,'-rd');
plot(O,'-mx');