受this answer 的启发,我决定尝试以类似的方式解决您的问题。我能够找到一个可行的解决方案,但它并不漂亮,因为您需要复制几个轴对象,而且它似乎不适合调整图形的大小。希望它仍然会有所帮助!
代码如下。
%//Original code
x=0:0.25:5;
y=sin(x);
ax1 = subplot(1,2,1);
plot(x,y)
set(ax1, 'YAxisLocation', 'right')
ylabel(ax1, 'y');
xlabel(ax1, 'x', 'Color', 'b'); %// Give the blue 'x' as label
%//Solution
my_xticks = [2 4 6]; %// The XTicks you want to show
drawnow; %//Must draw the axes here due to YAxisLocation, otherwise will not work
ax2 = copyobj(ax1, gcf); %// Create a copy the axes
set(ax2, 'XTick', my_xticks, 'XColor', 'b', 'Color', 'none') %// Keep only my_xticks in blue
ax3 = copyobj(ax1, gcf); %// Create another copy...
set(ax3, 'XTick', [], 'Color', 'none'); %// From which we keep only the black gridline
xlabel(ax3, ''); %// Remove the xlabel from ax3 (would show x in wrong position)
set(ax1, 'xtick', my_xticks); %// In ax1, show black ticks at desired locations
最终结果如下所示:
如前所述,注意事项:您将轴对象复制两次,这是一种浪费。如果调整图形大小,结构似乎会内爆。我不知道如何解决这些问题。