Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

plot

matlab

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))

Now, can I e.g. make the number 3 on the x-axis red?

like image 257
Stewie Griffin Avatar asked May 21 '13 18:05

Stewie Griffin


1 Answers

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

like image 185
Eitan T Avatar answered Oct 31 '22 15:10

Eitan T