【问题标题】:Multi-Coloured bars in matplotlibmatplotlib 中的多色条
【发布时间】:2011-11-28 01:52:50
【问题描述】:

我在 matplotlib 中有一个图表(显示正确),但我希望每个条有不同的颜色(尽管仍然是相同的条形图)。这可能吗?

谢谢

【问题讨论】:

    标签: python python-3.x matplotlib


    【解决方案1】:

    如果您在图表创建时着色:

    In [15]: x= range(5)
    In [16]: y = [10, 23, 12, 45, 32]
    In [17]: color = ['r', 'b', 'y', 'g', 'c']
    In [18]: lines = bar(x, y, color=color)
    

    如果您想在创建图表后更改第一个条形的颜色,请注意您在 lines 中获得了您的条形列表:

    In [19]: lines      
    Out[19]:
    [<matplotlib.patches.Rectangle object at 0x02
     <matplotlib.patches.Rectangle object at 0x02
     <matplotlib.patches.Rectangle object at 0x02
     <matplotlib.patches.Rectangle object at 0x02
     <matplotlib.patches.Rectangle object at 0x02
    

    然后,只需简单地设置它的颜色:

    In [20]: lines[0].set_color('c')    #changes from original red to cyan
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 2021-01-07
      • 2017-07-16
      相关资源
      最近更新 更多