Yannick
Yannick

Reputation: 872

Matplotlib set individual tick style

Simple and straightforward question. Say I have set the following X ticks with

plt.xticks([-5,-4,-3,-2,-1,0,1,2,3,4,5])

Is it possible to set the tick at x = -5 to be green, the tick at x = 0 to have a different line (solid for example "-") style and the tick at x = 5 to be blue?

Thanks!

Upvotes: 2

Views: 1757

Answers (1)

CT Zhu
CT Zhu

Reputation: 54390

Not all the tick parameters can be modified, only some of them can be. See: document.

However, you can change the color:

plt.plot(np.linspace(-10, 10), np.linspace(-10, 10))
plt.gca().tick_params('x', length=20, width=2, which='major')

xTicks = plt.xticks([-5,-4,-3,-2,-1,0,1,2,3,4,5])

xTicks_cld = xTicks[0][0].get_children()
xTicks[0][0].get_children()[0].set_color('g') 
#change the color of the 1st bottom tick

xTicks[0][0].get_children()[1].set_color('r') 
#change the color of the 1st top tick

xTicks[0][-1].get_children()[0].set_color('b') 
#change the color of the last bottom tick

xTicks[0][5]._apply_params(color='r') 
#change the color of the ticks of x=0, both top and bottom xaxis.

enter image description here

Upvotes: 4

Related Questions