how to plot binned data?

12 visualizzazioni (ultimi 30 giorni)
Zhou Ci
Zhou Ci il 18 Mar 2022
Modificato: Adam Danz il 18 Mar 2022
Hi everyone,
I am trying to check the effect of aerosols concentration on temperature. I need to check the relation between temperature and aerosol conc. for each bin. This is my simple code for this work. I can't figure out how to make plot for these values. Using scatter gives me error because length of X and Y is not same. I need to place temperature on y-axis and aerosol concentration on x-axis. Data is attached. Thank you
Temp = Data(:,1);
Aer1 = Data(:,2);
[counts,edg,bin] = histcounts(Aer1)
counts =
85 4 4 1 0 2 1 2 0 1
edg =
1.0e-06 *
0 0.0300 0.0600 0.0900 0.1200 0.1500 0.1800 0.2100 0.2400 0.2700 0.3000
>> Med = accumarray(bin(:),Temp(:),[],@median)
Med =
-27.0000
-29.0000
-27.5000
-26.0000
0
-24.0000
-24.0000
-24.5000
0
-24.0000

Risposta accettata

Adam Danz
Adam Danz il 18 Mar 2022
Modificato: Adam Danz il 18 Mar 2022
> I need to check the relation between temperature and aerosol conc. for each bin
It looks like you're comparing the median values of aerosol for each range (bin) of temperature.
I recommend using a box plot that shows the median values (horizontal lines in the middle of the boxes) and the quartile ranges and any outliers. This gives you much more information that merely displaying the medians alone.
% Read in data as a table
T = readtable('Data.xlsx');
% Define temperature bins (bin width = 5deg)
edges = floor(min(T.temp)/10)*10 : 5 : ceil(max(T.temp)/10)*10;
% Compute bin counts
bins = discretize(T.temp,edges);
% Plot results
boxchart(bins,T.Aer1)
% Set x-tick labels to show bin edges
ax = gca();
ax.XTick = 1:max(bins);
ax.XTickLabel = compose('[%.0f, %.0f] ',edges(1:end-1)',edges(2:end)');
xlabel('Temperature (deg c)')
ylabel('Aerosol (units)')
grid on
Or, if you want to switch axes,
figure()
boxchart(bins,T.Aer1,'Orientation','horizontal')
ax = gca();
ax.YTick = 1:max(bins);
ax.YTickLabel = compose('[%.0f, %.0f] ',edges(1:end-1)',edges(2:end)');
ylabel('Temperature (deg c)')
xlabel('Aerosol (units)')
grid on

Più risposte (1)

Mathieu NOE
Mathieu NOE il 18 Mar 2022
hello
maybe this ?
you can change the number of bins (M)
Data = readmatrix('Data.xlsx');
Temp = Data(:,1);
Aer1 = Data(:,2);
M = 25; % histcounts uses M bins.
[counts,edg,bin] = histcounts(Aer1,M);
% Med = accumarray(bin(:),Temp(:),[],@median);
Med = accumarray(bin(:),Temp(:),[],@mean);
dx = mean(diff(edg));
xx = edg(1)+dx/2:dx:edg(end)-dx/2; % create a x axis centered (between edges)
ind = abs(Med)>0;% do not show Med values that are zero
plot(Aer1,Temp,'*',xx(ind),Med(ind),'-r');

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by