Set color for xticklabels individually in matplotlib

后端 未结 1 1781
梦谈多话
梦谈多话 2021-01-02 02:13

How can I give the labels \"a\", \"b\", \"c\" individual colors (e.g. \"a\" in green, \"b\" in blue, \"c\" in red) in the example below?

import numpy as np
i         


        
1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-02 02:49

    The code:

    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()
    

    Gives me:

    enter image description here

    0 讨论(0)
提交回复
热议问题