【问题标题】:How to set colors using a comprehension list in matplotlib?如何使用 matplotlib 中的理解列表设置颜色?
【发布时间】:2017-11-07 12:39:42
【问题描述】:

这个问题被最初不理解它的人垄断和否决。提供的答案具有挑衅性、徒劳的冗长和故意误导。考虑到我的问题无法删除,我决定删除它的内容并发布一个新的。

幸运的是,我得到了一个更加宽容和礼貌的答案。

【问题讨论】:

  • 你看过我给你的两个链接了吗?仅仅知道df 表示一个DataFrame 是不够的。
  • 我们意识到df 的含义,@ImportanceOfBeingErnest 的含义可能是df 看起来 的样子。 IE。提供一些代码,其中包含一些重现问题的虚假数据,以便我们可以在我们的机器上运行它。换句话说,MCVE.
  • @DavidG 实际上你永远无法确定。所以只是为了表明如果df 不是数据框,问题中的代码实际上可以工作,我添加了一个无意义的答案。

标签: bar-chart


【解决方案1】:

这是为了说明为什么需要可验证的示例。

人们肯定会想到一个数据结构,其中来自问题的代码实际上会产生所需的结果,如下所示。

import numpy as np; np.random.seed(1)
import matplotlib.pyplot as plt

class C(list):
    def __init__(self, l):
        self.curr = 0
        list.__init__(self, l)
        self.get = list.__getitem__

    def plot(self, kind, color):
        plt.bar(range(len(self)), self, color=color)

    def __getitem__(self,index):
        self.curr = self.index(index)
        return self

    def count(self):
        return self.get(self,self.curr)


df = C(list(np.random.rand(15)*80000))    

# unchanged code from the question      
df.plot(kind='bar', color=['powderblue' if df[e].count() < 40000 else 'red' for e in df])

plt.show()

因此,重要的是通过提供mcve 来明确什么是什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-08
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    • 2012-02-14
    相关资源
    最近更新 更多