Format scientific decimal place bar plot

Hello there,
I want to make a data tip of the bar plot appeared in scientific format with one decimal place (example: 2.1x10^-3 instead of 0.0021). Anyone can help?
Here is my code so far, and please find the datasets attached:
load('data_c.mat');
x = unique_stations';
vals = [means_ep; means_kzo];
b = bar(x,vals);
set(gca,'yscale','log');grid on; box on;
xtips1 = b(1).XEndPoints;ytips1 = b(1).YEndPoints;
xtips2 = b(2).XEndPoints;ytips2 = b(2).YEndPoints;
labels1 = string(b(1).YData);labels2 = string(b(2).YData);
text(xtips1,ytips1,labels1,'HorizontalAlignment','center','VerticalAlignment','bottom') % should be adjusted here, I think
text(xtips2,ytips2,labels2,'HorizontalAlignment','center','VerticalAlignment','bottom') % should be adjusted here, I think
legend('mean e','mean kz');
I want this version:
Thank you!

 採用された回答

Star Strider
Star Strider 2024 年 11 月 10 日
編集済み: Star Strider 2024 年 11 月 10 日

0 投票

I wrote some utility functions for just this purpose for my own use a few years ago.
Try this —
load('data_c.mat');
x = unique_stations';
vals = [means_ep; means_kzo];
b = bar(x,vals);
set(gca,'yscale','log');grid on; box on;
xtips1 = b(1).XEndPoints;ytips1 = b(1).YEndPoints;
xtips2 = b(2).XEndPoints;ytips2 = b(2).YEndPoints;
expstr = @(x) [x(:).*10.^ceil(-log10(abs(x(:)+(x==0)))) floor(log10(abs(x(:)+(x==0))))]; % Updated: 2021 05 04
% labels1 = string(b(1).YData);labels2 = string(b(2).YData)
labels1 = compose("%.1f\\times 10^{%d}", expstr(b(1).YData.'));
labels2 = compose("%.1f\\times 10^{%d}", expstr(b(2).YData.'));
text(xtips1,ytips1,labels1,'HorizontalAlignment','center','VerticalAlignment','bottom') % should be adjusted here, I think
text(xtips2,ytips2,labels2,'HorizontalAlignment','center','VerticalAlignment','bottom') % should be adjusted here, I think
legend('mean e','mean kz');
EDIT — Corrected typographical errors.
.

7 件のコメント

Adi Purwandana
Adi Purwandana 2024 年 11 月 10 日
Just perfect as always! Thank you @Star Strider!
Star Strider
Star Strider 2024 年 11 月 10 日
As always, my pleasure!
Adi Purwandana
Adi Purwandana 2024 年 11 月 10 日
編集済み: Adi Purwandana 2024 年 11 月 10 日
Sorry @Star Strider; my additional question if you don't mind. What if the case is and 'ordinary' plot? Any adjustment (I think so) to add the values in the plot?
figure;
x=datetimeDate';
plot(x, means_ep, '--bs', 'LineWidth', 0.5,'MarkerFaceColor','b'); hold on;
set(gca,'yscale','log')
expstr = @(x) [x(:).*10.^ceil(-log10(abs(x(:)+(x==0)))) floor(log10(abs(x(:)+(x==0))))]; % Updated: 2021 05 04
labels1 = compose("%.1f\\times 10^{%d}", expstr(means_ep'));
text(x, means_ep,labels1,'HorizontalAlignment','center','VerticalAlignment','bottom') % should be adjusted here, I think
Please find an example datasets attached.
Adi Purwandana
Adi Purwandana 2024 年 11 月 10 日
Done! sorry...
Star Strider
Star Strider 2024 年 11 月 10 日
No worries!
My apologies for missing this. (Off doing other things for a few minutes this morning.)
This appears to work as written. The only improvemeenet I can suggest is to add:
axis('padded')
or something else to expand the axis limits so that everything plots within the axes boundary —
load('data_dated.mat')
whos('-file','data_dated.mat')
Name Size Bytes Class Attributes datetimeDate - 78 datetime means_ep 1x6 48 double
figure;
x=datetimeDate';
plot(x, means_ep, '--bs', 'LineWidth', 0.5,'MarkerFaceColor','b'); hold on;
set(gca,'yscale','log')
expstr = @(x) [x(:).*10.^ceil(-log10(abs(x(:)+(x==0)))) floor(log10(abs(x(:)+(x==0))))]; % Updated: 2021 05 04
labels1 = compose("%.1f\\times 10^{%d}", expstr(means_ep'));
text(x, means_ep,labels1,'HorizontalAlignment','center','VerticalAlignment','bottom') % should be adjusted here, I think
axis('padded')
figure;
x=datetimeDate';
plot(x, means_ep, '--bs', 'LineWidth', 0.5,'MarkerFaceColor','b'); hold on;
set(gca,'yscale','log')
expstr = @(x) [x(:).*10.^ceil(-log10(abs(x(:)+(x==0)))) floor(log10(abs(x(:)+(x==0))))]; % Updated: 2021 05 04
labels1 = compose("%.1f\\times 10^{%d}", expstr(means_ep'));
text(x, means_ep,labels1,'HorizontalAlignment','left','VerticalAlignment','bottom') % should be adjusted here, I think
xlim(xlim+[-minutes(5) +minutes(75)])
ylim(ylim+[-2E-9 +2E-7])
Otherwise, setting 'HorizontalAlignment','left' as well could work. (I experimented with that here.)
.
Adi Purwandana
Adi Purwandana 2024 年 11 月 11 日
Nice, Thanks!
Star Strider
Star Strider 2024 年 11 月 11 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (2 件)

Shashi Kiran
Shashi Kiran 2024 年 11 月 10 日

0 投票

To display data tips in scientific notation with one decimal place on your bar plot, you can format the labels1 and labels2 variables by converting the YData values into strings with scientific notation.
In MATLAB, the "num2str" function can achieve this formatting. Here is how to adjust your code:
% Format labels in scientific notation with one decimal place using num2str
labels1 = num2str(b(1).YData', '%.1e'); % Transpose to column vector for num2str
labels2 = num2str(b(2).YData', '%.1e'); % Transpose to column vector for num2str
For more details about "num2str" refer to this documentation: https://www.mathworks.com/help/matlab/ref/num2str.html
Hope this helps.
Bruno Luong
Bruno Luong 2024 年 11 月 10 日
編集済み: Bruno Luong 2024 年 11 月 10 日

0 投票

load('data_c.mat');
x = unique_stations';
vals = [means_ep; means_kzo];
b = bar(x,vals);
set(gca,'yscale','log');grid on; box on;
xtips1 = b(1).XEndPoints;ytips1 = b(1).YEndPoints;
xtips2 = b(2).XEndPoints;ytips2 = b(2).YEndPoints;
labels1 = mystr(b(1).YData);labels2 = mystr(b(2).YData);
text(xtips1,ytips1,labels1,'HorizontalAlignment','center','VerticalAlignment','bottom') % should be adjusted here, I think
text(xtips2,ytips2,labels2,'HorizontalAlignment','center','VerticalAlignment','bottom') % should be adjusted here, I think
legend('mean e','mean kz');
function sc = mystr(a)
nd = floor(log10(a));
sc = arrayfun(@(x,n)sprintf('%1.1f x 10^{%d}',x,n), a./10.^nd, nd, 'unif', 0);
% EDIT: ADD CODE TO REMOVE EXPONENT WHEN IT IS EQUAL TO 1
sc = strrep(sc, ' x 10^{0}','');
end

1 件のコメント

Adi Purwandana
Adi Purwandana 2024 年 11 月 10 日
Good to know the function. Thanks @Bruno Luong!

サインインしてコメントする。

カテゴリ

製品

リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by