How can I assign multiple colors to tick labels in plots in MATLAB?

后端 未结 3 1068
耶瑟儿~
耶瑟儿~ 2020-12-19 13:33

Is it possible to color a single number (or a set of numbers) on one of the axes in MATLAB?

Suppose I have a plot:

plot(1:10, rand(1,10))
         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-19 13:46

    Unfortunately, you cannot have multiple colors for tick labels in one axes object. However, there's a solution (inspired by this page from MathWorks support site) that achieves the same effect. It overlays the existing axes it with another axes that has only one red tick.

    Here's an example:

    figure
    plot(1:10, rand(1,10))
    ax2 = copyobj(gca, gcf);                             %// Create a copy the axes
    set(ax2, 'XTick', 3, 'XColor', 'r', 'Color', 'none') %// Keep only one red tick
    ax3 = copyobj(gca, gcf);                             %// Create another copy
    set(ax3, 'XTick', [], 'Color', 'none')               %// Keep only the gridline
    

    The result is:

    result

提交回复
热议问题