2014-02-21 24 views
4

如何为单独的颜色(例如,绿色的“a”,蓝色的“b”,红色的“c”)赋予标签“a”,“b”,“c”下面的例子?在matplotlib中为xticklabels单独设置颜色

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.

回答

8

代码:

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

相关问题