【问题标题】:Matplotlib set individual tick styleMatplotlib 设置单独的刻度样式
【发布时间】:2015-11-13 14:31:58
【问题描述】:

简单直接的问题。假设我用

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

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

谢谢!

【问题讨论】:

    标签: python python-3.x matplotlib


    【解决方案1】:

    并非所有的刻度参数都可以修改,只有部分可以。见: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.
    

    【讨论】:

      猜你喜欢
      • 2020-10-01
      • 2013-02-21
      • 1970-01-01
      • 2011-09-23
      • 2020-06-06
      • 1970-01-01
      • 2016-02-13
      • 2018-10-06
      • 1970-01-01
      相关资源
      最近更新 更多