Matplotlib: coloring axis/tick labels

后端 未结 2 1176
梦谈多话
梦谈多话 2020-12-09 10:03

How would one color y-axis label and tick labels in red?

So for example the \"y-label\" and values 0 through 40, to be colored in red.

2条回答
  •  难免孤独
    2020-12-09 10:51

    The xlabel can be colorized when setting it,

    ax.set_xlabel("x-label", color="red")
    

    For setting the ticklabels' color, one may either use tick_params, which sets the ticklabels' as well as the ticks' color

    ax.tick_params(axis='x', colors='red')
    

    Alternatively, plt.setp can be used to only set the ticklabels' color, without changing the ticks' color.

    plt.setp(ax.get_xticklabels(), color="red")
    

    Note that for changing the properties on the y-axis, one can replace the x with a y in the above.

提交回复
热议问题