Plotting negative errorbar on semilogx

18 次查看(过去 30 天)
Sonali
Sonali 2024-1-24
评论: dpb 2024-2-1
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 个评论
the cyclist
the cyclist 2024-1-24
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.

请先登录,再进行评论。

回答(2 个)

Sulaymon Eshkabilov
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 个评论
the cyclist
the cyclist 2024-1-24
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.

请先登录,再进行评论。


Sulaymon Eshkabilov
编辑:Sulaymon Eshkabilov 2024-1-25
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 个评论
the cyclist
the cyclist 2024-2-1
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 2024-2-1
"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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Errorbars 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by