【问题标题】:Set color for xticklabels individually in matplotlib在 matplotlib 中分别为 xticklabels 设置颜色
【发布时间】:2014-03-23 01:27:59
【问题描述】:
如何在下面的示例中为标签“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()
【问题讨论】:
标签:
python
matplotlib
boxplot
【解决方案1】:
代码:
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()
给我: