【问题标题】:Matlab - plot - How to get the x-axis labels in a color keeping the x-axis color black and the first tick value hidden?Matlab - plot - 如何获取 x 轴标签的颜色,保持 x 轴颜色为黑色并隐藏第一个刻度值?
【发布时间】:2015-12-20 01:25:40
【问题描述】:

如何将 x 轴标签 2 4 6 设为蓝色,保持 x 轴颜色为黑色并隐藏第一个刻度值?

当我尝试更改刻度标签颜色时,刻度标签消失

x=0:0.25:5
y=sin(x)

ax1 = subplot(1,2,1) ;
plot(x,y)
set(gca, 'YAxisLocation', 'right')
xlabel('x','Color','b')
ylabel('y')
%set(gca,'xColor','k');
%set(gca,'xticklabel','b')
Q=get(gca,'xtick');
R=get(gca,'xticklabel');
set(gca,'xtick',Q(2:end))
set(gca,'xticklabel',R(2:end,:))

【问题讨论】:

    标签: matlab plot


    【解决方案1】:

    您可以使用轴的the undocumented XRuler property 来做到这一点:

    h = gca;
    h.XRuler.TickLabels.ColorData = uint8([0;0;255;255]);
    

    请注意,这在旧版本的 MATLAB 中可能不可用,它在 2015a 上适用于我

    【讨论】:

      【解决方案2】:

      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
      

      最终结果如下所示:

      如前所述,注意事项:您将轴对象复制两次,这是一种浪费。如果调整图形大小,结构似乎会内爆。我不知道如何解决这些问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-28
        • 1970-01-01
        • 1970-01-01
        • 2018-01-10
        • 1970-01-01
        • 2020-01-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多