Clear Filters
Clear Filters

Why scatterplot changes when I try to make subplots?

2 views (last 30 days)
Hello everyone,
I am trying to make 9 subplots (scatterplots) with 4 variables. Temp 1, Base_Temp, Energy, Particle Conc.
My below code works fine for single plot and gives me the required results. Here I've done the binning of Energy and plot it. Temp1 is on Y-axis and Particle Conc. on X-axis. Data is attached
Data=readtable('E:\D_Table\D(01.06.2022).xlsx', 'Sheet', 1, 'ReadVariableNames', true, 'VariableNamingRule','preserve');
Data.Particle=1E9*Data.Particle;
Data.Energy_Bin=discretize(Data.Energy,[0,500,900,inf]);
figure
hAx=axes; hold on;
hL=splitapply(@plotrows,Data.Particle,Data.Temp1,Data.Energy_Bin);
hAx.XScale='log';
grid on
set(gca, 'YDir', 'reverse');
But I want 9 subplots. And I want a separate plot for each bin of energy and Base_Temp. First subplot should have energy values from 0-500, second subplot (500-900), third subplot (900-inf). I also need to do binning of Base_Temp so, for this 1st row Base_Temp should be between 0-10. For the second row Base_Temp should be between 10-20 and for third row it should be 20-25. (In all these subplots Temp1 should be on Y-axis and Particle_Conc on X-axis.
The problem is when I try to make just 3 subplots using CAPE values (with Temp1 on Y-axis and Particle_Conc on X_axis), it is not giving me correct results. I get a single point in all the 3 subplots. I used above code and just changed line 3 into:
Data.Energy_Bin=discretize(Data.Energy,[0,500]);
%I also added subplot
subplot(3,3,1)
for the second subplot again I used all the above lines while changing third one into:
Data.Energy_Bin=discretize(Data.Energy,[500,900]);
subplot(3,3,2)
Using this I am not geeting correct results. I'll be very grateful if someone can explain how can I complete this. Thank you.

Answers (1)

Walter Roberson
Walter Roberson on 25 Aug 2022
discretize() of double() data returns NaN if the data is outside of the range of the bins specified.
Otherwise discretize() returns the bin number such that bin(i) <= x < bin(i+1), except in the case of the last bin, in which case the test is bin(i) <= x <= bin(i+1) (so the exact upper limit is included in the last bin.
Thus, discretize of double() data returns either nan or an integer that is in the range 1 to length(bins)-1
You are passing in exactly two values for the bins, such as [0, 500] . As a result, you are going to be getting a mix of nan (value not in the range 0 <= x <= 500) or 1 (data fits in first and only bin.)
I suspect that instead of passing in [0, 500] that you should be passing in something finer grained, such as 0:50:500 .
Or perhaps what you should be doing is extracting subsets of data,
mask = Data.Energy >= 0 & Data.Energy < 500;
E1 = Data.Energy(mask);
Data.Energy_Bin = discretize(E1, N);
where N is number of bins (but remember that the bins will be dynamically created according to input values, so the plot could vary depending on the data; you would probably be safer with linspace(0,500,N)
  2 Comments
Zhou Ci
Zhou Ci on 26 Aug 2022
@Walter Roberson Thank you for this explanation.
Unfortunately I am not able to solve the issue. Based on your suggestion I did this.
mask = Data.Energy >= 0 & Data.Energy < 500;
E1 = Data.Energy(mask);
N = 0:50:500
Data.Energy_Bin = discretize(E1, N);
It shows following error:
To assign to or create a variable in a table, the number of rows must match the height of the table.
I think it's because my Data.Energy is 185x1 and E1 after doing masking becomes 145x1. How can I solve this.
Walter Roberson
Walter Roberson on 26 Aug 2022
%initialize
Data.Energy_Bin = -inf * ones(height(Data),1);
%then
mask = Data.Energy >= 0 & Data.Energy < 500;
E1 = Data.Energy(mask);
N = 0:50:500;
Data.Energy_Bin(mask) = discretize(E1, N);
If you do this, then the Energy_Bin for each entry will be "local" -- the bin number within the range you are examining.
Perhaps you should be using a different approach, discretizing all of Data.Energy at (for example) increments of 50 -- and then as you loop though the ranges, you could histogram the same data each time, but just use different xlim() for each plot (unless you ask histogram to normalize, in which case you need to think about whether you want to normallize relative to overall value or relative to the particular range.)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!