Matplotlib 设置单个刻度样式 [英] Matplotlib set individual tick style

查看:54
本文介绍了Matplotlib 设置单个刻度样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单明了的问题.假设我已将以下X勾号设置为

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

是否可以将 x = -5 处的刻度设置为绿色,将 x = 0 处的刻度设置为不同的线条(例如实心-")样式,并将 x = 5 处的刻度设置为蓝色?

谢谢!

解决方案

不是所有的tick参数都可以修改,只有其中一些可以修改.请参阅:

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!

解决方案

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.

这篇关于Matplotlib 设置单个刻度样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆