2014-02-21 27 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

相關問題