Search code examples
pythonmatplotlibcolorspyqtseaborn

How to change the color of the axis, ticks and labels


I'd like to change the color of the axis, as well as ticks and value-labels for a plot I did using matplotlib and PyQt.


Solution

  • As a quick example (using a slightly cleaner method than the potentially duplicate question):

    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    ax.plot(range(10))
    ax.set_xlabel('X-axis')
    ax.set_ylabel('Y-axis')
    
    ax.spines['bottom'].set_color('red')
    ax.spines['top'].set_color('red')
    ax.xaxis.label.set_color('red')
    ax.tick_params(axis='x', colors='red')
    
    plt.show()
    

    alt text

    Alternatively

    [t.set_color('red') for t in ax.xaxis.get_ticklines()]
    [t.set_color('red') for t in ax.xaxis.get_ticklabels()]