【问题标题】:Plotting a function with 4 arguments in a grid of heatmaps在热图网格中绘制具有 4 个参数的函数
【发布时间】:2018-11-10 18:37:27
【问题描述】:

我在 python 2 中使用 plotnine(但很高兴使用 matplotlib 或任何其他绘图包的解决方案)。我有一个带有 4 个参数的函数(下面稍微简化了)。我想绘制一个热图网格,“超轴”改变两个参数,每个热图改变另外两个。有点像这样:

我想出了这个代码:

from plotnine import ggplot, aes, facet_grid, geom_tile
import pandas as pd
import itertools

def fun((i, j, n, p)):
    if n > j:
        return 1 if (p**(3*n)) > ((p+i)**(3*(n-j))) else 0
    return -1

ilist, jlist, nlist, plist = range(1,10), range(1,9), range(8,10), range(4,6)
rows = itertools.product(ilist, jlist, nlist, plist)

df = pd.DataFrame(list(rows))
df.columns = ['i','j','n','p']
df['fun'] = df.apply(fun, axis=1)

(ggplot(df, aes('factor(i)', 'factor(j)')) +
 geom_tile(aes(fill='fun')) +
facet_grid('n ~ p', labeller='label_both'))

这会产生以下内容:

这(或多或少)具有我想要的形式,但热图似乎是错误的。 (编辑:我发现错误出现在 fun 的定义中,而不是在绘图中 - 请参阅我的答案)。

【问题讨论】:

    标签: python matplotlib plot heatmap plotnine


    【解决方案1】:

    好的...事实证明不是绘图有问题,而是功能有问题。我不完全理解出了什么问题,但我认为一些计算遇到了“巨大的整数”问题。当我运行它以在日志空间中进行比较时,它开始工作。

    我将把它留在这里,因为它可能会帮助其他尝试制作类似热图网格的人,也许有人会以更好的方式回答。

    这是我的带注释的代码,用于从具有 ['i', 'j', 'n', 'p'] 列的数据框中制作热图本身:

    # The first two lines are defining the subplots, i.e. the heatmaps
    # themselves. I am passing the dataframe, and specifying the names of the
    # columns to use as axes. I say 'factor(i)' etc to treat the i column as
    # discrete, not continuous (in my case it's integers).
    gg = (ggplot(df, aes('factor(i)', 'factor(j)')) +
        geom_tile(aes(fill='fun')) +
    # The last bit is to call facet_grid which applies the above code
    # in a grid. The parameter 'n ~ p' specifies that I want the grid to be
    # over the columns 'n' and 'p' from the dataframe. The labeller
    # parameter is what makes the labels at the edges (see top and right in
    # image below) show both the column name and the value).
        facet_grid('n ~ p', labeller='label_both'))
    

    下面是完整的修正后的代码:

    from plotnine import ggplot, aes, facet_grid, geom_tile
    import pandas as pd
    import numpy as np
    import itertools
    
    def fun((i, j, n, p)):
        if n > j:
            return 1 if np.log10(p) * (3*n) > np.log10(p+i) * (3*(n-j)) else 0
    
        return -1
    
    ilist, jlist, nlist, plist = range(1,10), range(1,9), range(8,10), range(4,6)
    rows = itertools.product(ilist, jlist, nlist, plist)
    df = pd.DataFrame(list(rows))
    df.columns = ['i','j','n','p']
    df['fun'] = df.apply(fun, axis=1)
    
    gg = (ggplot(df, aes('factor(i)', 'factor(j)')) +
        geom_tile(aes(fill='fun')) +
        facet_grid('n ~ p', labeller='label_both'))
    
    gg.draw()
    

    【讨论】:

      猜你喜欢
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      相关资源
      最近更新 更多