python - Matplotlib 设置单独的刻度样式

标签 python python-3.x matplotlib

简单直接的问题。假设我已经设置了以下 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.

enter image description here

关于python - Matplotlib 设置单独的刻度样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33695057/

相关文章:

python - 如何为 numpy genfromtxt 设置字符串到浮点转换器

python - tkinter 滚动条折叠它所在的 Canvas

python - 如何在python中遍历二维列表

python - 如何按特定顺序对第一个字母进行排序,并按其他特定顺序对第二个字母进行排序

python - 获取文件的最后n行——解码错误

python - 获取 matplotlib 直方图函数中的 bin 信息

python - Matplotlib 无法通过 PyCharm 工作

python - pick_event 使用 matplotlib 获取点

Python line_profiler 安装

python - 比较并返回字典字符串值?