Matplotlib设置了单独的刻度样式

时间:2015-11-13 14:31:58

标签: python python-3.x matplotlib

简单明了的问题。假设我用

设置了以下X刻度
plt.xticks([-5,-4,-3,-2,-1,0,1,2,3,4,5])

是否可以将x = -5处的刻度设置为绿色,将x = 0处的刻度设置为具有不同的线(实例为#34; - ")样式以及x处的刻度线= 5是蓝色?

谢谢!

1 个答案:

答案 0 :(得分:0)

并非所有刻度参数都可以修改,只有部分可以修改。请参阅:document

但是,您可以更改颜色:

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