我有一個使用雙 y 軸的圖yyaxis。我想設定XTick為只有整數值,例如 1、2、3、4、5、6,而不是 1、1.5、2、2.5、3、3.5、4、4.5、5、5.5、6。
使用 normal plot,我可以使用:
ax = gca;
ax.XTick = unique(round(ax.XTick));
以實作這一目標。
但這似乎不是yyaxis情節的情況。
請幫忙!
更新:這是一個 MWE 來說明我的問題:
figure('Position', get(0, 'Screensize'));
max_iter = 100;
A_history = nan(1,max_iter); B_history = nan(1,max_iter);
for iter = 1 : max_iter
yyaxis left;
A_history(iter) = randn(1);
plot(A_history(1:iter), '-ob', 'LineWidth', 0.9, 'MarkerSize', 4.5, 'MarkerFaceColor', 'b');
ylabel('first yaxis');
yyaxis right;
B_history(iter) = randn(1);
plot(B_history(1:iter), '-or', 'LineWidth', 0.6, 'MarkerSize', 3, 'MarkerFaceColor', 'r');
ylabel('second yaxis');
% ax = gca;
% ax.XTick = unique(round(ax.XTick));
pause(1);
end
上面的代碼將xticks在第一次迭代中創建0, 0.1, 0.2, 0.3, 0.4, ..., 1 ;例如,第二次迭代的 xticks 為 1, 1.1, 1.2, 1.3, 1.4, ..., 2。
我想要的是去掉 中的所有非整數值(例如,0.1, ... ,0.9, 1.1, ... ,1.9)xticks,只保留整數值(例如,第一次迭代時為 0,1 ; 1,2 用于第二次迭代)。
我試圖添加注釋的 2 行代碼。但不幸的是,他們沒有給我預期的結果。更糟糕的是,它xticks會一直變成 [0,1] ......
希望這能讓問題描述更清楚。
uj5u.com熱心網友回復:
它實際上是在您執行以下操作時'XTickMode'設定的屬性'manual':
ax = gca;
ax.XTick = unique(round(ax.XTick));
'auto'在嘗試僅保留唯一的舍入值之前,您可以將其更改回以評估新的 xticks。IE
ax = gca;
ax.XTickMode= 'auto';
ax.XTick = unique(round(ax.XTick));
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/361463.html
標籤:MATLAB
