Matplotlib: coloring axis/tick labels

后端 未结 2 1166
梦谈多话
梦谈多话 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:39
      label = plt.ylabel("y-label")
      label.set_color("red")
    

    similarly, you can obtain and modify the tick labels:

    [i.set_color("red") for i in plt.gca().get_xticklabels()]
    
    0 讨论(0)
  • 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.

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