How to take average for each bin and then plot it?

23 views (last 30 days)
IMC
IMC on 25 Jun 2021
Edited: IMC on 26 Jun 2021
Hello everyone,
I am trying to bin and then plot the binned data. My data is radius = 19125x1 double and temperature is 19125x1 double. I want to bin radius according to temperature and also calculate the average radius for each temperature bin.
I used below line of code for this purpose:
[N, temp] = histcounts(rad);
N =
Columns 1 through 11
5 73 295 656 1033 982 932 812 671 489 518
Columns 12 through 22
461 384 262 300 302 227 191 205 185 149 137
Columns 23 through 28
181 142 97 47 30 18
temp =
Columns 1 through 22
4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46
Columns 23 through 29
48 50 52 54 56 58 60
I want figure like this, where on X-Axis radius is plotted and temperature is plotted on Y-axis.
Any help is highly appreciated. Thank you
  2 Comments
Scott MacKenzie
Scott MacKenzie on 25 Jun 2021
It might help if you posted your data (or a subset of the data).
IMC
IMC on 25 Jun 2021
Sure Sir, here is my data:
Column A represents temperature and column B is for Radius.

Sign in to comment.

Answers (1)

Scott MacKenzie
Scott MacKenzie on 25 Jun 2021
Here's what I put together. Looks like most of the 19124 data points have NaN for either temperature or data. There are 6901 points with a valid temperature and radius measurement. The first plot is your average radius for each temperature bin graph. I don't think you're going to get anything close to the tempearture vs. radius plot example in your question. In the scatter plot, you can see a genearal lack of trend between these two variables. The temperatures are all negative, but I suppose you're aware of that. Anyway, hope this helps. Good luck.
f = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/665795/Data_temp_radius.xlsx';
M = readmatrix(f);
temp = M(:,1); % y-axis data
rad = M(:,2); % x-axis data
% number of data points with temperature *and* radius measurement
nData = sum(~isnan(temp) & ~isnan(rad))
tiledlayout(2,1);
% plot mean radius by temperature bin
nexttile;
[~, edges] = histcounts(temp);
y = discretize(temp, edges);
m = grpstats(rad, y);
bar(edges(1:end-1), m);
set(gca, 'xtick', edges(1:end-1));
xlabel('Temperature');
ylabel('Mean Radius');
% radius vs. temperature scatter plot
nexttile;
scatter(rad, temp, '.')
xlabel('Radius');
ylabel('Temperature');
  1 Comment
dpb
dpb on 25 Jun 2021
I was just ready to paste in the same conclusion -- with the comment of "you can't always have what you want!" :)
These data will not come close to producing anything at all like the associated figure; there's essentially no correlation between the temperature and the radius measurements -- and it weren't for the discrete nature of the temperature measurement there would be no pattern whatsoever in the scatter plot -- as it is, all one sees is the spacing between the measurement levels.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!