Plotting negative errorbar on semilogx

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

You'll need to attach the data and code here...
I have added the google drive link.
@Sonali, I looked at your google drive. You cannot expect people to download 150 files and try to run your code. Very few people, if any, will do that. You need to construct a small example that illustrates the problem you are having, and post it here.
please check now
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)

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

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.

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

That will indeed show error bars, but this solution assumes that what is stored in the variable err is actually the log10 exponent of the error. Could be, but seems unlikely to me.
I don't see how to make progress on this until @Sonali responds to these comments and proposed solutions.
This worked for one case that I shared earlier. But it is not working for all cases. I have added few columns for which it didnt work. @Sulaymon Eshkabilov @the cyclist
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.
"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.

Asked:

on 24 Jan 2024

Commented:

dpb
on 1 Feb 2024

Community Treasure Hunt

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

Start Hunting!