Change color of specific ticks at plot with matplotlib

后端 未结 1 1632
天命终不由人
天命终不由人 2020-12-06 11:40

Using matplotlib, is there an option to change the color of specific tick labels on the axis?

I have a simple plot that show some values by days, an

1条回答
  •  独厮守ぢ
    2020-12-06 12:17

    You can get a list of tick labels using ax.get_xticklabels(). This is actually a list of text objects. As a result, you can use set_color() on an element of that list to change the color:

    import matplotlib.pyplot as plt
    
    fig, ax = plt.subplots(figsize=(5,4))
    ax.plot([1,2,3])
    
    ax.get_xticklabels()[3].set_color("red")
    
    plt.show()
    

    Alternatively, you can get the current axes using plt.gca(). The below code will give the same result

    import matplotlib.pyplot as plt
    
    plt.figure(figsize=(5,4))
    plt.plot([1, 2, 3])
    
    plt.gca().get_xticklabels()[3].set_color("red")
    
    plt.show()
    

    0 讨论(0)
提交回复
热议问题