Heatmap with circles indicating size of population

后端 未结 3 432
长发绾君心
长发绾君心 2020-12-06 15:00

Hi I would like to produce a heatmap in Python, similar to the one shown, where the size of the circle indicates the size of the sample in that cell. I looked in seaborn\'s

3条回答
  •  余生分开走
    2020-12-06 15:19

    Here's a possible solution using Bokeh Plots:

    import pandas as pd
    from bokeh.palettes import RdBu
    from bokeh.models import LinearColorMapper, ColumnDataSource, ColorBar
    from bokeh.models.ranges import FactorRange
    from bokeh.plotting import figure, show
    from bokeh.io import output_notebook
    
    import numpy as np
    
    output_notebook()
    
    d = dict(x = ['A','A','A', 'B','B','B','C','C','C','D','D','D'], 
             y = ['B','C','D', 'A','C','D','B','D','A','A','B','C'], 
             corr = np.random.uniform(low=-1, high=1, size=(12,)).tolist())
    
    df = pd.DataFrame(d)
    
    df['size'] = np.where(df['corr']<0, np.abs(df['corr']), df['corr'])*50
    #added a new column to make the plot size
    
    colors = list(reversed(RdBu[9]))
    exp_cmap = LinearColorMapper(palette=colors, 
                                 low = -1, 
                                 high = 1)
    
    
    p = figure(x_range = FactorRange(), y_range = FactorRange(), plot_width=700, 
               plot_height=450, title="Correlation",
               toolbar_location=None, tools="hover")
    
    p.scatter("x","y",source=df, fill_alpha=1,  line_width=0, size="size", 
              fill_color={"field":"corr", "transform":exp_cmap})
    
    p.x_range.factors = sorted(df['x'].unique().tolist())
    p.y_range.factors = sorted(df['y'].unique().tolist(), reverse = True)
    
    p.xaxis.axis_label = 'Values'
    p.yaxis.axis_label = 'Values'
    
    bar = ColorBar(color_mapper=exp_cmap, location=(0,0))
    p.add_layout(bar, "right")
    
    show(p)
    
    

提交回复
热议问题