Plotting negative errorbar on semilogx
조회 수: 17 (최근 30일)
이전 댓글 표시
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
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
2024년 1월 24일
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_pos = sum(IDX1) % Positive
N_neg = sum(IDX2) % Negative
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
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
2024년 1월 24일
편집: 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
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
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 Center 및 File Exchange에서 Errorbars에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!