Plotting Chart With 2 X Axes and 2 Y Axes
4 ビュー (過去 30 日間)
古いコメントを表示
Hi there,
I am trying to plot various curves using semilogx along with the line of best fit. I also want to plot some other plots of semilogx on different axes (but in the same figure). I have:
ARI = ones(n,1)./P; % Average Return Interval 1/P
semilogx((ARI), Q);
p_1 = polyfit(log(ARI),Q,1); % Linear best fit
f_1 = polyval(p_1,log(ARI));
hold on
ax1 = gca; % Axis for Linear Fit
ax1.XColor = 'r';
ax1.YColor = 'r';
ax1_pos = ax1.Position; % Position of first axes
ax2 = axes('Position', ax1_pos, 'XAxisLocation','top','YAxisLocation',"right",'Color', 'none'); % Create the second axis
semilogx(ARI, f_1, '--r');
Q_100yr = polyval(p_1, log(100)) % Q value for 1 in 100 year
hold off
For some reason when I try to create the second axis, the position of the axis is not as I would like and I lose my original log curve. It ends up looking like this:
Could someone help me to get the 2 axes working.
Thanks in advance,
Brian
0 件のコメント
採用された回答
Adam Danz
2020 年 5 月 26 日
編集済み: Adam Danz
2020 年 5 月 26 日
In the example plot provided in the question, the x axis is the same for both pairs of axes. To use a left and right axes with the same x axis, se yyaxis.
ARI = ones(n,1)./P; % Average Return Interval 1/P
yyaxis left
semilogx((ARI), Q);
yyaxis right
p_1 = polyfit(log(ARI),Q,1); % Linear best fit
f_1 = polyval(p_1,log(ARI));
semilogx(ARI, f_1, '--r');
Q_100yr = polyval(p_1, log(100)) % Q value for 1 in 100 year
6 件のコメント
Adam Danz
2020 年 5 月 28 日
I'm assuming you want to maintain the log scale. Otherwise, you could just use plot instead of semilogx. Or,
ax = gca();
ax.XScale = 'linear'; % = 'log' to switch back to log scale
Here's how to change the x tick format from exponential to their log10 values.
ax.XTick = ax.XTick; % important to set the xtick first
ax.XTickLabel = cellstr(num2str(ax.XTick'));
Here's how to specify the number of ticks along the x axis in log space. Note that the ticks here are rounded to the nearest 10th but you can adapt as needed. The first line below produces 6 tics spanning the range of the x axis.
xl = xlim(ax); % xlim cannot contain 0, of course.
newTicks = logspace(log10(xl(1)), log10(xl(2)), 6);
ax.XTick = newTicks;
ax.XTickLabel = cellstr(sprintf('%.1f\n',ax.XTick));
ax.XMinorTick = 'off'; %Turn off minor ticks
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Log Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!