【问题标题】:Pass argument to reduce_C_function in matplotlib hexbin plot在 matplotlib hexbin 图中将参数传递给 reduce_C_function
【发布时间】:2018-07-12 17:21:36
【问题描述】:

我正在尝试在函数中实现一个 hexbin 图,并且有一个相当复杂的 reduce_C_function 需要接收参数 a。一个(尽管微不足道)的例子:

    def sum_i(z,a):
        return a*np.sum(z)
    def some_function(X,Y,Z,a):
        hexb = plt.hexbin(X,Y,C=Z,reduce_C_function=sum_i)

现在,matplotlib 文档 (https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.hexbin.html) 对reduce_C_function 的使用并没有提供非常丰富的信息,那么我该如何设法传递a 呢?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您可以从您的sum_i 创建一个partial 函数并将其作为单个参数的函数传递给plt.hexbin

    from functools import partial
    
    def sum_i(z,a):
        return a*np.sum(z)
    def some_function(X,Y,Z,a):
        reduce_function = partial(sum_i, a=a)
        hexb = plt.hexbin(X,Y,C=Z,reduce_C_function=reduce_function)
    

    source code 的外观来看,没有其他方法可以为reduce_C_function 传递额外的参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-27
      • 2013-12-05
      • 1970-01-01
      • 2018-01-08
      • 2011-10-07
      • 2011-06-16
      • 1970-01-01
      相关资源
      最近更新 更多