【问题标题】: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()
    

    给我:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-23
      • 2020-09-24
      相关资源
      最近更新 更多