Log axis with percentage tick labels

17 ビュー (過去 30 日間)
Dmitriy Saltanov
Dmitriy Saltanov 2020 年 9 月 24 日
コメント済み: Steve Eddins 2020 年 9 月 24 日
Hi, just starting to use matlab here, running a few experimental calculations and pretty excited so far.
I'm currently struggling with displaying a graph of cumulative relative returns where changing value axis scale to log seems to defeat percentage formatting and override it with a default format, making presentation hard to grasp.
figure;
ax = axes;
plot(sourceData.Date, sourceData.ResultCompound);
ytickformat(ax, 'percentage'); % also tried ytickformat(ax, '%0.2f%%')
ax.YScale = 'log'; % no problem with default/'linear'
So my question is, is there a way to make log-scaled graphs with percentage tick labels?

採用された回答

Star Strider
Star Strider 2020 年 9 月 24 日
I am not certain what you want to do.
Try this:
x = linspace(-2, 2, 50); % Create Data
y = exp(-(x).^2/2); % Create Data
figure
semilogy(x, y)
grid
yt = get(gca, 'YTick');
ytl = compose('%.0f%%', yt*100)
set(gca, 'YTick',yt, 'YTickLabel',ytl)
.
  2 件のコメント
Dmitriy Saltanov
Dmitriy Saltanov 2020 年 9 月 24 日
Thank you, I played with your code and it looks like a good starting point. What I need is actually kind of simple graph that can display a few scalar growth rate-based cumulative indicators over time, such as relative total returns from financial instruments, inflation and synthetic growth indices etc. Semilog scale offers the best representation for the visual assessement of these, and since these indicators are usually expressed as % of starting value per time-period such as per annum, percentage format is the most convenient.
Star Strider
Star Strider 2020 年 9 月 24 日
My pleasure!
I am not certain what you want. It is straightforward to define new ticks and tick labels.
This one change to my original code does that:
x = linspace(-2, 2, 50); % Create Data
y = exp(-(x).^2/2); % Create Data
figure
semilogy(x, y)
grid
yt = get(gca, 'YTick');
ytn = linspace(min(yt), max(yt), 4); % Define Nes ‘ytick’ Values
ytl = compose('%.1f%%', ytn*100)
set(gca, 'YTick',ytn, 'YTickLabel',ytl)
The new tick values always need to be within the range of the current tick values, however within that range they can be anything.

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

その他の回答 (1 件)

Steve Eddins
Steve Eddins 2020 年 9 月 24 日
From what I can tell, the ruler object ignores the TickLabelFormat property (which is set by ytickformat) when the Scale is 'log'. I will record an enhancement/documentation request about this.
As a workaround, you can set the tick labels directly to whatever strings you want by using yticklabels. Try something like this:
yticklabels(string(yticks) + "%")
  2 件のコメント
Dmitriy Saltanov
Dmitriy Saltanov 2020 年 9 月 24 日
Thank you very much. I'm sure that beside ensuring "orthogonality" vis a vis linear scale, making this feature more straighforward would add good value for financial applications where there's need to compare cumulative returns from scalar growth rates, typically expressed with % per time-period.
The workaround you offered seems to work fine but I think I'll be working off Star Rider's solution since it offers a glance at accessing more features along the way (in particular setting the labels).
Steve Eddins
Steve Eddins 2020 年 9 月 24 日
Thanks, Dmitriy. I have your comments to the enhancement request record.

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

カテゴリ

Help Center および File ExchangeSpecifying Target for Graphics Output についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by