在matplotlib中单独设置xticklabels的颜色

时间:2014-02-21 13:38:36

标签: python matplotlib boxplot

如何在下面的例子中给出标签“a”,“b”,“c”个别颜色(例如“a”为绿色,“b”为蓝色,“c”为红色)?

import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
p = plt.boxplot(np.random.normal(size=(10,3)))
ax.set_xticklabels(list("abc"))
plt.show()

Example of boxplot without individually colored labels.

1 个答案:

答案 0 :(得分:13)

代码:

import numpy as np
import matplotlib.pyplot as plt
    fig, ax = plt.subplots()
    p = plt.boxplot(np.random.normal(size=(10,3)))
    ax.set_xticklabels(list("abc"))

[t.set_color(i) for (i,t) in
 zip(['red','green','blue'],ax.xaxis.get_ticklabels())]

plt.show()

给我:

enter image description here