Matplotlib:着色轴/刻度标签

时间:2013-01-04 21:33:10

标签: matplotlib colors label text-coloring

如何将y轴标签和刻度标签涂成红色?

例如,“y-label”和值0到40,用红色着色。 sample_image

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)

fig = plt.figure()
ax = plt.subplot(111)
ax.set_ylabel("y-label")

for i in xrange(5):
    ax.plot(x, i * x, label='$y = %ix$' % i)

ax.legend()

plt.show()

2 个答案:

答案 0 :(得分:20)

  label = plt.ylabel("y-label")
  label.set_color("red")

同样,您可以获取并修改刻度标签:

[i.set_color("red") for i in plt.gca().get_xticklabels()]

答案 1 :(得分:14)

设置时,xlabel可以着色,

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

用于设置勾选标签'颜色,可以使用tick_params,它设置勾选标签'以及蜱虫'颜色

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

enter image description here

或者,plt.setp可用于仅设置勾选标签'颜色,不改变刻度'颜色。

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

enter image description here

请注意,要更改y轴上的属性,可以用上面的y替换x。

相关问题