how to plot binned data?
조회 수: 12 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Adam Danz
2022년 3월 18일
편집: Adam Danz
2022년 3월 18일
> 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
댓글 수: 0
추가 답변 (1개)
Mathieu NOE
2022년 3월 18일
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');
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Scatter Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!