Plotting negative errorbar on semilogx

6 views (last 30 days)
Sonali
Sonali on 24 Jan 2024
Commented: dpb on 1 Feb 2024
Hi I have got some half errorbars plotted on the semilogx. I understand that its possibly because of negative/complex values. I have tried most of the suggestions made to the previous posts. But they seem to not work with my program. Will really appreciate some help here.
Please find the file of data attached.
I am trying to errorbars for plot mid(Y) v/s density(X) on a semilogx using command below:
semilogx(density,mid,'*m',LineStyle='none',MarkerSize=4)
hold on
errorbar(density,mid,err,'horizontal',LineStyle='none', Color='red',LineWidth = 0.1,CapSize=2)
  5 Comments
the cyclist
the cyclist on 24 Jan 2024
Let's take a look at what your data + error bars look like, plotted in linear space:
data = readtable("file.xlsx");
density = data.density;
mid = data.mid;
err = data.err;
figure
plot(density,mid,'*m',LineStyle='none',MarkerSize=4)
hold on
errorbar(density,mid,err,'horizontal',LineStyle='none', Color='red',LineWidth = 0.1,CapSize=2)
and then just the data (without error bars) in log space:
data = readtable("file.xlsx");
density = data.density;
mid = data.mid;
err = data.err;
figure
semilogx(density,mid,'*m',LineStyle='none',MarkerSize=4)
As you realize, the error bars extend into the negative numbers, so their log will be complex.
But what do you want to show on the plot? More importantly, are those really the correct errorbars? I expect that the density cannot actually go negative, so that is the real problem.

Sign in to comment.

Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 24 Jan 2024
This is how the nagtive values can be displayed in the plot:
D = readtable('file.xlsx');
IDX1 = log10(D.density)>0;
semilogx(D.density(IDX1),D.mid(IDX1),'*m',LineStyle='none',MarkerSize=4)
hold on
errorbar(D.density(IDX1),D.mid(IDX1),D.err(IDX1),'horizontal',...
LineStyle='none', Color='red',LineWidth = 0.1,CapSize=2)
IDX2 = log10(D.density)<=0;
Offset = 1e-13;
Density_Neg=D.density(IDX2)+Offset;
Mid_Neg = D.mid(IDX2);
Err_Neg = D.err(IDX2);
semilogx(Density_Neg,Mid_Neg,'bo',LineStyle='none',MarkerSize=6)
errorbar(Density_Neg,Mid_Neg,Err_Neg,'horizontal',LineStyle='none', Color='b',LineWidth = 0.2,CapSize=3)
hold off
legend('Mid: Density_{Pos}','Err: Density_{Pos}', 'Err: Density_{Neg}', ...
'Mid: Desnity_{Pos}', 'Location', 'Best')
Even though it says ignored but they are displayed as shown and can be tested.
N_total = numel(D.density) % ALL
N_total = 68
N_pos = sum(IDX1) % Positive
N_pos = 61
N_neg = sum(IDX2) % Negative
N_neg = 7
Warning: Negative data ignored
The warning pops up because of the MATLAB's graphics set up:
In matlab.graphics.shape.internal.AxesLayoutManager>calculateLooseInset
In matlab.graphics.shape.internal/AxesLayoutManager/updateStartingLayoutPosition
In matlab.graphics.shape.internal/AxesLayoutManager/doUpdate
  1 Comment
the cyclist
the cyclist on 24 Jan 2024
I don't think this solution is handling the case where density is positive, but (density - err) is negative. Those are the points that have only "half" the errorbar (in the positive direction), and are at the heart of the question.

Sign in to comment.


Sulaymon Eshkabilov
Sulaymon Eshkabilov on 24 Jan 2024
Edited: Sulaymon Eshkabilov on 25 Jan 2024
It looks like this is how to display the missing half of error bar values. The solution with the x- axis limit set up:
D = readtable('file.xlsx');
IDX1 = log10(D.density)>0;
semilogx(D.density(IDX1),D.mid(IDX1),'*m',LineStyle='none',MarkerSize=4, Marker="square")
hold on
errorbar(D.density(IDX1),D.mid(IDX1),log10(D.err(IDX1)),'horizontal',...
LineStyle='none', LineWidth = 0.1,CapSize=2)
xlim([20 150])
  4 Comments
the cyclist
the cyclist on 1 Feb 2024
You never answered the question I asked in my earlier comment.
Your error bars extend to negative numbers. But surely you can't actually have negative density. How do you want to handle that?
You cannot make a sensible plot from non-sensible data.
dpb
dpb on 1 Feb 2024
"surely you can't actually have negative density. How do you want to handle that?"
On log scale, one would generally use proportional error bands, not absolute, then the negative values wouldn't show up.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!