Python 使用HoloViews+;绘制与热图相连的高斯曲线;博克

Python 使用HoloViews+;绘制与热图相连的高斯曲线;博克,python,plot,bokeh,holoviews,Python,Plot,Bokeh,Holoviews,全部 我对HV完全陌生(实际上是从昨晚开始),所以要温柔 我在玩弄HV,所以我构建了一个热图,其中x和y是平均值和标准偏差(sigma)的一些值。然后,对于平均值和西格玛的每个组合,我计算变异系数(只是西格玛和平均值的比率)。我已经成功地做到了 现在我要做的是,对于热图中的每个单元格,绘制一个高斯分布,对应于标准和平均值组合。我不知道怎么做。有人有主意吗?(如你所见,我尝试过,但没有成功。) 其次,我不知道如何更改我的悬停工具来显示一些自定义信息,比如说mean、std和cv,而不是x、y和z

全部

我对HV完全陌生(实际上是从昨晚开始),所以要温柔

我在玩弄HV,所以我构建了一个热图,其中x和y是平均值和标准偏差(sigma)的一些值。然后,对于平均值和西格玛的每个组合,我计算变异系数(只是西格玛和平均值的比率)。我已经成功地做到了

  • 现在我要做的是,对于热图中的每个单元格,绘制一个高斯分布,对应于标准和平均值组合。我不知道怎么做。有人有主意吗?(如你所见,我尝试过,但没有成功。)
  • 其次,我不知道如何更改我的悬停工具来显示一些自定义信息,比如说mean、std和cv,而不是x、y和z
  • 第三,如何将此HV代码用作HTML(输出HTML文件?),以便将其导入网站
  • 提前谢谢

    import pandas as pd
    import numpy as np
    import holoviews as hv
    from holoviews import opts
    hv.extension('bokeh')
    
    # here the mean and std vales are given (hardcoded)
    mu = [-1, 5, 10]
    std = [10, 50, 90]
    
    # make an empty list to append coefficient of variation
    cov = []
    
    # calculate the coefficient of variation c_v = std/mu for all combos of std and mu
    
    for i in range(0, len(mu)):
        for j in range(0, len(std)):
            cov.append(std[j] / mu[i])
    
    # to place the values of std and mean on a 2D plot, values have to be
    # repeated in a certain way, therefore
    
    std_rep = np.tile(std, 3)
    mu_rep = np.repeat(mu, 3)
    
    sigma = std_rep.astype(str)
    mean = mu_rep.astype(str)
    
    ###heatmap entries per cell have to be strings, like this
    sigma2=['10', '50', '90', '10', '50', '90', '10', '50', '90']
    mean2 = ['-1', '-1', '-1', '5', '5', '5', '10', '10', '10']
    ################################################
    
    # make heatmap
    heatmap = hv.HeatMap((sigma, mean, cov))
    
    
    
    # declare tap stream with heatmap as source and initial values
    posxy = hv.streams.Tap(source=heatmap, x='sigma', y='mean')
    
    # Define function to compute histogram based on tap location
    def tap_histogram(x, y):
        
        x_vals = np.arange(-100, 100, 0.1)
        y_vals = norm(mean, sigma)
    
        plt.plot(x_vals, y_vals.pdf(x_vals))
        
        return hv.Curve((x_vals, y_vals.pdf(x_vals)), mean, sigma) 
    
    
    
    (heatmap).opts(opts.HeatMap(cmap='RdBu', tools=['hover', 'tap'], colorbar=True, 
                                width=500, height=500, toolbar='above', clim=(-100,15),
                                title ='coefficient of variation',
                                fontsize={'xticks': '10pt', 'yticks': '10pt', 
                                          'xlabel': '10pt', 'ylabel': '10pt',
                                         'title': '15pt'},
                                xlabel='STD', ylabel='MEAN'
                               
                               
                               ))
    
    #cmap examples cmpap = 'RdBu', 'Viridis', etc
    # more at http://holoviews.org/user_guide/Colormaps.html
    

    多亏了Aureliansciarra,这一问题得到了及时解决

    import pandas as pd
    import numpy as np
    import holoviews as hv
    from holoviews import opts
    hv.extension('bokeh')
    from bokeh.models import HoverTool
    from holoviews import streams
    # here the mean and std vales are given (hardcoded)
    mu = [-1, 5, 10]
    std = [10, 50, 90]
    
    # make an empty list to append coefficient of variation
    cov = []
    
    # calculate the coefficient of variation c_v = std/mu for all combos of std and mu
    
    for i in range(0, len(mu)):
        for j in range(0, len(std)):
            cov.append(std[j] / mu[i])
    
    # to place the values of std and mean on a 2D plot, values have to be
    # repeated in a certain way, therefore
    
    std_rep = np.tile(std, 3)
    mu_rep = np.repeat(mu, 3)
    
    sigma = std_rep.astype(str)
    mean = mu_rep.astype(str)
    
    ###heatmap entries per cell have to be strings, like this
    sigma2=['10', '50', '90', '10', '50', '90', '10', '50', '90']
    mean2 = ['-1', '-1', '-1', '5', '5', '5', '10', '10', '10']
    ################################################
    
    # make heatmap
    heatmap = hv.HeatMap((sigma, mean, cov))
    
    
    
    # declare tap stream with heatmap as source and initial values
    posxy = hv.streams.Tap(source=heatmap, x=10, y=10)
    
    # Define function to compute histogram based on tap location
    def tap_histogram(x, y):
        points = np.random.normal(int(y), int(x),1000)
        h= hv.Distribution(points).opts(width=500, height=500)
        return h
    
    hoverCustom = HoverTool(tooltips={'mean':'@x','std':'@y','cv':'@z'})
    
    heat=(heatmap).opts(opts.HeatMap(cmap='RdBu', tools=[hoverCustom, 'tap'], colorbar=True, 
                            width=500, height=500, toolbar='above', clim=(-100,15),
                            title ='coefficient of variation',
                            fontsize={'xticks': '10pt', 'yticks': '10pt', 
                                      'xlabel': '10pt', 'ylabel': '10pt',
                                     'title': '15pt'},
                            xlabel='STD', ylabel='MEAN'
                           
                           
                           ))
    
    tap_dmap = hv.DynamicMap(tap_histogram, streams=[posxy])
    
    (heat+tap_dmap)