【问题标题】:Pandas: what is plot in df.plot.bar()? Method or object?熊猫:df.plot.bar() 中的情节是什么?方法还是对象?
【发布时间】:2021-05-03 09:00:54
【问题描述】:

我已经看到 df.plot(kind='bar') 和 df.plot.bar() 在使用中。对于前者,我认为 plot 是一种方法,使用 kind 作为默认参数。对于后者,情节似乎是一个对象。

那么 df.plot 中的“绘图”是一个对象或方法吗?

谢谢!

【问题讨论】:

  • 如果一个python对象的类实现了__call__dunder方法,它也可以被调用。所以“类方法行为”和“类对象行为”并不是相互排斥的……

标签: python python-3.x pandas matplotlib


【解决方案1】:

如果一个python对象的类实现了__call__dunder方法,它也可以被调用。所以“类方法行为”和“类对象行为”并不是相互排斥的:

class TwoNumberOperation:
    
    def __init__(self, x1, x2):
        self.x1 = x1
        self.x2 = x2
        
    def addition(self):
        
        return self(what="add")
    
    def multiplication(self):
        
        return self(what="mult")
    
    def __call__(self, what="add"): 
        
        if what == "add":
            return self.x1 + self.x2
        elif what == "mult":
            return self.x1 * self.x2
        else:
            raise ValueError(f"{what} is not a valid operation")


two_n = TwoNumberOperation(1, 2)

现在我们可以直接调用这个实例了

two_n(what="mult")
# output: 2

或通过显式调用addition 方法:

two_n.addition()
# output:  3

现在是熊猫:

如果我们查看源代码,绘图属性在最后添加到DataFrame 类(link):

class DataFrame(NDFrame):
    ...

    # ----------------------------------------------------------------------
    # Add plotting methods to DataFrame
    plot = CachedAccessor("plot", pandas.plotting.PlotAccessor)
    ...

pandas.plotting.PlotAccessor 定义了 DataFrame 类中可用的大多数绘图方法,包括 .line (link)、.bar (link) 等。

它的 __call__ 方法 (link) 进行一些数据操作并将数据转发到绘图后端。它需要一个kind= 参数(这实际上就是为什么你可以使用df.plot(kind="bar") 而不是df.plot.bar())。

与上面的TwoNumberOperation 示例类似,pd.Dataframe.plot.bar 等方法在底层调用了PlotAccessor.__call__ 方法,所以df.line 实际上只是(link)

    def line(self, x=None, y=None, **kwargs):

        return self(kind="line", x=x, y=y, **kwargs)

所有的魔法都发生在__call__ 方法中!

【讨论】:

    猜你喜欢
    • 2018-10-10
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 2013-05-12
    • 2021-08-09
    • 2017-06-05
    • 2020-09-28
    • 2023-03-29
    相关资源
    最近更新 更多