简单直接的问题。假设我已经设置了以下 X 个刻度
plt.xticks([-5,-4,-3,-2,-1,0,1,2,3,4,5])
是否可以将 x = -5 处的刻度设置为绿色,将 x = 0 处的刻度设置为不同的线条(实线,例如“-”)样式,并将 x = 5 处的刻度设置为蓝色?
谢谢!
最佳答案
并不是所有的刻度参数都可以修改,只有部分参数可以。请参阅: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.
关于python - Matplotlib 设置单独的刻度样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33695057/